Skip to content

Write $merge's keepExisting as the server spells it - #4591

Open
evanchooly wants to merge 1 commit into
masterfrom
fix-merge-keep-existing
Open

evanchooly wants to merge 1 commit into
masterfrom
fix-merge-keep-existing

Conversation

@evanchooly

@evanchooly evanchooly commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Problem

MergeCodec writes whenMatched and whenNotMatched as the driver enum's name, lower-cased. That works for the one-word values (replace, merge, fail, insert, discard). But WhenMatched.KEEP_EXISTING becomes "keep_existing", and the server rejects the stage:

merge("target").on("_id").whenMatched(KEEP_EXISTING)
→ BadValue: Enumeration value 'keep_existing' for field 'whenMatched' is not a valid value.

So $merge with whenMatched: "keepExisting" can't be expressed in Morphia at all. The bug is on 2.5.x as well.

I found it while binding $merge for the operator-audit case generator (#4592), which converts MQL values like keepExisting to driver constants.

Fix

The codec writes the camel-case MQL value: KEEP_EXISTING → keepExisting. One-word values are unchanged. It lower-cases with Locale.ROOT, so the output no longer depends on the default locale.

TestMerge#keepExisting runs the stage against the server and checks that the matched document is kept and the unmatched one inserted. Without the fix it fails with the BadValue above. TestMerge passes under Critter and reflection.

For review

  • Backports: 2.5.x (and 2.4.x) have the same codec. Should this be backported?
  • WhenMatched.PIPELINE still writes "pipeline", which isn't a server value. A pipeline is set with whenMatched(Stage...), which writes the stages instead, so I left it alone.

Self-reviewed only.

MergeCodec lower-cased the driver's enum names, so WhenMatched.KEEP_EXISTING
became "keep_existing", which the server rejects. Write the camel-case MQL
value instead.
@evanchooly

Copy link
Copy Markdown
Member Author

Backports: #4593 (2.5.x), #4594 (2.4.x).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant