Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruby 3.4.2 and rails 7.2 JSON::ParserError #718

Open
danielavdh opened this issue Feb 28, 2025 · 0 comments
Open

ruby 3.4.2 and rails 7.2 JSON::ParserError #718

danielavdh opened this issue Feb 28, 2025 · 0 comments

Comments

@danielavdh
Copy link

danielavdh commented Feb 28, 2025

Hi,

with ruby 3.4.2 and rails 7.2.

adding (in my case to class Document)

include ImageUploader::Attachment.new(:image)

or

include ImageUploader::Attachment(:image)

causes a JSON::ParserError (unexpected end of input)

the reason is that image_data is stored as text (empty string "") and not as nil, whilst Shrine expects nil or valid JSON.

this still was no problem with ruby 3.2.5 and rails 7.0

work around:

# lib/shrine/plugins/handle_empty_strings.rb
class Shrine
  module Plugins
    module HandleEmptyStrings
      module AttacherClassMethods
        # Override the class method that initializes attachers from column data
        def from_column(data, **options)
          data = nil if data.is_a?(String) && data.empty?
          super(data, **options)
        end
      end
      
      module AttacherMethods
        # Override instance method for loading from column
        def load_column(data)
          data = nil if data.is_a?(String) && data.empty?
          super(data)
        end
        
        private
        
        # Override deserialization method
        def deserialize_column(data)
          data = nil if data.is_a?(String) && data.empty?
          super(data)
        end
      end
      
      def self.configure(uploader)
        # No need to try to access internal constants
      end
    end
    
    register_plugin(:handle_empty_strings, HandleEmptyStrings)
  end
end

add it first in initializers:

Shrine.plugin :handle_empty_strings

and that is not enough, also added in initializers: shrine_json_serializer_patch.rb

# config/initializers/shrine_json_serializer_patch.rb

# This needs to be loaded after Shrine is initialized
Rails.application.config.after_initialize do
  # Find the JsonSerializer class used by Shrine
  if defined?(Shrine::Plugins::Column::JsonSerializer)
    json_serializer = Shrine::Plugins::Column::JsonSerializer
  else
    # Try to find it through the Shrine options
    shrine_class = Shrine
    json_serializer = shrine_class.opts[:column][:serializer]
  end
  
  # Patch the load method to handle empty strings
  if json_serializer
    json_serializer.singleton_class.prepend(Module.new do
      def load(data)
        return nil if data.is_a?(String) && data.empty?
        super
      end
    end)
  end
end

Would you fix this?

Thanks, Daniela (supported by Claude 3.7)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant