JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Polymorphic code in serializer makes some AR assumptions

Offen
#829 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

In the following code in resource_serializer.rb


    def foreign_key_types_and_values(source, relationship)
      if relationship.is_a?(JSONAPI::Relationship::ToMany)
        if relationship.polymorphic?
          assoc = source._model.public_send(relationship.name)
          # Avoid hitting the database again for values already pre-loaded
          if assoc.respond_to?(:loaded?) and assoc.loaded?
            assoc.map do |obj|
              [obj.type.underscore.pluralize, @id_formatter.format(obj.id)]
            end
          else
            assoc.pluck(:type, :id).map do |type, id|
              [type.underscore.pluralize, @id_formatter.format(id)]
            end
          end
        else
          source.public_send(relationship.name).map do |value|
            [relationship.type, @id_formatter.format(value.id)]
          end
        end
      end
    end

It makes the assumption that if you specify a polymorphic relationship that it should get the data straight from the model rather than allowing this to be changed via the resource.
It also assumes that we are using "id" and "type" which is very much an active record thing.

In my application, I am using active record, but have a "has_many" relationship to a class which then has a has_many polymorphic. I effectively want to do a has_many through the middle class straight to the polymorphic data which I cannot find a way of doing.

If the above code allowed the resource to intervene here, I could have overriden something.

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 in resource_serializer.rb bei foreign_key_types_and_values und verfolge, wie polymorphe Beziehungen ihre Werte erhalten. Ermittle den Erweiterungspunkt auf Ressourcenebene, der benötigt wird, um die Annahmen von Active Record bezüglich id/type zu vermeiden, und überprüfe anschließend, dass eine Ressource eine polymorphe has-many-through-Beziehung bereitstellen kann, ohne dass der Serializer das Modell direkt abfragt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rails, ruby
Bereich
api, backend
Issue-Typ
Refactoring
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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