codeconsole commented on PR #16297:
URL: https://github.com/apache/grails-core/pull/16297#issuecomment-5529605712
A concrete example of the association-reference bug from a real application
running `defaultStoredAs: objectid` on 8.0.x today, in case it helps show why
this is worth fixing alongside the default change.
An `Activity` with `static belongsTo = [issue: Issue]`, where `Issue`
declares `String id`:
```js
activity._id = ObjectId("69d441c2ba1f8010688e2695")
activity.issue = "69d441c2ba1f8010688e2694" // BSON String
issue._id = ObjectId("69d441c2ba1f8010688e2694") // BSON ObjectId
```
The reference and its target are different BSON types, so nothing outside
GORM can join them:
```js
db.issue.find({_id: activity.issue}) // null
db.issue.find({_id: ObjectId(activity.issue)}) // found
db.activity.aggregate([
{ $lookup: { from: "issue", localField: "issue", foreignField: "_id", as:
"iss" } }
]) // iss: [] for every row
```
GORM traversal is unaffected — `activity.issue.title` works, because the
decoder coerces on read. Only `$lookup`, the raw driver and external clients
see the mismatch, which is why this has gone unnoticed.
`ToOneEncoder` wrote the reference using the declared id type rather than
the target's stored type. With the encoder change here, the reference is
written as an `ObjectId` and both queries above match.
--
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]