Skip to content

Commit 83c69b9

Browse files
authored
fix: Render enum default values correctly (#777)
1 parent 571c78d commit 83c69b9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/serde/member/MemberShapeDecodeGenerator.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import software.amazon.smithy.model.shapes.StructureShape
2929
import software.amazon.smithy.model.shapes.TimestampShape
3030
import software.amazon.smithy.model.shapes.UnionShape
3131
import software.amazon.smithy.model.traits.DefaultTrait
32+
import software.amazon.smithy.model.traits.EnumValueTrait
3233
import software.amazon.smithy.model.traits.SparseTrait
3334
import software.amazon.smithy.model.traits.TimestampFormatTrait
3435
import software.amazon.smithy.model.traits.XmlFlattenedTrait
@@ -38,6 +39,7 @@ import software.amazon.smithy.swift.codegen.integration.serde.json.TimestampUtil
3839
import software.amazon.smithy.swift.codegen.integration.serde.readwrite.NodeInfoUtils
3940
import software.amazon.smithy.swift.codegen.integration.serde.readwrite.ReadingClosureUtils
4041
import software.amazon.smithy.swift.codegen.integration.serde.readwrite.responseWireProtocol
42+
import software.amazon.smithy.swift.codegen.model.expectTrait
4143
import software.amazon.smithy.swift.codegen.model.getTrait
4244
import software.amazon.smithy.swift.codegen.model.hasTrait
4345
import software.amazon.smithy.swift.codegen.model.isError
@@ -150,7 +152,7 @@ open class MemberShapeDecodeGenerator(
150152
if (it.isNullNode) { return "" }
151153
// Provide a default value dependent on the type.
152154
return when (targetShape) {
153-
is EnumShape -> " ?? .${swiftEnumCaseName(it.expectStringNode().value, "")}"
155+
is EnumShape -> " ?? .${enumDefaultValue(targetShape, it.expectStringNode().value)}"
154156
is IntEnumShape -> intEnumDefaultValue(it)
155157
is StringShape -> " ?? \"${it.expectStringNode().value}\""
156158
is ByteShape -> " ?? ${it.expectNumberNode().value}"
@@ -172,6 +174,18 @@ open class MemberShapeDecodeGenerator(
172174
} ?: "" // If there is no default trait, provide no default value.
173175
}
174176

177+
// From the Smithy docs at https://smithy.io/2.0/spec/type-refinement-traits.html#default-value-constraints :
178+
// > The following shapes have restrictions on their default values:
179+
// >
180+
// > enum: can be set to any valid string _value_ of the enum.
181+
// So, find the member with the default value, then render it as a Swift enum case.
182+
private fun enumDefaultValue(enumShape: EnumShape, value: String): String {
183+
val matchingMember = enumShape.members().first { member ->
184+
value == member.expectTrait<EnumValueTrait>().expectStringValue()
185+
}
186+
return swiftEnumCaseName(matchingMember.memberName, value)
187+
}
188+
175189
private fun intEnumDefaultValue(node: Node): String {
176190
return when (node) {
177191
is StringNode -> " ?? .${node.value}"

0 commit comments

Comments
 (0)