blueprinter icon indicating copy to clipboard operation
blueprinter copied to clipboard

Unable to serialize activestorage filename

Open mrahul17 opened this issue 4 years ago • 2 comments

Hi, first of all I'd like to thank all contributors for making such an awesome gem :)

I've been using it in a project and came across a bug where serialization of filename of an ActiveStorage attachment is failing.

I have a setup like this model

class Foo < ApplicationRecord
 has_many_attached :bars
end

serializer

class FooSerializer < Blueprinter::Base
  association :bars_blobs, blueprint: BarBlobSerializer
end

class BarBlobSerializer < Blueprinter::Base
  field :filename
end

when my controller with the following code is executed

f  = Foo.find_by(id: 1)
f.bars.attach(params[:file])
render json: FooSerializer.render(f)

I get the following error:

    "status": 500,
    "error": "Internal Server Error",
    "exception": "#<ArgumentError: wrong number of arguments (given 1, expected 0)>",
        "Full Trace": [
            {
                "exception_object_id": 70022805390180,
                "id": 0,
                "trace": "activestorage (6.0.3.2) app/models/active_storage/filename.rb:70:in `to_json'"
            },
            {
                "exception_object_id": 70022805390180,
                "id": 1,
                "trace": "/usr/local/lib/ruby/2.6.0/json/common.rb:224:in `generate'"
            },
            {
                "exception_object_id": 70022805390180,
                "id": 2,
                "trace": "/usr/local/lib/ruby/2.6.0/json/common.rb:224:in `generate'"
            },
            {
                "exception_object_id": 70022805390180,
                "id": 3,
                "trace": "blueprinter (0.25.0) lib/blueprinter/configuration.rb:21:in `public_send'"
            },
            {
                "exception_object_id": 70022805390180,
                "id": 4,
                "trace": "blueprinter (0.25.0) lib/blueprinter/configuration.rb:21:in `jsonify'"
            },
            {
                "exception_object_id": 70022805390180,
                "id": 5,
                "trace": "blueprinter (0.25.0) lib/blueprinter/helpers/base_helpers.rb:66:in `jsonify'"
            },
            {
                "exception_object_id": 70022805390180,
                "id": 6,
                "trace": "blueprinter (0.25.0) lib/blueprinter/base.rb:193:in `render'"
            },

The error appears to be because the method defined here https://github.com/rails/rails/blob/0133428ffa09463070e9d2404995b78a6eeda8bb/activestorage/app/models/active_storage/filename.rb#L70 does not accept arguments, which is unlike the usual way to_json methods are redefined. But I am not sure if this an activestorage bug or blueprinter's .

mrahul17 avatar Aug 07 '20 17:08 mrahul17

@light94 What's the exception class and exception message?

philipqnguyen avatar Aug 11 '20 21:08 philipqnguyen

@philipqnguyen sorry for that, I must have removed it while reducing the length of the trace. I updated the above trace, here is the exception class

    "exception": "#<ArgumentError: wrong number of arguments (given 1, expected 0)>",

mrahul17 avatar Aug 12 '20 04:08 mrahul17

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Nov 03 '23 01:11 github-actions[bot]