Skip to content

Serialize Instant fields as ISO-8601 strings (#49715)#49720

Open
pure-zero wants to merge 1 commit into
Azure:mainfrom
pure-zero:main
Open

Serialize Instant fields as ISO-8601 strings (#49715)#49720
pure-zero wants to merge 1 commit into
Azure:mainfrom
pure-zero:main

Conversation

@pure-zero

Copy link
Copy Markdown

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:

  • [] The pull request does not introduce [breaking changes] - This is behaviour changing but non breaking for existing data, happy to discuss this.
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • [x ] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Copilot AI review requested due to automatic review settings July 3, 2026 11:19
@pure-zero pure-zero requested review from a team and kirankumarkolli as code owners July 3, 2026 11:19
@github-actions github-actions Bot added Community Contribution Community members are working on the issue Cosmos customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Jul 3, 2026
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Thank you for your contribution @pure-zero! We will review the pull request and get back to you soon.

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

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_TIMESTAMPS on the default Cosmos ObjectMapper created by Utils.createAndInitializeObjectMapper(...).
  • Add unit tests validating the ObjectMapper configuration and Instant ISO-8601 serialization/round-trip behavior through both Utils and the default CosmosItemSerializer.
  • Document the behavior change in the azure-cosmos changelog (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.

Comment thread sdk/cosmos/azure-cosmos/CHANGELOG.md Outdated
@pure-zero

Copy link
Copy Markdown
Author

@pure-zero please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

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

Labels

Community Contribution Community members are working on the issue Cosmos customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants