JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Expose JSONAPI::ResourceSerializer#attribute_hash ?

Offen
#437 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Type: Enhancement
Vorherrschende Sprache
Ruby
Sterne
2.3k
Forks
546
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, JSONAPI::ResourceSerializer und seinen privaten Einstiegspunkt attribute_hash zu lesen, und vergleiche anschließend, wie die Attribute und Beziehungen von SongResource derzeit serialisiert werden. Bestätige, dass ein eingebettetes Modell als verschachtelte Attribute serialisiert werden kann, mit den angeforderten identifier- und site-Feldern und ohne Embed als separate Ressource offenzulegen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
ruby
Bereich
api, backend
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.