kotwal-itpro opened a new pull request, #7924:
URL: https://github.com/apache/hop/pull/7924

   Fixes #7183.
   
   ## Problem
   
   Since the BSON driver 5.x upgrade in Hop 2.17, the MongoDB Output transform 
can no longer use `_id` (or any ObjectId-typed field) as an Update match field.
   
   `MongoDbOutputData.setMongoValueFromHopValue()` calls `Document.parse()` on 
the incoming JSON string when `json_field=Y`. For an ObjectId field the value 
is BSON extended JSON of the form `{"$oid": "6a15efdd0a93aa5ed3b4b947"}`. In 
BSON 4.x, `Document.parse()` accepted this at the top level; in BSON 5.x the 
parser recognises it as the OBJECT_ID BSON type and rejects it with:
   
   ```
   BsonInvalidOperationException: readStartDocument can only be called when
   CurrentBSONType is DOCUMENT, not when CurrentBSONType is OBJECT_ID.
   ```
   
   Existing workarounds are unattractive: `json_field=N` produces a `String` 
that won't match an ObjectId in MongoDB (different BSON types → updates 
silently match zero documents), and matching on non-`_id` fields sacrifices the 
built-in `_id` index.
   
   ## Fix
   
   Extract the JSON parsing in `setMongoValueFromHopValue()` into a small 
helper `parseMongoJsonValue(String)` that:
   
   - Peeks the top-level BSON type via a `JsonReader`.
   - If it's `OBJECT_ID`, returns a real `ObjectId` so the value is stored with 
the correct BSON type and the update match works against Mongo's native `_id`.
   - Otherwise falls through to `Document.parse(val)`, preserving pre-existing 
behaviour and the error surface for regular embedded documents.
   
   The helper is package-private for testing.
   
   ## Test
   
   Adds two unit tests in `MongoDbOutputDataTest`:
   
   1. `parseMongoJsonValue_topLevelObjectIdExtendedJson_returnsObjectId` — 
reproduces the exact input from the issue's stack trace and asserts an 
`ObjectId` with the expected hex is returned (rather than throwing 
`BsonInvalidOperationException`).
   2. `parseMongoJsonValue_regularDocument_returnsDocument` — asserts that 
regular embedded-JSON documents still flow through `Document.parse` unchanged.
   
   Neither test needs a live MongoDB. Ran `./mvnw spotless:apply` — no 
formatting changes required.


-- 
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