codeconsole commented on code in PR #15583:
URL: https://github.com/apache/grails-core/pull/15583#discussion_r3164543163


##########
grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoCodecSession.groovy:
##########
@@ -286,6 +286,44 @@ class MongoCodecSession extends AbstractMongoSession {
         return entityWrites
     }
 
+    /**
+     * If the entity's id mapping declares {@code storedAs} and it differs 
from the in-memory
+     * native key type, coerce the key so that update/delete filters target 
BSON values of
+     * the correct type (otherwise {@code {_id: "<hex>"}} sent as a BSON 
String would never
+     * match an {@code _id: ObjectId(...)} document on disk, and the write 
would silently miss,
+     * surfacing as a misleading {@link OptimisticLockingException}).
+     *
+     * <p>Exercised end-to-end by {@code StringIdWithObjectIdStorageSpec}:
+     * <ul>
+     *   <li>"with storedAs ObjectId, updates persist (no phantom 
OptimisticLockingException)" — happy path on update filter</li>
+     *   <li>"with storedAs ObjectId, update of a non-hex id document lands on 
the right row" — null-return fallback on update filter</li>
+     *   <li>"with storedAs ObjectId, delete of a non-hex id document removes 
the row" — null-return fallback on delete filter</li>
+     *   <li>"with storedAs ObjectId, legacy documents written directly as 
BSON ObjectId are fully accessible" — update path against legacy BSON ObjectId 
_id</li>
+     * </ul>
+     */
+    protected Object coerceIdToStoredType(Object nativeKey, PersistentEntity 
entity) {

Review Comment:
   Fixed: added MongoIdCoercion (Java, in 
org.grails.datastore.mapping.mongo.engine) with resolveStoredAs(entity) and 
coerceIdToStoredType(key, entity). Two Groovy methods delegate; MongoQuery 
IdEquals/In call directly. Null-return fallback lives in one place.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to