jsonapi-rb / jsonapi-rails

Rails gem for fast jsonapi-compliant APIs.
http://jsonapi-rb.org
MIT License
318 stars 62 forks source link

snake case not working in include #118

Open matteo95g opened 4 years ago

matteo95g commented 4 years ago

Hello,

I was trying to return a serializer with a relationship and I noticed that in the relationship field I was always getting { meta { included: false } }

At first, I thought it was not finding the correct serializer for the relationship... But after some time I realized that if I put the name of the model in camelCase in the included array it started working...

This is the index method in the controller:

def index
  render jsonapi: Club.all, include: [:cover, :fieldFolder]
end

These are the serializers:

class SerializableClub < SerializableBase
  attribute :name
  attribute :category
  attribute :area
  attribute :formal
  attribute :created_at
  attribute :updated_at

  has_one :cover
  has_one :field_folder
end
class SerializableFieldFolder < SerializableBase
  attribute :club_id

  belongs_to :club
end
class SerializableBase < JSONAPI::Serializable::Resource
  extend JSONAPI::Serializable::Resource::KeyFormat
  key_format ->(key) { key.to_s.camelize(:lower) }

  type { @object.class.name.underscore.pluralize }
end

The thing is that I'm transforming the keys to camelCase in the base serializer.

Is this the correct way to transform the keys? Or I should do it in a different way?

If keys transformation is ok, I don't think that it should affect the format of the include array, maybe calling underscore for each included key fix the issue.

Thanks!