Serialize Instant fields as ISO-8601 strings (#49715)#49720
Open
pure-zero wants to merge 1 commit into
Open
Conversation
Contributor
|
Thank you for your contribution @pure-zero! We will review the pull request and get back to you soon. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes java.time (e.g., Instant, LocalDateTime) JSON serialization in the Cosmos DB Java SDK’s default item serializer by ensuring the shared Jackson ObjectMapper writes ISO-8601 strings instead of numeric epoch timestamps.
Changes:
- Disable
SerializationFeature.WRITE_DATES_AS_TIMESTAMPSon the default CosmosObjectMappercreated byUtils.createAndInitializeObjectMapper(...). - Add unit tests validating the ObjectMapper configuration and
InstantISO-8601 serialization/round-trip behavior through bothUtilsand the defaultCosmosItemSerializer. - Document the behavior change in the
azure-cosmoschangelog (as a breaking change).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Utils.java | Configures the shared Jackson ObjectMapper to serialize Java time types as ISO-8601 strings. |
| sdk/cosmos/azure-cosmos/CHANGELOG.md | Adds a breaking-change entry describing the new default wire format for java.time fields. |
| sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/UtilsTest.java | Adds unit coverage ensuring WRITE_DATES_AS_TIMESTAMPS is disabled and Instant serializes as an ISO-8601 JSON string. |
| sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosItemSerializerUnitTests.java | Adds unit coverage verifying the default serializer writes Instant as ISO-8601 and can round-trip it. |
Author
@microsoft-github-policy-service agree |
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.
Description
Fixes java.time.Instant fields (and other java.time types handled by Jackson's JavaTimeModule, e.g. LocalDateTime) serializing as a numeric
epoch timestamp instead of an ISO-8601 string when written through the default CosmosItemSerializer (#49715)
Root cause: Utils.createAndInitializeObjectMapper() (com.azure.cosmos.implementation.Utils) registers Jackson's JavaTimeModule but never
disables SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, which defaults to true on a plain ObjectMapper. Since this is the single factory
backing Utils.getSimpleObjectMapper() / Utils.getDocumentObjectMapper(...) — which CosmosItemSerializer.DEFAULT_SERIALIZER uses for every item
read/write on both CosmosContainer and CosmosAsyncContainer — every Instant field was silently written as a number (e.g.
1690019200.123456789) instead of a quoted ISO-8601 string (e.g. "2023-07-22T09:46:40.123456789Z").
Fix: explicitly configure SerializationFeature.WRITE_DATES_AS_TIMESTAMPS = false alongside the JavaTimeModule registration.
Compatibility: this only changes the write format going forward. Jackson's InstantDeserializer recognizes a numeric JSON token as epoch time
independent of this flag, so existing items already stored with epoch-numeric date fields continue to deserialize correctly into Instant — no
data migration needed. It's flagged as a breaking change because any downstream consumer (non-Java SDKs, raw JSON tooling, dashboards) that
was relying on the old numeric wire format will now see a string instead.
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines