JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Linked data with polymorphism does not work with custom `key_type`

Abierto
#1,285 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

This issue is a (choose one):

  • Problem/bug report.
  • Feature request.
  • Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.

Checklist before submitting:

  • I've searched for an existing issue.
  • I've asked my question on Gitter and have not received a satisfactory answer.
  • I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
  • The feature I'm asking for is compliant with the JSON:API spec.

Description

If you create a polymorphic association with custom key_type id is always taken which leads to a wrong data sharing.

Bug reports:

Everything is on lib/jsonapi/resource_serializer.rb:464
When calling source.public_send("#{relationship.name}_id"), it is fine (faster) but it does not take care about custom key_type.
But if you call source.public_send(relationship.name).try(:id), you're using the custom key_type.

Features:

We can set another argument to has_one to be able to mention that the key_type has been modified.
Here is my proposition:

# app/resources/my_awesome_resource
has_one :positionable, polymorphic: true, foreign_key: :positionable_id, foreign_key_type_changed: true

# jsonapi/resource_serializer.rb

  def foreign_key_value(source, relationship)
      # If you have changed the key_name, don't even try to look at `"#{relationship.name}_id"` 
      # just load the association and call the custom key_name
      foreign_key_type_changed = relationship.options[:foreign_key_type_changed] || false
      related_resource_id =
        if source.preloaded_fragments.has_key?(format_key(relationship.name))
          source.preloaded_fragments[format_key(relationship.name)].values.first.try(:id)
        elsif !foreign_key_type_changed && source.respond_to?("#{relationship.name}_id")
          # If you have direct access to the underlying id, you don't have to load the relationship
          # which can save quite a lot of time when loading a lot of data.
          # This does not apply to e.g. has_one :through relationships.
          source.public_send("#{relationship.name}_id")
        else
          source.public_send(relationship.name).try(:id)
        end
      return nil unless related_resource_id
      @id_formatter.format(related_resource_id)
    end

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 en lib/jsonapi/resource_serializer.rb:464 e inspecciona foreign_key_value, centrándote en cómo las relaciones polimórficas obtienen sus IDs cuando se configura un key_type personalizado. Reproduce el caso indicado y añade cobertura que demuestre que el ID de la relación serializada respeta el key_type personalizado en lugar de usar siempre el ID directo de la asociación.

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

Evaluación

Stack tecnológico
rails, ruby
Área
api, backend
Tipo de issue
Error
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
52/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.