Skip to content

Commit

Permalink
Removed regardlessOfModel option from IgnoreKeys trait
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez committed Sep 27, 2023
1 parent 3c7febc commit febc8da
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,11 @@ object RuntimeTypes {
val asSdkSerializable = symbol("asSdkSerializable")
val field = symbol("field")

val IgnoreKey = symbol("IgnoreKey")

object SerdeJson : RuntimeTypePackage(KotlinDependency.SERDE_JSON) {
val JsonSerialName = symbol("JsonSerialName")
val JsonSerializer = symbol("JsonSerializer")
val JsonDeserializer = symbol("JsonDeserializer")
val IgnoreKey = symbol("IgnoreKey")
}

object SerdeXml : RuntimeTypePackage(KotlinDependency.SERDE_XML) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ package aws.smithy.kotlin.runtime.serde

import aws.smithy.kotlin.runtime.InternalApi

/**
* This tag interface provides a mechanism to attach type-specific metadata to any field.
* See [aws.smithy.kotlin.runtime.serde.xml.XmlList] for an example implementation.
*
* For example, to specify that a list should be serialized in XML such that values are wrapped
* in a tag called "boo", pass an instance of XmlList to the FieldDescriptor of `XmlList(elementName="boo")`.
*/
@InternalApi
public interface FieldTrait

/**
* Denotes that a Map or List may contain null values
* Details at https://awslabs.github.io/smithy/1.0/spec/core/type-refinement-traits.html#sparse-trait
*/
@InternalApi
public object SparseValues : FieldTrait

/**
* A protocol-agnostic type description of a field.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,7 @@ private class JsonFieldIterator(
val propertyName = token.value
val field = descriptor.fields.find { it.serialName == propertyName }

if (descriptor.traits.contains(IgnoreKey(propertyName, false))) {
if (field == null) { // not in the model
reader.skipNext()
return findNextFieldIndex()
} else {
field.index
}
} else if (descriptor.traits.contains(IgnoreKey(propertyName, true))) {
if (descriptor.traits.contains(IgnoreKey(propertyName))) {
reader.skipNext() // the value of the ignored key
return findNextFieldIndex()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ public data class JsonSerialName(public val name: String) : FieldTrait
@InternalApi
public val SdkFieldDescriptor.serialName: String
get() = expectTrait<JsonSerialName>().name

/**
* Indicates to deserializers to ignore field/key
*/
@InternalApi
public data class IgnoreKey(public val key: String) : FieldTrait
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package aws.smithy.kotlin.runtime.serde.json

import aws.smithy.kotlin.runtime.serde.*
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

Expand Down Expand Up @@ -166,80 +169,4 @@ class JsonDeserializerIgnoresKeysTest {
assertEquals(2, y)
assertEquals(null, z)
}

// Now testing `regardlessOfInModel = false` option
class IgnoresKeysConsideringModelTest {
companion object {
val X_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("x"))
val Y_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("y"))
val Z_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("z"))
val OBJ_DESCRIPTOR = SdkObjectDescriptor.build {
trait(IgnoreKey("x", false)) // <----
field(X_DESCRIPTOR)
field(Y_DESCRIPTOR)
field(Z_DESCRIPTOR)
}
}
}

@Test
fun itDoesNotIgnoreKeyBecauseItWasInTheModel() {
val payload = """
{
"x": 0
}
""".trimIndent().encodeToByteArray()

val deserializer = JsonDeserializer(payload)
var unionValue: Int? = null
deserializer.deserializeStruct(IgnoresKeysConsideringModelTest.OBJ_DESCRIPTOR) {
loop@ while (true) {
when (findNextFieldIndex()) {
IgnoresKeysConsideringModelTest.X_DESCRIPTOR.index -> unionValue = deserializeInt()
IgnoresKeysConsideringModelTest.Y_DESCRIPTOR.index -> unionValue = deserializeInt()
IgnoresKeysConsideringModelTest.Z_DESCRIPTOR.index -> unionValue = deserializeInt()
null -> break@loop
else -> unionValue = deserializeInt()
}
}
}

assertEquals(0, unionValue)
}

class IgnoresKeysBecauseNotInModelTest {
companion object {
val Y_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("y"))
val Z_DESCRIPTOR = SdkFieldDescriptor(SerialKind.Integer, JsonSerialName("z"))
val OBJ_DESCRIPTOR = SdkObjectDescriptor.build {
trait(IgnoreKey("x", false)) // <----
field(Y_DESCRIPTOR)
field(Z_DESCRIPTOR)
}
}
}

@Test
fun itIgnoresKeyBecauseWasNotInTheModel() {
val payload = """
{
"x": 0
}
""".trimIndent().encodeToByteArray()

val deserializer = JsonDeserializer(payload)
var unionValue: Int? = null
deserializer.deserializeStruct(IgnoresKeysBecauseNotInModelTest.OBJ_DESCRIPTOR) {
loop@ while (true) {
when (findNextFieldIndex()) {
IgnoresKeysBecauseNotInModelTest.Y_DESCRIPTOR.index -> unionValue = deserializeInt()
IgnoresKeysBecauseNotInModelTest.Z_DESCRIPTOR.index -> unionValue = deserializeInt()
null -> break@loop
else -> unionValue = deserializeInt()
}
}
}

assertEquals(null, unionValue)
}
}

0 comments on commit febc8da

Please sign in to comment.