-
Notifications
You must be signed in to change notification settings - Fork 49
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
fix: ignore __type when deserializing union for AWS Json protocols #1054
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d60b4d
Ignoring __type when deserializing union for AWS Json protocols
0marperez 422c33e
Changelog
0marperez 8861cc6
Added documentation and modified code-gen logic
0marperez 33770ba
Fixed changelog
0marperez 4abaa7d
Fixed misspell
0marperez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "ac672b00-f8bb-4235-8db4-aad0ae2f157d", | ||
"type": "bugfix", | ||
"description": "ignore `__type` when deserializing union for AWS JSON 1.0, AWS JSON 1.1, and AWS restJson 1", | ||
"issues": [ | ||
"awslabs/aws-sdk-kotlin#1044" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...n/src/main/kotlin/aws/sdk/kotlin/codegen/protocols/json/AwsJsonProtocolParserGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package aws.sdk.kotlin.codegen.protocols.json | ||
|
||
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.toRenderingContext | ||
import software.amazon.smithy.kotlin.codegen.rendering.serde.JsonParserGenerator | ||
import software.amazon.smithy.kotlin.codegen.rendering.serde.JsonSerdeDescriptorGenerator | ||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.Shape | ||
|
||
/** | ||
* Overrides the [JsonParserGenerator] when using `AWS Json 1.0`, `AWS Json 1.1`, and `AWS RestJson 1` protocols. | ||
* | ||
* See https://github.com/smithy-lang/smithy/pull/1945 | ||
*/ | ||
class AwsJsonProtocolParserGenerator( | ||
private val protocolGenerator: ProtocolGenerator, | ||
private val supportsJsonNameTrait: Boolean = true, | ||
) : JsonParserGenerator(protocolGenerator, supportsJsonNameTrait) { | ||
|
||
override fun descriptorGenerator( | ||
ctx: ProtocolGenerator.GenerationContext, | ||
shape: Shape, | ||
members: List<MemberShape>, | ||
writer: KotlinWriter, | ||
): JsonSerdeDescriptorGenerator = AwsJsonProtocolSerdeDescriptorGenerator(ctx.toRenderingContext(protocolGenerator, shape, writer), members, supportsJsonNameTrait) | ||
} |
46 changes: 46 additions & 0 deletions
46
...n/kotlin/aws/sdk/kotlin/codegen/protocols/json/AwsJsonProtocolSerdeDescriptorGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package aws.sdk.kotlin.codegen.protocols.json | ||
|
||
import software.amazon.smithy.kotlin.codegen.core.RenderingContext | ||
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes | ||
import software.amazon.smithy.kotlin.codegen.rendering.serde.JsonSerdeDescriptorGenerator | ||
import software.amazon.smithy.kotlin.codegen.rendering.serde.SdkFieldDescriptorTrait | ||
import software.amazon.smithy.kotlin.codegen.rendering.serde.add | ||
import software.amazon.smithy.kotlin.codegen.utils.dq | ||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.Shape | ||
|
||
/** | ||
* Overrides the [JsonSerdeDescriptorGenerator] when using `AWS Json 1.0`, `AWS Json 1.1`, and `AWS RestJson 1` protocols. | ||
* | ||
* See: https://github.com/smithy-lang/smithy/pull/1945 | ||
*/ | ||
class AwsJsonProtocolSerdeDescriptorGenerator( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docs |
||
ctx: RenderingContext<Shape>, | ||
memberShapes: List<MemberShape>? = null, | ||
supportsJsonNameTrait: Boolean = true, | ||
) : JsonSerdeDescriptorGenerator(ctx, memberShapes, supportsJsonNameTrait) { | ||
|
||
/** | ||
* Adds a trait to ignore `__type` in union shapes for `AWS Json 1.0`, `AWS Json 1.1`, `RestJson 1.0` protocols | ||
* Sometimes the unnecessary field `__type` is added and needs to be ignored | ||
* | ||
* NOTE: Will be ignored unless it's in the model | ||
* | ||
* Source: https://github.com/smithy-lang/smithy/pull/1945 | ||
*/ | ||
override fun getObjectDescriptorTraits(): List<SdkFieldDescriptorTrait> { | ||
val traitList = super.getObjectDescriptorTraits().toMutableList() | ||
val typeMember = memberShapes.find { it.memberName == "__type" } | ||
|
||
if (ctx.shape?.isUnionShape == true && typeMember == null) { | ||
traitList.add(RuntimeTypes.Serde.SerdeJson.IgnoreKey, "__type".dq()) | ||
} | ||
|
||
return traitList | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...tlin/aws/sdk/kotlin/codegen/protocols/json/AwsJsonProtocolSerdeDescriptorGeneratorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package aws.sdk.kotlin.codegen.protocols.json | ||
|
||
import software.amazon.smithy.kotlin.codegen.test.* | ||
import software.amazon.smithy.model.shapes.ShapeId | ||
import kotlin.test.Test | ||
|
||
class AwsJsonProtocolSerdeDescriptorGeneratorTest { | ||
@Test | ||
fun itAddsIgnoreKeysTrait() { | ||
val model = """ | ||
@http(method: "POST", uri: "/foo") | ||
operation Foo { | ||
input: FooRequest | ||
} | ||
|
||
structure FooRequest { | ||
strVal: String, | ||
intVal: Integer | ||
} | ||
|
||
union Bar { | ||
x: String, | ||
y: String, | ||
} | ||
""".prependNamespaceAndService(operations = listOf("Foo")).toSmithyModel() | ||
|
||
val testCtx = model.newTestContext() | ||
val writer = testCtx.newWriter() | ||
val shape = model.expectShape(ShapeId.from("com.test#Bar")) | ||
val renderingCtx = testCtx.toRenderingContext(writer, shape) | ||
|
||
AwsJsonProtocolSerdeDescriptorGenerator(renderingCtx).render() | ||
|
||
val expectedDescriptors = """ | ||
val X_DESCRIPTOR = SdkFieldDescriptor(SerialKind.String, JsonSerialName("x")) | ||
val Y_DESCRIPTOR = SdkFieldDescriptor(SerialKind.String, JsonSerialName("y")) | ||
val OBJ_DESCRIPTOR = SdkObjectDescriptor.build { | ||
trait(IgnoreKey("__type")) | ||
field(X_DESCRIPTOR) | ||
field(Y_DESCRIPTOR) | ||
} | ||
""".formatForTest("") | ||
|
||
val contents = writer.toString() | ||
contents.shouldContainOnlyOnceWithDiff(expectedDescriptors) | ||
} | ||
|
||
@Test | ||
fun itDoesNotAddIgnoreKeysTrait() { | ||
val model = """ | ||
@http(method: "POST", uri: "/foo") | ||
operation Foo { | ||
input: FooRequest | ||
} | ||
|
||
structure FooRequest { | ||
strVal: String, | ||
intVal: Integer | ||
} | ||
|
||
union Bar { | ||
__type: String, | ||
y: String, | ||
} | ||
""".prependNamespaceAndService(operations = listOf("Foo")).toSmithyModel() | ||
|
||
val testCtx = model.newTestContext() | ||
val writer = testCtx.newWriter() | ||
val shape = model.expectShape(ShapeId.from("com.test#Bar")) | ||
val renderingCtx = testCtx.toRenderingContext(writer, shape) | ||
|
||
AwsJsonProtocolSerdeDescriptorGenerator(renderingCtx).render() | ||
|
||
val expectedDescriptors = """ | ||
val TYPE_DESCRIPTOR = SdkFieldDescriptor(SerialKind.String, JsonSerialName("__type")) | ||
val Y_DESCRIPTOR = SdkFieldDescriptor(SerialKind.String, JsonSerialName("y")) | ||
val OBJ_DESCRIPTOR = SdkObjectDescriptor.build { | ||
field(TYPE_DESCRIPTOR) | ||
field(Y_DESCRIPTOR) | ||
} | ||
""".formatForTest("") | ||
|
||
val contents = writer.toString() | ||
contents.shouldContainOnlyOnceWithDiff(expectedDescriptors) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix Missing documentation.
Should we rename this (and
AwsJsonProtocolSerdeDescriptorGenerator
) since these aren't specific to the "aws json" protocols? It's a bit confusing but I also don't know that I have a better suggestion at the moment. At the very least we should document that this isn't specific toawsJson
protocols.