Skip to content

Throw SerializationException for null values in dense collections#1261

Merged
sugmanue merged 1 commit into
smithy-lang:mainfrom
sugmanue:sugmanue/fix-codgen-dense-behavior
Jun 23, 2026
Merged

Throw SerializationException for null values in dense collections#1261
sugmanue merged 1 commit into
smithy-lang:mainfrom
sugmanue:sugmanue/fix-codgen-dense-behavior

Conversation

@sugmanue

Copy link
Copy Markdown
Contributor

Both codegen and dynamic schema deserialization paths now reject null values in non-sparse lists and maps with a SerializationException instead of silently skipping or corrupting data.

What behavior changes?

  • Deserialization (codegen path): Previously, null values in dense lists/maps were silently skipped. Now throws SerializationException.
  • Deserialization (dynamic schema path): Previously, null values in dense lists were coerced to the string "null", and null in dense maps flowed through unchecked, causing NPE later during serialization. Now throws SerializationException.
  • Sparse collections: Behavior unchanged, null values are preserved as expected.

Why is this change needed?

A customer reported an NPE in the MCP server path when a response contained null values in a dense map. The null was silently accepted during deserialization, then caused a NullPointerException inside ContentDocument.serializeContents when the result was later accessed via getMember. The fix fails fast at deserialization time with a clear error message.

How was this validated?

  • Dynamic schema tests (SchemaGuidedDocumentBuilderTest): 7 new tests covering dense throw, sparse preserve, validation on setMemberValue, and the customer's nested map NPE scenario.
  • Codegen golden file test (sparse-vs-dense-collections): Verifies generated SharedSerde emits the correct throw/preserve logic for dense vs sparse collections.
  • Codegen integration test (DenseSparseNullTest): 4 tests exercising generated builders at runtime with null values in dense/sparse lists and maps.
  • XML codec tests updated: StringList in the test model marked @sparse since those tests exercise null-in-list behavior.

What should reviewers focus on?

  • SchemaGuidedDocumentBuilder.java; ListConsumer and MapConsumer null handling (the core fix for dynamic schemas).
  • ListGenerator.java / MapGenerator.java; template changes for codegen (the ${^sparse}throw...${/sparse} pattern).
  • PluginTestRunner.java; removed discoverModels() to make golden file tests deterministic.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@sugmanue sugmanue enabled auto-merge (squash) June 23, 2026 17:35
Both codegen and dynamic schema deserialization paths now reject null
values in non-sparse lists and maps with a SerializationException
instead of silently skipping or corrupting data.
@adwsingh adwsingh force-pushed the sugmanue/fix-codgen-dense-behavior branch from 0dfe0b9 to e60c2be Compare June 23, 2026 17:39
@sugmanue sugmanue merged commit 7e519b9 into smithy-lang:main Jun 23, 2026
3 checks passed
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.

2 participants