Skip to content

Commit a11c511

Browse files
authored
fix: always serialize defaults (#1315)
1 parent 3f14934 commit a11c511

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "d4f09e32-8bcc-406e-96e0-2743280c13d9",
3+
"type": "bugfix",
4+
"description": "Switch to always serialize defaults in requests. Previously fields were not serialized in requests if they weren't `@required` and hadn't been changed from the default value.",
5+
"issues": [
6+
"https://github.com/awslabs/aws-sdk-kotlin/issues/1608"
7+
]
8+
}

codegen/smithy-kotlin-codegen-testutils/src/main/kotlin/software/amazon/smithy/kotlin/codegen/test/ModelTestUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fun Model.defaultSettings(
177177
sdkId: String = TestModelDefault.SDK_ID,
178178
generateDefaultBuildFiles: Boolean = false,
179179
nullabilityCheckMode: CheckMode = CheckMode.CLIENT_CAREFUL,
180-
defaultValueSerializationMode: DefaultValueSerializationMode = DefaultValueSerializationMode.WHEN_DIFFERENT,
180+
defaultValueSerializationMode: DefaultValueSerializationMode = DefaultValueSerializationMode.DEFAULT,
181181
): KotlinSettings {
182182
val serviceId = if (serviceName == null) {
183183
this.inferService()

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55

66
package software.amazon.smithy.kotlin.codegen
77

8-
import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait
9-
import software.amazon.smithy.aws.traits.protocols.AwsJson1_1Trait
10-
import software.amazon.smithy.aws.traits.protocols.AwsQueryTrait
11-
import software.amazon.smithy.aws.traits.protocols.Ec2QueryTrait
12-
import software.amazon.smithy.aws.traits.protocols.RestJson1Trait
13-
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait
8+
import software.amazon.smithy.aws.traits.protocols.*
149
import software.amazon.smithy.codegen.core.CodegenException
1510
import software.amazon.smithy.kotlin.codegen.lang.isValidPackageName
1611
import software.amazon.smithy.kotlin.codegen.utils.getOrNull
@@ -24,10 +19,10 @@ import software.amazon.smithy.model.shapes.ServiceShape
2419
import software.amazon.smithy.model.shapes.Shape
2520
import software.amazon.smithy.model.shapes.ShapeId
2621
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait
27-
import java.util.Optional
22+
import java.util.*
2823
import java.util.logging.Logger
24+
import java.util.stream.Collectors
2925
import kotlin.IllegalArgumentException
30-
import kotlin.streams.toList
3126

3227
// shapeId of service from which to generate an SDK
3328
private const val SERVICE = "service"
@@ -164,7 +159,7 @@ fun Model.inferService(): ShapeId {
164159
val services = shapes(ServiceShape::class.java)
165160
.map(Shape::getId)
166161
.sorted()
167-
.toList()
162+
.collect(Collectors.toList())
168163

169164
return when {
170165
services.isEmpty() -> {
@@ -273,10 +268,15 @@ enum class DefaultValueSerializationMode(val value: String) {
273268

274269
override fun toString(): String = value
275270
companion object {
271+
/**
272+
* The default value serialization mode, which is [ALWAYS]
273+
*/
274+
val DEFAULT = ALWAYS
275+
276276
fun fromValue(value: String): DefaultValueSerializationMode =
277-
values().find {
278-
it.value == value
279-
} ?: throw IllegalArgumentException("$value is not a valid DefaultValueSerializationMode, expected one of ${values().map { it.value }}")
277+
requireNotNull(entries.find { it.value.equals(value, ignoreCase = true) }) {
278+
"$value is not a valid DefaultValueSerializationMode, expected one of ${values().map { it.value }}"
279+
}
280280
}
281281
}
282282

@@ -291,7 +291,7 @@ enum class DefaultValueSerializationMode(val value: String) {
291291
data class ApiSettings(
292292
val visibility: Visibility = Visibility.PUBLIC,
293293
val nullabilityCheckMode: CheckMode = CheckMode.CLIENT_CAREFUL,
294-
val defaultValueSerializationMode: DefaultValueSerializationMode = DefaultValueSerializationMode.WHEN_DIFFERENT,
294+
val defaultValueSerializationMode: DefaultValueSerializationMode = DefaultValueSerializationMode.DEFAULT,
295295
val enableEndpointAuthProvider: Boolean = false,
296296
val protocolResolutionPriority: Set<ShapeId> = DEFAULT_PROTOCOL_RESOLUTION_PRIORITY,
297297
) {
@@ -315,7 +315,7 @@ data class ApiSettings(
315315
node.get()
316316
.getStringMemberOrDefault(
317317
DEFAULT_VALUE_SERIALIZATION_MODE,
318-
DefaultValueSerializationMode.WHEN_DIFFERENT.value,
318+
DefaultValueSerializationMode.DEFAULT.value,
319319
),
320320
)
321321
val enableEndpointAuthProvider = node.get().getBooleanMemberOrDefault(ENABLE_ENDPOINT_AUTH_PROVIDER, false)

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/KotlinDependency.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ data class KotlinDependency(
134134
// External third-party dependencies
135135
val KOTLIN_STDLIB = KotlinDependency(GradleConfiguration.Implementation, "kotlin", "org.jetbrains.kotlin", "kotlin-stdlib", KOTLIN_COMPILER_VERSION)
136136
val KOTLIN_TEST = KotlinDependency(GradleConfiguration.TestImplementation, "kotlin.test", "org.jetbrains.kotlin", "kotlin-test", KOTLIN_COMPILER_VERSION)
137+
val KOTLIN_TEST_IMPL = KOTLIN_TEST.copy(config = GradleConfiguration.Implementation)
137138
}
138139

139140
override fun getDependencies(): List<SymbolDependency> {

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpStringValuesMapSerializerTest.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class HttpStringValuesMapSerializerTest {
5353

5454
@Test
5555
fun `it handles primitive header shapes when different mode`() {
56-
val contents = getTestContents(defaultModel, "com.test#PrimitiveShapesOperation", HttpBinding.Location.HEADER)
56+
val settings = defaultModel.defaultSettings(defaultValueSerializationMode = DefaultValueSerializationMode.WHEN_DIFFERENT)
57+
val contents = getTestContents(defaultModel, "com.test#PrimitiveShapesOperation", HttpBinding.Location.HEADER, settings)
5758
contents.assertBalancedBracesAndParens()
5859

5960
val expectedContents = """
@@ -68,7 +69,8 @@ class HttpStringValuesMapSerializerTest {
6869

6970
@Test
7071
fun `it handles primitive query shapes when different mode`() {
71-
val contents = getTestContents(defaultModel, "com.test#PrimitiveShapesOperation", HttpBinding.Location.QUERY)
72+
val settings = defaultModel.defaultSettings(defaultValueSerializationMode = DefaultValueSerializationMode.WHEN_DIFFERENT)
73+
val contents = getTestContents(defaultModel, "com.test#PrimitiveShapesOperation", HttpBinding.Location.QUERY, settings)
7274
contents.assertBalancedBracesAndParens()
7375

7476
val expectedContents = """
@@ -129,7 +131,8 @@ class HttpStringValuesMapSerializerTest {
129131
}
130132
""".prependNamespaceAndService(operations = listOf("Foo")).toSmithyModel()
131133

132-
val contents = getTestContents(model, "com.test#Foo", HttpBinding.Location.HEADER)
134+
val settings = defaultModel.defaultSettings(defaultValueSerializationMode = DefaultValueSerializationMode.WHEN_DIFFERENT)
135+
val contents = getTestContents(model, "com.test#Foo", HttpBinding.Location.HEADER, settings)
133136
contents.assertBalancedBracesAndParens()
134137

135138
val expectedContents = """

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/SerializeStructGeneratorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class SerializeStructGeneratorTest {
7272
}
7373
""".trimIndent()
7474

75-
val actual = codegenSerializerForShape(model, "com.test#Foo")
75+
val settings = model.defaultSettings(defaultValueSerializationMode = DefaultValueSerializationMode.WHEN_DIFFERENT)
76+
val actual = codegenSerializerForShape(model, "com.test#Foo", settings = settings)
7677
actual.shouldContainOnlyOnceWithDiff(expected)
7778
}
7879

0 commit comments

Comments
 (0)