JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Expose JSONAPI::ResourceSerializer#attribute_hash ?

Abierto
#437 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Type: Enhancement
Lenguaje dominante
Ruby
Estrellas
2.3k
Forks
546
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza leyendo JSONAPI::ResourceSerializer y su punto de entrada privado attribute_hash; después, compara cómo se serializan actualmente los atributos y las relaciones de SongResource. Confirma que un modelo incrustado puede serializarse como atributos anidados, con los campos identifier y site solicitados y sin exponer Embed como un recurso separado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
ruby
Área
api, backend
Tipo de issue
Nueva funcionalidad
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.