Skip to content

KeyFormat breaks included relationships #119

Open
@juni0r

Description

@juni0r

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"
        }
      ]
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions