JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Expose JSONAPI::ResourceSerializer#attribute_hash ?

Open
#437 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Type: Enhancement
Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

Thanks for all the work on this gem. I've been using it for a new project and I really enjoy it.

For a project I'm currently working on, I have a Song model that has_one :embed. I don't want to expose the Embed model as part of the REST API. I always want to include the attributes of the embed in the response.

The JSON API format document explicitly states that says that attributes "may contain any valid JSON value" and that "complex data structures involving JSON objects and arrays are allowed as attribute values."

I propose that models specified as attributes in a resource should be serialized simply, using #attribute_hash.

For example, this is what my JSON looks like if I simply add :embed as an attribute to my SongResource

"embed": {
  "id": 1,
  "identifier": "1101",
  "site": "youtube",
  "art_content_id": 1,
  "art_content_type": "Song",
  "created_at": "2015-09-04T19:55:41.596Z",
  "updated_at": "2015-09-04T19:55:41.596Z"
}

If it was passed through the serializer, this is what I would get. (this is what I want)

"embed": {
  "identifier": "1101",
  "site": "youtube"
}

Right now, I can do this by manually with code like this in my SongResource

def embed
  resource = JSONAPI::Resource.resource_for @model.embed.class.to_s.underscore
  serializer = JSONAPI::ResourceSerializer.new resource
  serializer.send :attribute_hash, resource.new(@model.embed)
end

If you would be willing to consider a feature like this, I would be happy to put together a pull request for it

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading JSONAPI::ResourceSerializer and its private attribute_hash entry point, then compare how SongResource attributes and relationships are currently serialized. Confirm that an embedded model can be serialized as nested attributes, with the requested identifier and site fields and without exposing Embed as a separate resource.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.