Skip to content

Fix RestJson serialization of sparse null map values #3682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

AlexDaines
Copy link
Contributor

Description

Modified the JSON marshaller generation for sparse maps to properly handle null values in accordance with the REST JSON protocol specification.

Motivation and Context

Fixes RestJsonSerializesSparseNullMapValues protocol test. The marshaller was not correctly handling null values in sparse maps, causing an InvalidOperationException when attempting to serialize null values.

Testing

Dry-run succeeded @ Feb 27, 2025, 17:59:48.041 (UTC-05:00)

All tests pass, including RestJsonSerializesSparseNullMapValues. The fix ensures proper serialization of null values for:

  • Nullable boolean values (bool?)
  • Nullable number values (int?)
  • String values
  • Struct values

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 updated the documentation accordingly
  • 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

@dscpinheiro
Copy link
Contributor

I'm not of the reviewers, but I just merged 3 PRs to v4-development. I recommend doing another dry-run to make sure your changes still pass.

@AlexDaines AlexDaines force-pushed the adaines/fix-RestJsonSerializesSparseNullMapValues-protocol-test-v2 branch from d571f0b to b40cb80 Compare February 28, 2025 21:58
@@ -160,8 +165,76 @@ namespace <#= this.Config.Namespace #>.Model.Internal.MarshallTransformat
<#=new string(' ', level * 4)#> context.Writer.WritePropertyName(<#=flatVariableName#>Kvp.Key);
<#=new string(' ', level * 4)#> var <#=flatVariableName#>Value = <#=flatVariableName#>Kvp.Value;

<#+
ProcessStructure(level + 1, flatVariableName + "Value", structure.ValueShape);
<#+ // Check for null values - only null checks for sparse maps as defined in customizations
Copy link
Contributor

Choose a reason for hiding this comment

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

@boblodgett I could be convinced either way. Should we also include the logic for sparse lists even though we don't have a test for this? There are tests right now that we are skipping for example: https://github.com/aws/aws-sdk-net/blob/main/generator/ProtocolTestsGenerator/smithy-dotnet-codegen/src/main/java/software/amazon/smithy/dotnet/codegen/customizations/ProtocolTestCustomizations.java#L36
which target SparseStringList etc. Maybe just backlog it so we are aware

Copy link
Contributor

Choose a reason for hiding this comment

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

I am ok with backlog item for it. Nothing uses it right now anyway and C2J doesn't support sparse.

@dscpinheiro dscpinheiro added the v4 label Mar 3, 2025
@AlexDaines AlexDaines force-pushed the adaines/fix-RestJsonSerializesSparseNullMapValues-protocol-test-v2 branch 2 times, most recently from 3627331 to da300d5 Compare March 6, 2025 19:50
Copy link
Contributor

@peterrsongg peterrsongg left a comment

Choose a reason for hiding this comment

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

Looks good, but we have to remember not to overwrite the additions you made in the normal.json file somehow b/c in the next copy/paste we do from the artifact it will get overwritten. Can we add an additional file like:

rest-json-protocol-partial.json

which has just your additions in it?

Comment on lines +103 to +104
"RestJsonNullAndEmptyHeaders",
"NullAndEmptyHeaders"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did we add back in two more skips in here? All of the tests should be removed from VNextTests before complete or moved to the main skip section with information why it was skipped.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was assuming this was because he needs to rebase?

if (this.Structure != null)
{
var memberWithMap = this.Structure.Members.FirstOrDefault(m => m.PropertyName == variableName.Split('.').Last());
isNullableMap = (memberWithMap != null && memberWithMap.UseNullable);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not blocking but another slightly more compact way to do this is: isNullableMap = memberWithMap?.UseNullable == true;

@@ -389,6 +389,17 @@
"output":{"shape":"JsonMapsInputOutput"},
"documentation":"<p>The example tests basic map serialization.</p>"
},
"SparseJsonMaps":{
Copy link
Contributor

Choose a reason for hiding this comment

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

These files come from a repo and can't be changed. If you did this to get the models generated that is fine but you will have to copy the generated code to the partial class and then undo these *.json files.

@@ -1,76 +0,0 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

These custom partials should not be deleted. They are for some of the tests.

@@ -1,56 +0,0 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

This file should not be deleted. It is a partial.

@@ -2095,6 +2095,40 @@ internal virtual SimpleScalarPropertiesResponse SimpleScalarProperties(SimpleSca

#endregion

#region SparseJsonMaps

internal virtual SparseJsonMapsResponse SparseJsonMaps(SparseJsonMapsRequest request)
Copy link
Contributor

Choose a reason for hiding this comment

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

These are only being generated because of the .json files that shouldn't be modified. Remove the JSON files, run the generator again and these will be removed. That will cause you to have to keep the partial files that were deleted.

@AlexDaines AlexDaines force-pushed the adaines/fix-RestJsonSerializesSparseNullMapValues-protocol-test-v2 branch from da300d5 to adce3aa Compare March 10, 2025 14:57
@AlexDaines AlexDaines closed this Mar 20, 2025
@dscpinheiro dscpinheiro deleted the adaines/fix-RestJsonSerializesSparseNullMapValues-protocol-test-v2 branch March 21, 2025 11:22
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.

4 participants