Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyFormat breaks relationships #122

Open
juni0r opened this issue Oct 23, 2020 · 1 comment
Open

KeyFormat breaks relationships #122

juni0r opened this issue Oct 23, 2020 · 1 comment

Comments

@juni0r
Copy link

juni0r commented Oct 23, 2020

I'm using JSONAPI::Serializable::Resource::KeyFormat for camel-cased keys. However this breaks inclusion of relationships when the relationship's name is subject to key transformation:

# app/resources/contact_resource.rb
class ContactResource < JSONAPI::Serializable::Resource
  extend JSONAPI::Serializable::Resource::KeyFormat

  type 'contacts'

  key_format ->(key) { key.to_s.camelize(:lower) }

  has_many :contact_groups

  attributes :name, :email # ...
end
# app/controllers/contacts_controller.rb
class ContactsController < ApplicationController
  def index
    render jsonapi: Contact.all, include: [:contact_groups]
  end
end

Results in

# GET /api/v1/contacts
{
  "id": "046ffaf3-9c7b-4f36-a33b-1948cb889200",
  "type": "contacts",
  "attributes": {
    "name": "John Doe"
    "email": 'john.doe@email.org',
  },
  "relationships": {
   "contactGroups": {
      "meta": {
        "included": false
      }
    }
  }
}

Without using the key_format it works as expected:

# GET /api/v1/contacts
{
  "id": "046ffaf3-9c7b-4f36-a33b-1948cb889200",
  "type": "contacts",
  "attributes": {
    "name": "John Doe"
    "email": 'john.doe@email.org',
  },
  "relationships": {
    "contact_groups": {
      "data": [
        {
          "type": "contactGroups",
          "id": "c88e3a19-f814-49d9-a458-40c2103f0003"
        }
      ]
    }
  }
}
@oudesab
Copy link

oudesab commented Apr 2, 2021

Hi, try it:

render jsonapi: Contact.all, include: [:contactGroups]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants