-
Notifications
You must be signed in to change notification settings - Fork 28
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 #964
Merged
+218
−2
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c7febc
IgnoreKey added to Json deserializer
0marperez febc8da
Removed regardlessOfModel option from IgnoreKeys trait
0marperez c2cfdbb
Simplified IgnoreKeys tests
0marperez c207a32
Style changes
0marperez 5769b44
Added test for key not in model
0marperez 5258f30
Simplified test
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
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
161 changes: 161 additions & 0 deletions
161
...-json/common/test/aws/smithy/kotlin/runtime/serde/json/JsonDeserializerIgnoresKeysTest.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,161 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package aws.smithy.kotlin.runtime.serde.json | ||
|
||
import aws.smithy.kotlin.runtime.serde.SdkFieldDescriptor | ||
import aws.smithy.kotlin.runtime.serde.SdkObjectDescriptor | ||
import aws.smithy.kotlin.runtime.serde.SerialKind | ||
import aws.smithy.kotlin.runtime.serde.deserializeStruct | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class JsonDeserializerIgnoresKeysTest { | ||
private val X_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("x")) | ||
private val Y_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("y")) | ||
private val Z_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("z")) | ||
private val OBJ_DESCRIPTOR = SdkObjectDescriptor.build { | ||
trait(IgnoreKey("z")) | ||
field(X_DESCRIPTOR) | ||
field(Y_DESCRIPTOR) | ||
field(Z_DESCRIPTOR) | ||
} | ||
|
||
@Test | ||
fun itIgnoresKeys() { | ||
val payload = """ | ||
{ | ||
"x": 1, | ||
"y": 2, | ||
"z": 3 | ||
} | ||
""".trimIndent().encodeToByteArray() | ||
|
||
val deserializer = JsonDeserializer(payload) | ||
var x: Int? = null | ||
var y: Int? = null | ||
var z: Int? = null | ||
deserializer.deserializeStruct(OBJ_DESCRIPTOR) { | ||
loop@ while (true) { | ||
when (findNextFieldIndex()) { | ||
X_DESCRIPTOR.index -> x = deserializeInt() | ||
Y_DESCRIPTOR.index -> y = deserializeInt() | ||
Z_DESCRIPTOR.index -> z = deserializeInt() | ||
null -> break@loop | ||
} | ||
} | ||
} | ||
|
||
assertEquals(1, x) | ||
assertEquals(2, y) | ||
assertEquals(null, z) | ||
} | ||
|
||
@Test | ||
fun itIgnoresKeysOutOfOrder() { | ||
val payload = """ | ||
{ | ||
"z": 3, | ||
"x": 1, | ||
"y": 2 | ||
} | ||
""".trimIndent().encodeToByteArray() | ||
|
||
val deserializer = JsonDeserializer(payload) | ||
var x: Int? = null | ||
var y: Int? = null | ||
var z: Int? = null | ||
deserializer.deserializeStruct(OBJ_DESCRIPTOR) { | ||
loop@ while (true) { | ||
when (findNextFieldIndex()) { | ||
X_DESCRIPTOR.index -> x = deserializeInt() | ||
Y_DESCRIPTOR.index -> y = deserializeInt() | ||
Z_DESCRIPTOR.index -> z = deserializeInt() | ||
null -> break@loop | ||
} | ||
} | ||
} | ||
|
||
assertEquals(1, x) | ||
assertEquals(2, y) | ||
assertEquals(null, z) | ||
} | ||
|
||
@Test | ||
fun itIgnoresKeysManyTimes() { | ||
val payload = """ | ||
{ | ||
"x": 1, | ||
"y": 2, | ||
"z": 3, | ||
"z": 3, | ||
"z": 3 | ||
} | ||
""".trimIndent().encodeToByteArray() | ||
|
||
val deserializer = JsonDeserializer(payload) | ||
var x: Int? = null | ||
var y: Int? = null | ||
var z: Int? = null | ||
deserializer.deserializeStruct(OBJ_DESCRIPTOR) { | ||
loop@ while (true) { | ||
when (findNextFieldIndex()) { | ||
X_DESCRIPTOR.index -> x = deserializeInt() | ||
Y_DESCRIPTOR.index -> y = deserializeInt() | ||
Z_DESCRIPTOR.index -> z = deserializeInt() | ||
null -> break@loop | ||
} | ||
} | ||
} | ||
|
||
assertEquals(1, x) | ||
assertEquals(2, y) | ||
assertEquals(null, z) | ||
} | ||
|
||
private val W_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("w")) | ||
private val MULT_KEYS_OBJ_DESCRIPTOR = SdkObjectDescriptor.build { | ||
trait(IgnoreKey("w")) | ||
trait(IgnoreKey("z")) | ||
field(W_DESCRIPTOR) | ||
field(X_DESCRIPTOR) | ||
field(Y_DESCRIPTOR) | ||
field(Z_DESCRIPTOR) | ||
} | ||
|
||
@Test | ||
fun itIgnoresMultipleKeys() { | ||
val payload = """ | ||
{ | ||
"w": 0, | ||
"x": 1, | ||
"y": 2, | ||
"z": 3 | ||
} | ||
""".trimIndent().encodeToByteArray() | ||
|
||
val deserializer = JsonDeserializer(payload) | ||
var w: Int? = null | ||
var x: Int? = null | ||
var y: Int? = null | ||
var z: Int? = null | ||
deserializer.deserializeStruct(MULT_KEYS_OBJ_DESCRIPTOR) { | ||
loop@ while (true) { | ||
when (findNextFieldIndex()) { | ||
W_DESCRIPTOR.index -> w = deserializeInt() | ||
X_DESCRIPTOR.index -> x = deserializeInt() | ||
Y_DESCRIPTOR.index -> y = deserializeInt() | ||
Z_DESCRIPTOR.index -> z = deserializeInt() | ||
null -> break@loop | ||
} | ||
} | ||
} | ||
|
||
assertEquals(null, w) | ||
assertEquals(1, x) | ||
assertEquals(2, y) | ||
assertEquals(null, z) | ||
} | ||
} |
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.
Suggestion: Every test has explicitly modeled field descriptors. You're missing coverage for unmodeled fields that we want to skip rather than enumerate as unknown.