Skip to content

Possible bug? render jsonapi returning error: undefined method `any?' for object #52

@kitsuneyo

Description

@kitsuneyo

Apologies for a second issue in a few days. This one is probably something I'm doing wrong, but I don't know how to fix it and hoping someone can help.

I'm using render jsonapi: @record across every show, create, and update action in my API, and it works great, except for in one controller.

I have a model Media::Image and a serializer Media::ImageSerializer. Here's the serializer:

class Media::ImageSerializer < ApplicationSerializer
  set_type :image

  attribute :original do |record|
    record.file.url.presence if record.file.url
  end

  attribute :thumb do |record|
    record.file.versions[:th].url.presence if record.file.versions
  end

  attributes  :kind, :title, :description, :height, :width, :size, :year,
              :date, :usage_type, :attributed_name, :attributed_url,
              :source_url, :slug

  belongs_to  :uploaded_by, serializer: UserSerializer
  belongs_to  :media_license, serializer: Media::LicenseSerializer
  belongs_to  :place, serializer: PlaceSerializer
  has_many    :articles, serializer: ArticleSerializer

  meta do |record|
    base_meta(record)
  end
end

Expected Behavior

When setting @record = Media::Image.find(some_id) and calling render jsonapi: @record, status: :ok, the serializer should serialize the resource.

Actual Behavior

What actually happens is that an error is thrown, e.g.

NoMethodError: undefined method `any?' for #<Media::Image:0x00007fa11c436218>

Even if I remove every attribute and relationship from the serializer, I still get the error.

I can render collections just fine though.

I also have a Media::LicenseSerializer, and a corresponding model, which work as they should, so I don't think this is a naming issue.

Steps to Reproduce the Problem

I don't know how to reproduce this. This is just happening with one model & serializer, and I don't know why.

The method in jsonapi.rb that uses any? seem to be add_renderer!. But I'm not sure why this particular controller/serializer is acting this way. Could it be the renderer thinks it's dealing with a collection for some reason?

Right now I'm having to use render json: Media::ImageSerializer.new(@record).serializable_hash.to_json for single records, which means I can't use fields or include queries.

Specifications

  • Version: 1.7.0
  • Ruby version: 2.7.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions