JSON:API assumes entity reference field's main property must be the entity ID

Created on 23 September 2024, 12 months ago

Problem/Motivation

The core entity reference field does not document any requirement that the main property name on the field is the same as the value of the entity ID.

However, the code in jsonapi module assumes this is true in at least a couple places. This breaks subclasses of EntityReferenceItem that use the UUID (e.g. entity_reference_uuid module) or some other unique identifier (e.g. the revision ID) as the main property.

related:
#3050845: Includes using the `entity_reference_uuid` contrib module not working β†’
πŸ› Cannot use UUID as entity ID Needs work

Steps to reproduce

Using the entity_reference_uuid module, try to include related data for the reference filed, or try to send a PATCH request to the relationship URL

Proposed resolution

Since we are already loading the entity, we can simply use/assign the "entity" property on the reference field instead of trying to guess the mapping of entity ID to property name.

Remaining tasks

port contrib patch, update core patch

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Release notes snippet

todo

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

jsonapi.module

Created by

πŸ‡ΊπŸ‡ΈUnited States pwolanin

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

Merge Requests

Comments & Activities

  • Issue created by @pwolanin
  • πŸ‡ΊπŸ‡ΈUnited States pwolanin
  • First commit to issue fork.
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Ok, moved it to an issuefork so we can get tests.

    Although this seems fine, we do need some testing around this. I guess testing the 2 changed methods might be good enough, but not entirely sure yet how to do that.

  • Pipeline finished with Success
    12 months ago
    Total: 742s
    #292087
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands
  • Pipeline finished with Success
    5 months ago
    Total: 442s
    #467611
  • First commit to issue fork.
  • Merge request !11771Resolve #3476224 "Entity only" β†’ (Closed) created by heddn
  • Status changed to Needs work 5 months ago
  • Pipeline finished with Failed
    5 months ago
    Total: 154s
    #468539
  • Pipeline finished with Failed
    5 months ago
    Total: 153s
    #468571
  • Pipeline finished with Failed
    5 months ago
    Total: 212s
    #468620
  • Pipeline finished with Success
    5 months ago
    Total: 733s
    #468627
  • Pipeline finished with Failed
    5 months ago
    #468651
  • Pipeline finished with Failed
    5 months ago
    Total: 140s
    #468719
  • Pipeline finished with Failed
    5 months ago
    Total: 727s
    #468720
  • heddn Nicaragua

    This has a failing test now on MR 11771

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Seems there has already been a review.

    Can the branches/MRs be cleaned up some for easier reviews

    Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States pwolanin

    Discussed with @tstoeckler in person who has run into similar issues using jsonapi. His feeling was the the branch where we've started adding the test is the best way to move forward - basically assuming that all entity reference fields have an "entity" computed field that we can use to associate the loaded entity

  • πŸ‡ΊπŸ‡ΈUnited States pwolanin

    pwolanin β†’ changed the visibility of the branch 3476224-jsonapi-assumes-entity to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States pwolanin

    pwolanin β†’ changed the visibility of the branch 3476224-target-id-only to hidden.

  • heddn Nicaragua

    Working on some more test coverage and path to patch the addition of a relationship.

  • heddn Nicaragua

    This adds more test coverage for adding relationships when target_id is not the main property. Interestingly, I stumbled upon the fact that JsonApiFunctionalTest does some very similar tests, but with more traditional entity reference fields. Of the 2 scenarios I've added, the first was the only one that actually didn't presently work. The second already worked but I added it so we don't have a regression in that scenario.

  • Pipeline finished with Success
    5 months ago
    Total: 485s
    #476105
  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    Yes, thanks for managing the MRs @pwolanin that makes it more manageable. Yes, I agree with the proposed resolution. The only way this would be a breaking change would be if a custom entity reference implementation either did not provide a computed entity property at all - which would (among lots of other things!) break the entity query joining across the entity reference while having absolutely no benefit - or did provide one but did not manage the sync from the entity property to its target ID property - which just seems like a straight up bug. So I don't think this should be a reason to hold this up, in particular since this is an actual contrib blocker. I can't really imagine that there are actually people impacted by this, but will write a change notice for this just in case anyway.

    And will do a proper code review afterwards.

  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    Did not intend to unassign, apologies.

  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    In starting to write the change notice and looking at some of the code I found that - in addition to the current assumption of the main property that is discussed here - JSON:API currently also already assumes a computed entity property that is specifically called 'entity'. See \Drupal\jsonapi\IncludeResolver::resolveIncludeTree(). So what I said above is not true, there is no change that this property is required - because it already is. I think that is further evidence that the proposed approach is the correct and this is just consolidating different assumptions in different places to one assumption.

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    For reference. This was the commit:
    πŸ› ResourceTypeRepository wrongly assumes that all entity reference fields have the setting "target_type" Fixed : Issue #3057545 by acbramley, hchonov, bbrala, bradjones1, larowlan, yogeshmpawar, Leon Kessler, gease, joachim, gabesullice, kfritsche, jibran, Wim Leers, Berdir, smustgrave, alexpott, catch: ResourceTypeRepository wrongly assumes that all entity reference fields have the setting "target_type"

  • πŸ‡­πŸ‡ΊHungary GΓ‘bor Hojtsy Hungary
  • heddn Nicaragua

    Thanks to the helpful input. I've addressed all feedback on the MR. I re-tested if scenario #2 still works before/after this change and that is still the case. All that wasn't working is scenario #1.

  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    Wow, what awesome turnaround, thanks a lot. Looked through the changes and it looks great to me. I did spend some more time looking at the other usages of getMainPropertyName(), but I'm not yet convinced there's actually anything actionable there, so not opening a follow-up issue for that. So there's really nothing left to do here from my point of view.

  • Pipeline finished with Failed
    5 months ago
    Total: 3799s
    #476821
  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    Hmm... strange, don't really understand why the Nightwatch tests would fail, but I guess would be good to get a green pipeline before this goes in.

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Nightwatch tests fail sometimes ramdomly, at that point, just hit retry and they will succeed most of the time.

  • Assigned to heddn
  • Status changed to Downport 3 months ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Committed 71cc5fd and pushed to 11.x. Thanks!

    As we're in the RC phase going to backport this to 11.2.x once 11.2.0 is out.

    • alexpott β†’ committed 71cc5fd8 on 11.x
      Issue #3476224 by pwolanin, heddn, bbrala, tstoeckler: JSON:API assumes...
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    Thanks everyone!

    Contributed project blockers are generally major by default. Also, even if it weren't a contrib blocker, the bug sounds major to me on its own.

    I think this is safe to backport during RC (and I would actually prefer it before 11.2.0 rather than in a patch release given the small internal change to assumptions as per the CR).

    Crediting @alexpott for the previous commit; he was uncredited somehow.

  • πŸ‡¬πŸ‡§United Kingdom catch
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    @catch also said in Slack that he thought it was RC-safe FWIW; crediting RM reviews.

    • alexpott β†’ committed 8d70081b on 11.2.x
      Issue #3476224 by pwolanin, heddn, bbrala, tstoeckler: JSON:API assumes...
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Thanks @xjm and @catch for considering this for backport. Committed 8d70081 and pushed to 11.2.x.

  • πŸ‡ΊπŸ‡ΈUnited States pwolanin

    Thanks!

    We have been using a variant on this patch for a long time in production, would love to see it bakcport to 10.x so we don't have to keep applying a core patch.

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    It's not patch-eligible, but @catch and I did agree on backporting it to 10.6 since it's a major bug and a contributed project blocker, so it will at least be addressed with the December release. (Of course the bug will also go away when the site is upgraded to Drupal 11.) πŸ˜‰

    • xjm β†’ committed 7e134766 on 10.6.x
      Revert "Issue #3476224 by pwolanin, heddn, bbrala, tstoeckler: JSON:API...
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    Looks like it's not a clean backport, unfortunately:

      Line   core/modules/jsonapi/tests/src/Functional/JsonApiRelationshipTest.php  
     ------ ----------------------------------------------------------------------- 
      52     Call to static method createBundle() on an unknown class               
             Drupal\entity_test\EntityTestHelper.                                   
             πŸ’‘ Learn more at https://phpstan.org/user-guide/discovering-symbols    
     ------ ----------------------------------------------------------------------- 
     [ERROR] Found 1 error    

    So we would need a separate MR for backport.

  • Pipeline finished with Failed
    3 months ago
    Total: 754s
    #531386
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    The merge request might need another attempt. :)

  • πŸ‡ΊπŸ‡ΈUnited States pwolanin

    It looks like that static method EntityTestHelper::createBundle() replaced a module function entity_test_create_bundle() in πŸ“Œ Move entity_test_create_bundle from entity_test.module Needs work in 11.x, so for the backport we can switch it back.

  • Pipeline finished with Failed
    2 months ago
    Total: 115s
    #547428
  • Pipeline finished with Success
    2 months ago
    #547430
  • πŸ‡ΊπŸ‡ΈUnited States pwolanin

    Fixing the backport was just that 1-line fix to use the prior version of that test code helper function

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    xjm β†’ changed the visibility of the branch 3476224-entity-only to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    xjm β†’ changed the visibility of the branch 3476224-entity-only to hidden.

    • xjm β†’ committed 4058bba3 on 10.6.x
      Issue #3476224 by pwolanin, heddn, bbrala, xjm, tstoeckler, alexpott,...
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    Committed the backport to 10.6.x. Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    xjm β†’ changed the visibility of the branch 11.x to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    xjm β†’ changed the visibility of the branch 10.6.x to hidden.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024