JSON:API module should support POSTing dynamic entity references with only `type`, `id`/entity reference field should validate type when setting with entity property

Created on 27 April 2022, over 2 years ago
Updated 23 May 2024, 7 months ago

Problem/Motivation

There are a few existing issues (added as related) dealing with core/json:api module's assumption that entity references are, basically, always "core" entity references which can only target a single entity type. The denormalization performed in the top-level JSON:API normalizer does what amounts to early denormalization of this data and deprives field normalizers (or Extras field enhancers) sufficient information about the reference to accommodate for other entity type targets. E.g., Dynamic Entity Reference needs to set entity_type in addition to target_id.

Interestingly, we are dangerously close to actually supporting this, in so far as the target entity type is identifiable by its public json:api name, which is how these entities actually pass initial validation and are loaded up to get their target IDs.

You can post DER data by specifying the target_type in meta, however this is unnecessary as it effectively duplicates the type member of the relationship identifier object and requires the client to know about this drupalism (and potentially expose your internals.) See #2888553: Handle denormalizing meta payload in relationships β†’ where this pattern was introduced.

Note that there is ✨ Dynamic Entity Reference normalization support Active and others which address the normalization/querying side, but AFAICT simple reporting of DER field data "works", and there may be some quirks with querying (particularly as it relates to included data and filtering by relationships) but there are no open issues for the create/update denormalization side of the coin.

Steps to reproduce

Try to post just a relationship with type and id to a DER field.

Proposed resolution

Load up what I'm referring to as the "early denormalized" value array for entity references with the entity type; this should be a no-op for the core ER field but will provide DER with what it needs (and any other field doing something similar.)

Remaining tasks

Title update
Change record review and updates, see #49

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
JSON APIΒ  β†’

Last updated 6 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

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.

  • Pipeline finished with Failed
    8 months ago
    Total: 176s
    #165393
  • Pipeline finished with Success
    8 months ago
    Total: 492s
    #165403
  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Rebased on to 11.x and all green now! This was cautiously RTBC before, maybe we can get it there again?

  • Status changed to Needs work 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Left a few comments nothing major.

  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Nits addressed, back to NR.

  • Pipeline finished with Failed
    8 months ago
    Total: 203s
    #165711
  • Pipeline finished with Success
    8 months ago
    Total: 606s
    #165730
  • Status changed to RTBC 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Seems this has gone through a number of reviews so will lean on that.

    Did run the test-only feature to show the tests are failing as expected https://git.drupalcode.org/issue/drupal-3277553/-/jobs/1527829

    Feedback from earlier has been addressed.

    Believe this one to be good.

  • Status changed to Needs work 7 months ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    @smustgrave. The definition of RTBC includes the following, "Setting an issue to this status is a judgment call: if you have thoroughly tested and reviewed the patch and believe it is ready, you may change the status." I don't think that criteria has been meet in this case because as you say you are leaning on the reviews of others.

    Let's have a title update here. The title is a problem statement and is better as a positive statement of what is being fixed. Also, I didn't find a comment that any of the 4 change records have been reviewed. That needs to be done. And all the change records need an update to the branch/version.

    I did not review the patch.

  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Updates made.

  • Status changed to Needs work 7 months ago
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Went through the changes again. I have some feedback of thinigs that have changed in core since. Also i'm afraid we need a BC path for the contructor addition. I think @larowlan just missed that.

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

    I think we are ok with no bc path for the topdocumentnornalizer.

    https://git.drupalcode.org/search?group_id=2&repository_ref=11.x&scope=b...

    No usage. Just to be sure I'll also check the service

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

    And for the service. Think we are golden, no bc path.

    https://git.drupalcode.org/search?group_id=2&repository_ref=11.x&scope=b...

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

    Extra note, also read through the cr's

  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Removed the two dead code lines in test, sounds like nothing to change on the BC side (excellent.)

  • Pipeline finished with Canceled
    7 months ago
    Total: 3124s
    #168957
  • Pipeline finished with Failed
    7 months ago
    Total: 638s
    #168993
  • Pipeline finished with Failed
    7 months ago
    Total: 690s
    #169032
  • Status changed to Needs work 7 months ago
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Might need a rebase, but that single test is failing consistently.

  • Pipeline finished with Success
    7 months ago
    Total: 604s
    #169073
  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Done, green.

  • Pipeline finished with Canceled
    7 months ago
    Total: 235s
    #169081
  • Status changed to RTBC 7 months ago
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    All feedback has been resolved. Think this is ready, ty for the work here.

  • Pipeline finished with Success
    7 months ago
    Total: 573s
    #169089
  • Status changed to Needs work 7 months ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I read the issue summary and am concentrating on the remaining tasks.
    The title has been updated, thank you. However it still long and I do not understand if the comma is separating 2 phrases or the forward slash is. This seems to be fixing multiple things? Is this what it means, "JSON:API validates entity reference fields and supports POSTing dynamic entity references with only 'type' and 'id'"

    In #44 @bralla read through the change records so I presume that means they are accurate.
    The branch/version on the CRs is correct.

    That is the three remaining tasks. I am setting back to NW for a title update.

    I then took a closer look at the change records. Since there are four of them I will ask if this issue has too wide a scope?

    This change record β†’ isn't really clear in explaining the action the developer should take. It uses 'may' so it is not clear if the change must be made or not or what the consequences are. For example, what happens if a site is using the following code in say Drupal 12? Is this a problem? Does a site using that have to change that code?

    meta": {
                "entity_type": "user"
              }

    Anyway, I worked on all the change records to convert them to plain language and to correct the casing of JSON:API per the specification. I hope that helps to keep this moving along.

Production build 0.71.5 2024