Use UUID as entity ID

Created on 16 September 2022, almost 2 years ago
Updated 2 July 2024, about 16 hours ago

Problem/Motivation

On an "API-first" site, most/all of our client side interactions with entities is over JSON:API. Entities exposed over JSON:API require (I think?!) a UUID field. We also take steps to hide serialized entity IDs, where Drupal is apt to send them, to prevent enumeration and make the API less "Drupally."

AFAIK there is no strict requirement that content entities must implement a serial integer ID; they simply need an ID because, well, that's what is used around Drupal's core entity API to load and, uh, identity entities.

I posed the question in Drupal Slack, what if we just used the UUID for both? (This is for a custom content entity, obviously.)

This seems to work in theory, however DefaultTableMapping.php assumes these entity keys are unique field definitions, so if they refer to the same field, you get a deeply merged combination of duplicate values, which screws up the initialization of the entity tables.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs review

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated 1 minute 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.

  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany
  • Status changed to Needs review 2 days ago
  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    After talking to @rnsrk abou this in Burgas, spent some more time on this.

    Fixed the issue mentioned in #9. Also added some inline comments about the changes and finally added some test coverage. I pushed those to individual commits in case there are any objections to any of those.

    Note that I thought a bit about how to avoid all this array madness (i.e. array_values(array_unique(array_filter(...))) and friends) and I think the solution would be to make DefaultTableMapping provide an addFields() method, which would internally check for duplicate fields. Will open a follow-up about that, so we can avoid that discussion here (even though it does make the situation (inherently) slightly worse).

    Not exactly why this was tagged "Needs change record" or what the change record should be. I mean we could literally write a change notice with "Content entities may now use the same field for the ID and UUID keys" if people think that's useful. (I don't, in particular, but it also doesn't hurt, so I wouldn't mind it.)

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

    Not sure why the testbot doesn't run for the updated commits, but I opened πŸ“Œ Replace DefaultTableMapping::setFieldNames() with ::add*() and ::remove*() Active in the meantime.

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

    ...ahh maybe because the MR is targeting 10.1.x. @bradjones1 do you have access to change the target branch? Or should we create a new MR? Not sure...

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

    MR target changed.

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

    Needed a rebase and now GitLab CI is running.

  • Pipeline finished with Success
    about 18 hours ago
    Total: 550s
    #213340
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Added a basic CR. Tests green now.

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

    Awesome, thanks. You dropped my commits from the MR, though, I'm guessing that was accidental? πŸ€”

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

    That is so strange, I think something weird happened with changing the merge target. I thought it was strange I didn't need to do a rebase --onto... and clearly that would be necessary. I've fixed this. Thanks for catching it.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life
  • Pipeline finished with Failed
    about 16 hours ago
    Total: 2219s
    #213373
  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    Great, thanks! πŸ‘

  • Pipeline finished with Success
    about 16 hours ago
    Total: 673s
    #213393
Production build 0.69.0 2024