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._
This commit is contained in:
Russell Cohen 2023-10-19 10:15:25 -04:00 committed by GitHub
parent 12fa4d3963
commit 307f0ed112
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 8 deletions

View File

@ -74,9 +74,6 @@ structure PrimitiveIntDocument {
defaultedValue: PrimitiveInt
}
@enum([{"value": "enumvalue", "name": "V"}])
string StringEnum
integer PrimitiveInt
structure AttributePartyInputOutput {

View File

@ -543,9 +543,6 @@ class DefaultProtocolTestGenerator(
"SDKAppliedContentEncoding_ec2Query",
"SDKAppliedContentEncoding_restJson1",
"SDKAppliedContentEncoding_restXml",
"AwsJson11DeserializeIgnoreType",
"AwsJson10DeserializeIgnoreType",
"RestJsonDeserializeIgnoreType",
)
}
}

View File

@ -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()} =>") {

View File

@ -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",
"""

View File

@ -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