Skip to content

Add DocumentJsonConverter for System.Text.Json serialization support#4347

Open
AlexDaines wants to merge 5 commits intodevelopmentfrom
adaines/fix-document-serialization
Open

Add DocumentJsonConverter for System.Text.Json serialization support#4347
AlexDaines wants to merge 5 commits intodevelopmentfrom
adaines/fix-document-serialization

Conversation

@AlexDaines
Copy link
Contributor

@AlexDaines AlexDaines commented Mar 11, 2026

Description

Add DocumentJsonConverter to fix System.Text.Json serialization of Document types. Document implements IEnumerable interfaces which causes STJ to treat it as a collection, throwing InvalidDocumentTypeConversionException for non-list/non-dictionary types.

Also fixes Document.FromObject number parsing to try TryGetInt32 before TryGetInt64, consistent with DocumentUnmarshaller.

Motivation and Context

Fixes #3694, #3837, #4078.

Testing

  • 10 test methods covering primitives, collections, round-trip, regression, and customer scenario reproductions.
  • dotnet test --filter "FullyQualifiedName~Document" → 26 passed, 0 failed.
  • Dry run: DRY_RUN-fa16ac3c-9736-4470-8c5b-cade7550b45a
  • Status: Succeeded

Breaking Changes Assessment

No breaking changes. DocumentJsonConverter is internal sealed. The [JsonConverter] attribute changes STJ behavior from broken (throws) to correct. Customer workarounds (custom converters in JsonSerializerOptions) take precedence per STJ resolution order.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed

License

  • I confirm that this pull request can be released under the Apache 2 license

Copy link
Contributor

Copilot AI left a comment

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 System.Text.Json serialization/deserialization for Amazon.Runtime.Documents.Document by adding a dedicated STJ converter so Document is no longer treated as an IEnumerable collection during serialization, and aligns Document.FromObject numeric parsing behavior with existing runtime unmarshalling logic.

Changes:

  • Added an internal DocumentJsonConverter and applied it to Document via [JsonConverter].
  • Updated Document.FromObject to prefer TryGetInt32 before TryGetInt64.
  • Added unit tests covering primitive/collection serialization, round-trips, and regressions; added a patch DevConfig entry.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
sdk/src/Core/Amazon.Runtime/Documents/DocumentJsonConverter.cs Implements STJ converter to serialize/deserialize Document by its DocumentType rather than via IEnumerable.
sdk/src/Core/Amazon.Runtime/Documents/Document.cs Applies the converter attribute and updates FromObject number parsing to try int before long.
sdk/test/UnitTests/Custom/Runtime/Documents/DocumentJsonConverterTests.cs Adds coverage for the new converter, including regressions and customer scenarios.
generator/.DevConfigs/180bd698-bd6a-4799-a23e-db90f89660d6.json Declares a patch-level core changelog entry for the fix.

@AlexDaines AlexDaines changed the title Adaines/fix document serialization Add DocumentJsonConverter for System.Text.Json serialization support Mar 12, 2026
@AlexDaines AlexDaines marked this pull request as ready for review March 12, 2026 15:59
@boblodgett boblodgett requested review from dscpinheiro and normj March 13, 2026 17:33
Copy link
Member

@normj normj left a comment

Choose a reason for hiding this comment

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

Very cool PR. This will help make working with JSON and Documents.

[TestMethod]
[TestCategory("UnitTest")]
[TestCategory("Runtime")]
public void SerializeFilterAttributeScenario()
Copy link
Contributor

Choose a reason for hiding this comment

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

This test seems to be a duplicate of SerializeObjectContainingDocument (that also mentions #3694).

Also, I don't see any tests referencing #3837, is that because it's covered by another test?

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.

4 participants