@@ -29,6 +29,7 @@ import software.amazon.smithy.model.shapes.StructureShape
2929import software.amazon.smithy.model.shapes.TimestampShape
3030import software.amazon.smithy.model.shapes.UnionShape
3131import software.amazon.smithy.model.traits.DefaultTrait
32+ import software.amazon.smithy.model.traits.EnumValueTrait
3233import software.amazon.smithy.model.traits.SparseTrait
3334import software.amazon.smithy.model.traits.TimestampFormatTrait
3435import software.amazon.smithy.model.traits.XmlFlattenedTrait
@@ -38,6 +39,7 @@ import software.amazon.smithy.swift.codegen.integration.serde.json.TimestampUtil
3839import software.amazon.smithy.swift.codegen.integration.serde.readwrite.NodeInfoUtils
3940import software.amazon.smithy.swift.codegen.integration.serde.readwrite.ReadingClosureUtils
4041import software.amazon.smithy.swift.codegen.integration.serde.readwrite.responseWireProtocol
42+ import software.amazon.smithy.swift.codegen.model.expectTrait
4143import software.amazon.smithy.swift.codegen.model.getTrait
4244import software.amazon.smithy.swift.codegen.model.hasTrait
4345import 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