Skip to content

fix: CustomItemSerializer not applied correctly in queries and SqlParameter [Cosmos]#48702

Closed
xinlian12 wants to merge 0 commit into
Azure:mainfrom
xinlian12:fix/issue-45521-custom-item-serializer
Closed

fix: CustomItemSerializer not applied correctly in queries and SqlParameter [Cosmos]#48702
xinlian12 wants to merge 0 commit into
Azure:mainfrom
xinlian12:fix/issue-45521-custom-item-serializer

Conversation

@xinlian12

@xinlian12 xinlian12 commented Apr 7, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #45521

Two bugs with CustomItemSerializer:

Bug 1 Deserialization failures in ORDER BY/GROUP BY/aggregate/DISTINCT queries

When customItemSerializer is configured on CosmosClientBuilder, complex queries (ORDER BY, GROUP BY, VALUE, COUNT, SUM, DISTINCT, HybridSearch) fail because the custom serializer leaks into the internal query pipeline and is used to deserialize internal SDK structures (OrderByRowResult, Document, etc.).

Root cause: PipelinedDocumentQueryExecutionContext.createBaseComponentFunction() called setCustomItemSerializer(null) on cloned request options, but RxDocumentClientImpl.getEffectiveItemSerializer() falls back to the client-level defaultCustomSerializer when the request-level serializer is null.

Fix: Set CosmosItemSerializer.DEFAULT_SERIALIZER instead of null on internal request options. This causes getEffectiveItemSerializer() to return DEFAULT_SERIALIZER immediately without falling through to the client-level custom serializer.

Bug 2 SqlParameter ignores customItemSerializer

SqlParameter.setValue() delegates to JsonSerializable.set() which hardcodes INTERNAL_DEFAULT_SERIALIZER. Custom serialization settings (e.g., dates as ISO strings) are never applied to query parameters.

Fix: Added rawValue field to SqlParameter to retain the original value, plus applySerializer() to re-serialize with a custom serializer. Called from PipelinedQueryExecutionContextBase.createAsync() where the effective serializer is known.

Changes

File Change
PipelinedDocumentQueryExecutionContext.java null DEFAULT_SERIALIZER in 4 internal pipeline paths
PipelinedQueryExecutionContextBase.java Apply serializer to SqlParameters at query execution time
SqlParameter.java Added rawValue field + applySerializer() method
SqlQuerySpec.java Added applySerializerToParameters()

Testing

Build compiles cleanly. Existing tests unaffected. Additional test cases for ORDER BY/GROUP BY/aggregate queries with custom serializer and SqlParameter serialization should be added.

@xinlian12 xinlian12 requested review from a team and kirankumarkolli as code owners April 7, 2026 03:03
Copilot AI review requested due to automatic review settings April 7, 2026 03:03
@xinlian12 xinlian12 requested a review from a team as a code owner April 7, 2026 03:03
@github-actions github-actions Bot added the Cosmos label Apr 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Cosmos DB query behavior when a CustomItemSerializer is configured by preventing it from deserializing internal SDK query structures, and by ensuring query parameters can be serialized with the effective item serializer.

Changes:

  • Force internal query pipeline request options to use CosmosItemSerializer.DEFAULT_SERIALIZER (instead of null) to avoid leaking client-level custom serializers into internal deserialization.
  • Add internal plumbing to re-serialize SqlParameter values using the effective serializer at query execution time.
  • Introduce internal bridge method to apply parameter re-serialization across packages.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/SqlQuerySpec.java Adds internal method to apply an effective serializer to all parameters.
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/SqlParameter.java Stores raw parameter values and adds internal re-serialization hook.
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java Exposes internal bridge method to apply serializer to query parameters.
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/PipelinedQueryExecutionContextBase.java Applies effective serializer to parameters at query execution time.
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/PipelinedDocumentQueryExecutionContext.java Forces DEFAULT serializer on internal pipeline request options to prevent custom serializer leaks.
.coding-harness/spec.json Adds harness metadata/spec for the reported issue and intended fix.

Comment thread sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/SqlParameter.java Outdated
@xinlian12 xinlian12 changed the title fix: CustomItemSerializer not applied correctly in queries and SqlParameter [Cosmos] [NO REVIEW]fix: CustomItemSerializer not applied correctly in queries and SqlParameter [Cosmos] Apr 7, 2026
@xinlian12

Copy link
Copy Markdown
Member Author

@sdkReviewAgent-2

@xinlian12

Copy link
Copy Markdown
Member Author

Review complete (54:56)

No new comments — existing review coverage is sufficient.

Steps: cross-sdk, history, past-prs, quality, sanity-check, synthesis, test-coverage

@xinlian12 xinlian12 changed the title [NO REVIEW]fix: CustomItemSerializer not applied correctly in queries and SqlParameter [Cosmos] fix: CustomItemSerializer not applied correctly in queries and SqlParameter [Cosmos] Apr 8, 2026
@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

2 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@FabianMeiswinkel FabianMeiswinkel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

2 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@xinlian12 xinlian12 force-pushed the fix/issue-45521-custom-item-serializer branch 4 times, most recently from 8838549 to f6140fa Compare April 13, 2026 16:40
@xinlian12

Copy link
Copy Markdown
Member Author

@sdkReviewAgent-2

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

2 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment thread sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/CosmosItemSerializer.java Outdated
@xinlian12

Copy link
Copy Markdown
Member Author

Review complete (45:12)

Posted 2 inline comment(s).

Steps: ✓ context, correctness, cross-sdk, design, history, past-prs, synthesis, test-coverage

@xinlian12

Copy link
Copy Markdown
Member Author

@sdkReviewAgent-2

@xinlian12

Copy link
Copy Markdown
Member Author

Review complete (45:25)

Posted 1 inline comment(s).

Steps: ✓ context, correctness, cross-sdk, design, history, past-prs, synthesis, test-coverage

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@xinlian12

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@FabianMeiswinkel FabianMeiswinkel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xinlian12

Copy link
Copy Markdown
Member Author

The failed tests are all related to thin client due to recent disablement

@xinlian12

Copy link
Copy Markdown
Member Author

/check-enforcer override

1 similar comment
@xinlian12

Copy link
Copy Markdown
Member Author

/check-enforcer override

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Cosmos – CustomItemSerializer - not working in certain queries and not applied in SqlParameter

3 participants