jsonapi-serializable icon indicating copy to clipboard operation
jsonapi-serializable copied to clipboard

KeyFormat breaks included relationships

Open juni0r opened this issue 5 years ago • 0 comments

KeyFormat breaks included relationships when the relationship's name is subject to key transformation.

class Resource < JSONAPI::Serializable::Resource
  extend JSONAPI::Serializable::Resource::KeyFormat
  key_format ->(key) { key.to_s.camelize(:lower) }
end
class ContactResource < Resource
  type 'contacts'

  has_many :contact_groups

  attributes :first_name, :last_name, :email # , ...
end
class ContactGroupResource < Resource
  type 'contactGroups'

  attributes :name
end

Results in the relationship key to be transformed but related resources don't get included:

{
  "id": "046ffaf3-9c7b-4f36-a33b-1948cb889200",
  "type": "contacts",
  "attributes": {
    "firstName": "John",
    "lastName": "Doe",
    "email": '[email protected]'
  },
  "relationships": {
   "contactGroups": {
      "meta": {
        "included": false
      }
    }
  }
}

Without using KeyFormat it works as expected:

{
  "id": "046ffaf3-9c7b-4f36-a33b-1948cb889200",
  "type": "contacts",
  "attributes": {
    "first_name": "John",
    "last_name": "Doe",
    "email": '[email protected]',
  },
  "relationships": {
    "contact_groups": {
      "data": [
        {
          "type": "contactGroups",
          "id": "c88e3a19-f814-49d9-a458-40c2103f0003"
        }
      ]
    }
  }
}

juni0r avatar Oct 23 '20 17:10 juni0r