Fix serverOnlyFields handling for new documents in replication#387
Open
Fix serverOnlyFields handling for new documents in replication#387
Conversation
mergeServerDocumentFieldsMonad had two bugs when handling new document
inserts via replication push with serverOnlyFields configured:
1. When assumedMasterState was undefined (new insert), it was transformed
into an object (e.g. {lastName: null}), causing masterWrite to treat
inserts as updates and produce false conflicts.
2. Server-only fields were set to null on newDocumentState for new
documents, which failed schema validation for non-nullable types
like string.
Fix: return falsy clientDoc as-is, and don't modify server-only fields
when serverDoc is undefined (new document).
https://claude.ai/code/session_014Dbfva2dD4vTxEaYZfjpgb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR contains:
Describe the problem you have without this PR
When replicating new documents via the server endpoint with
serverOnlyFieldsconfigured, the conflict resolution logic incorrectly transformed falsyassumedMasterState(used for new document inserts) into an object and initialized server-only fields tonull. This caused:nullfor those fieldsChanges
Source Code Fix (
src/plugins/server/helper.ts)Modified
mergeServerDocumentFieldsMonadto:clientDocas-is without transformationnullwhenserverDocis undefined (new documents)This ensures new documents pushed via replication are not modified with server-only fields that don't exist on the client side.
Tests
endpoint-replication.test.tsverifying new documents replicate correctly from client to server withserverOnlyFieldsconfiguredserver.test.ts:null)clientDochandlingTest Plan
Added comprehensive test coverage:
should replicate a new document from client to server when serverOnlyFields are set- verifies end-to-end replication of new documents with server-only fieldsmergeServerDocumentFieldsMonadcorrectly handles new documents and falsy inputsAll existing tests pass with the corrected behavior.
https://claude.ai/code/session_014Dbfva2dD4vTxEaYZfjpgb