Strict Item ID comparison always fails

Created on 27 November 2022, over 1 year ago
Updated 18 October 2023, 8 months ago

Problem/Motivation

ObjectID comparisons with strict equality always fail.

Steps to reproduce

  • $q = \Drupal::service('queue.mongodb')->get('somequeue');
  • $id = $q->createItem("whatever");
  • gettype($id) returns "object"
  • get_class($id) returns "MongoDB\BSON\ObjectId"
  • $item = $q->claimItem(); : result is a Drupal\mongodb_storage\Queue\Item
  • $id == $item->item_id is TRUE
  • $id === $item->item_id is FALSE (string vs object)
  • $id == $item->id() is TRUE
  • $id === $item->id() is FALSE : both are instances of MongoDB\BSON\ObjectId, and their string representation is the same, but spl_object_id($id) != spl_object_id($item->id())

Proposed resolution

A coupe of possibilities exist:

  1. Workaround: use loose equal, not strict - not very satisfying
  2. Compare using $id->__toString() or "$id" - supports strict comparison, but not obvious to new users
  3. Add an ID equality comparison method on the Item class - heavy
  4. Make createItem return a string, using __toString or quoting on the MongoDB result - support strict comparison, easy

Remaining tasks

Decide on the strategy to use and implement it.

User interface changes

None.

API changes

The return type of createItem (not documented) and Item::id()(phpdoc only) will change from ObjectID to string.

Data model changes

None. The stored _id remains a JS ObjectId within MongoDB itself.

Credits

Issue identified by @cmlara

📌 Task
Status

Fixed

Version

2.0

Component

Queue

Created by

🇫🇷France fgm Paris, France

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

Comments & Activities

Not all content is available!

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

Production build 0.69.0 2024