Add arity key on field data returned by jsonapi

Created on 15 April 2022, about 3 years ago
Updated 26 September 2024, 8 months ago

Problem/Motivation

#2864680: Spec Compliance: JSON API's schema disallows duplicate resource identifiers. EntityReferenceItems which reference the same entity must have an "arity" added an arity key to relationships returned by jsonapi to avoid an error when a reference field contains the same entity multiple times.

However, we have a similar problem when returning data about a specific field.
The previous fix only applies to ResourceIdentifier, but when querying a field, jsonapi returns normalized ResourceObject and those don't have an arity key.

Steps to reproduce

  1. Create an entity with a reference field and add the same entity multiple times in this reference field.
  2. Browse to /jsonapi/[entity_type]/[bundle]/[uuid]/[field_name] (real life example: /jsonapi/paragraph/carrousel/2f03a78f-1d0a-43e5-a6ee-6fb0fd8a90f7/field_images).
  3. You get this error:

    A JSON:API response failed validation (see the logs for details). Please report this in the issue queue on drupal.org

(However /jsonapi/[entity_type]/[bundle]/[uuid] does not have the same error because there, the relationship has arity keys.)

Proposed resolution

jsonapi should add an arity key on duplicated ResourceObject objects.

The arity logic from ResourceIdentifier should probably be refactorized to be used by other classes.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
JSON API 

Last updated 3 days ago

Created by

🇫🇷France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France GaëlG Lille, France

    gaëlg made their first commit to this issue’s fork.

  • 🇫🇷France GaëlG Lille, France

    gaëlg changed the visibility of the branch 3275497-add-arity-key to hidden.

  • Pipeline finished with Failed
    8 months ago
    Total: 152s
    #293110
  • Pipeline finished with Canceled
    2 months ago
    Total: 328s
    #441338
  • Pipeline finished with Failed
    2 months ago
    Total: 356s
    #441342
  • Pipeline finished with Failed
    2 months ago
    Total: 417s
    #441360
  • Status changed to Needs work 2 months ago
  • 🇫🇷France prudloff Lille

    I need to fix related tests.

  • Pipeline finished with Failed
    2 months ago
    Total: 76s
    #441435
  • Pipeline finished with Failed
    2 months ago
    Total: 729s
    #441436
  • Pipeline finished with Failed
    2 months ago
    Total: 655s
    #441450
  • Pipeline finished with Failed
    2 months ago
    Total: 563s
    #441637
  • Pipeline finished with Success
    2 months ago
    Total: 837s
    #441653
  • 🇫🇷France prudloff Lille

    The MR adds the arity key to every resource even if not necessary (for example on responses containing a single resource).
    It keeps the code simple and more predictable, but it means a lot of tests had to be updated.

  • 🇺🇸United States smustgrave

    Left comments/questions in MR.

  • Pipeline finished with Success
    2 months ago
    Total: 309s
    #442146
  • 🇫🇷France prudloff Lille

    I answered the questions.
    If this needs fixing, we should probably fix it in JsonApiResource as well?

  • 🇺🇸United States smustgrave

    All the tests having to be updated to pass makes me think we are introducing a breaking change

  • 🇳🇱Netherlands bbrala Netherlands

    Kinda depends, adding a meta to add responses does mean we are doing a change. Is it breaking? If your client support jsonapi it shouldn't be, its part of the spec.

    So i lean towards not breaking.

  • 🇺🇸United States smustgrave

    Can we add an example where it’s not passed to make sure both ways are covered?

  • 🇳🇱Netherlands bbrala Netherlands

    It's late. Is this also for creating entities? If so, then it should also work without, otherwise it is indeed a BC break since you might not do that :x

  • 🇺🇸United States smustgrave

    Not sure correct status but just thinking if we can add an example mentioned in #18

  • Status changed to Needs review 6 days ago
  • 🇫🇷France prudloff Lille

    The MR only adds the arity key to read responses, it does not change the requirements for creating/editing entities.
    I did not change the tests for these operations so if they pass it probably means it still works without an arity key.
    (However we could have a followup to allow specifying arity when editing a reference field with duplicates, in order to specify which of the references we want to change.)

    I'm not sure it is a breaking change. If your client does not use the arity key, it will continue to work as before.
    The main change is that now a response can contain the same entity multiple times.

    I agree that changing all these tests does not feel optimal. An alternative solution would be to only add the arity key when the response contains the same entity multiple times but it might make the code more complex and less predictable for a small benefit.

Production build 0.71.5 2024