JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Expose JSONAPI::ResourceSerializer#attribute_hash ?

Aperta
#437 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Type: Enhancement
Lingua principale
Ruby
Stelle
2.3k
Fork
546
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo JSONAPI::ResourceSerializer e il suo punto di ingresso privato attribute_hash, quindi confronta il modo in cui gli attributi e le relazioni di SongResource vengono attualmente serializzati. Conferma che un modello incorporato può essere serializzato come attributi annidati, con i campi identifier e site richiesti e senza esporre Embed come risorsa separata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
ruby
Ambito
api, backend
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.