From 307f0ed11267c02854a69a58ce7cc19751c6131f Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Thu, 19 Oct 2023 10:15:25 -0400 Subject: [PATCH] Upgrade Smithy to 1.40 and ignore __type in unions (#3081) ## Motivation and Context > JSON object. A union is serialized identically as a ``structure`` shape, but only a single member can be set to a non-null value. Deserializers MUST ignore an unrecognized ``__type`` member if present. [source](https://github.com/smithy-lang/smithy/blob/main/docs/source-2.0/aws/protocols/aws-json.rst.template#L133-L135) ## Description - upgrade to smithy 1.40 - unignore protocol tests - fix JSON deserializers ## Testing - protocol tests + extra unit tests ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --- codegen-client-test/model/rest-xml-extras.smithy | 3 --- .../generators/protocol/ProtocolTestGenerator.kt | 3 --- .../smithy/protocols/parse/JsonParserGenerator.kt | 7 ++++++- .../smithy/protocols/parse/JsonParserGeneratorTest.kt | 11 +++++++++++ gradle.properties | 2 +- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/codegen-client-test/model/rest-xml-extras.smithy b/codegen-client-test/model/rest-xml-extras.smithy index 76a6bbd9fa..b518ad09c6 100644 --- a/codegen-client-test/model/rest-xml-extras.smithy +++ b/codegen-client-test/model/rest-xml-extras.smithy @@ -74,9 +74,6 @@ structure PrimitiveIntDocument { defaultedValue: PrimitiveInt } -@enum([{"value": "enumvalue", "name": "V"}]) -string StringEnum - integer PrimitiveInt structure AttributePartyInputOutput { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt index 9b256bbfce..a188d3c948 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt @@ -543,9 +543,6 @@ class DefaultProtocolTestGenerator( "SDKAppliedContentEncoding_ec2Query", "SDKAppliedContentEncoding_restJson1", "SDKAppliedContentEncoding_restXml", - "AwsJson11DeserializeIgnoreType", - "AwsJson10DeserializeIgnoreType", - "RestJsonDeserializeIgnoreType", ) } } diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt index 0e3f531971..6b0a7b507b 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt @@ -551,13 +551,18 @@ class JsonParserGenerator( objectKeyLoop(hasMembers = shape.members().isNotEmpty()) { rustTemplate( """ + let key = key.to_unescaped()?; + if key == "__type" { + #{skip_value}(tokens)?; + continue + } if variant.is_some() { return Err(#{Error}::custom("encountered mixed variants in union")); } """, *codegenScope, ) - withBlock("variant = match key.to_unescaped()?.as_ref() {", "};") { + withBlock("variant = match key.as_ref() {", "};") { for (member in shape.members()) { val variantName = symbolProvider.toMemberName(member) rustBlock("${jsonName(member).dq()} =>") { diff --git a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt index 108207473f..e5ca4d19ae 100644 --- a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt +++ b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt @@ -170,6 +170,17 @@ class JsonParserGeneratorTest { """, ) + unitTest( + "dunder_type_should_be_ignored", + """ + // __type field should be ignored during deserialization + let input = br#"{ "top": { "choice": { "int": 5, "__type": "value-should-be-ignored-anyway" } } }"#; + let output = ${format(operationGenerator)}(input, test_output::OpOutput::builder()).unwrap().build(); + use test_model::Choice; + assert_eq!(Choice::Int(5), output.top.unwrap().choice); + """, + ) + unitTest( "empty_error", """ diff --git a/gradle.properties b/gradle.properties index 1db16fc661..33e82bc2ca 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,7 +18,7 @@ kotlin.code.style=official # codegen smithyGradlePluginVersion=0.7.0 -smithyVersion=1.39.0 +smithyVersion=1.40.0 # kotlin kotlinVersion=1.7.21