Skip to content

JSON-API includes #1164

Closed
Closed
@xn

Description

@xn

How do I configure or where does the logic live to select which serializer serializes an include in the json-api adapter?

I've got a situation like:

module Blah
  module V1
    module Serializers
      class FooSerializer < AMS
        attribute: id
        has_many :bars, serializer: Blah::V1::Serializers::BarSerializer
      end
    end
  end
end

What I want is:

{
  "data": {
    "id": "1",
    "type": "foos",
    "attributes": {
    },
    "relationships": {
      "bars": {
        "data": [
          {
            "id": "1",
            "type": "bars"
          }
        ]
      }
    }
  },
  "included": [
    {
      "id": "1",
      "type": "bars",
      "attributes": {
      }
    }
  ]
}

What I get is (note the integer ID in the includes):

{
  "data": {
    "id": "1",
    "type": "foos",
    "attributes": {
    },
    "relationships": {
      "bars": {
        "data": [
          {
            "id": "1",
            "type": "bars"
          }
        ]
      }
    }
  },
  "included": [
    {
      "id": 1,
      "type": "bars",
      "attributes": {
      }
    }
  ]
}

When I create a BarSerializer without the namespacing, I get what I want as far as the output goes, but I still want my namespacing.

Similarly, when I have a setup like this (without the serializer option):

module Blah
  module V1
    module Serializers
      class FooSerializer < AMS
        attribute: id
        has_many :bars
      end
    end
  end
end

I get this (note the integer id in the relationships):

{
  "data": {
    "id": "1",
    "type": "foos",
    "attributes": {
    },
    "relationships": {
      "bars": {
        "data": [
          {
            "id": 1,
            "type": "bars"
          }
        ]
      }
    }
  },
  "included": [
    {
      "id": 1,
      "type": "bars",
      "attributes": {
      }
    }
  ]
}

With luck, I can simply config this. Otherwise, I've got to hack it and then come up with a PR later in the next few weeks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions