Skip to content

Commit b4580e3

Browse files
authored
fix: fix modeled/implied default values for byte and short types (#876)
1 parent b2643e0 commit b4580e3

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "282d596e-a121-40e3-80d3-2f2a90aa4e5d",
3+
"type": "bugfix",
4+
"description": "Fix modeled/implied default values for byte and short types"
5+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,21 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
6868

6969
override fun toMemberName(shape: MemberShape): String = escaper.escapeMemberName(shape.defaultName())
7070

71-
override fun byteShape(shape: ByteShape): Symbol = numberShape(shape, "Byte")
71+
override fun byteShape(shape: ByteShape): Symbol = numberShape(shape, "Byte", "0.toByte()")
7272

73-
override fun integerShape(shape: IntegerShape): Symbol = numberShape(shape, "Int")
73+
override fun integerShape(shape: IntegerShape): Symbol = numberShape(shape, "Int", "0")
7474

7575
override fun intEnumShape(shape: IntEnumShape): Symbol = createEnumSymbol(shape)
7676

77-
override fun shortShape(shape: ShortShape): Symbol = numberShape(shape, "Short")
77+
override fun shortShape(shape: ShortShape): Symbol = numberShape(shape, "Short", "0.toShort()")
7878

7979
override fun longShape(shape: LongShape): Symbol = numberShape(shape, "Long", "0L")
8080

8181
override fun floatShape(shape: FloatShape): Symbol = numberShape(shape, "Float", "0f")
8282

8383
override fun doubleShape(shape: DoubleShape): Symbol = numberShape(shape, "Double", "0.0")
8484

85-
private fun numberShape(shape: Shape, typeName: String, defaultValue: String = "0"): Symbol =
85+
private fun numberShape(shape: Shape, typeName: String, defaultValue: String): Symbol =
8686
createSymbolBuilder(shape, typeName, namespace = "kotlin").defaultValue(defaultValue).build()
8787

8888
override fun bigIntegerShape(shape: BigIntegerShape?): Symbol = createBigSymbol(shape, "BigInteger")
@@ -281,6 +281,8 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
281281
is LongShape -> "${value}L"
282282
is FloatShape -> "${value}f"
283283
is DoubleShape -> if (value.matches("[0-9]*\\.[0-9]+".toRegex())) value else "$value.0"
284+
is ShortShape -> "$value.toShort()"
285+
is ByteShape -> "$value.toByte()"
284286
else -> value
285287
}
286288

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/core/SymbolProviderTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class SymbolProviderTest {
6363
"Integer, null, true",
6464
"PrimitiveInteger, 0, false",
6565
"Short, null, true",
66-
"PrimitiveShort, 0, false",
66+
"PrimitiveShort, 0.toShort(), false",
6767
"Long, null, true",
6868
"PrimitiveLong, 0L, false",
6969
"Byte, null, true",
70-
"PrimitiveByte, 0, false",
70+
"PrimitiveByte, 0.toByte(), false",
7171
"Float, null, true",
7272
"PrimitiveFloat, 0f, false",
7373
"Double, null, true",
@@ -176,10 +176,10 @@ class SymbolProviderTest {
176176
@CsvSource(
177177
"long,100,100L",
178178
"integer,5,5",
179-
"short,32767,32767",
179+
"short,32767,32767.toShort()",
180180
"float,3.14159,3.14159f",
181181
"double,2.71828,2.71828",
182-
"byte,10,10",
182+
"byte,10,10.toByte()",
183183
"string,\"hello\",\"hello\"",
184184
"blob,\"abcdefg\",\"abcdefg\"",
185185
"boolean,true,true",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class SerializeStructGeneratorTest {
4747
@ParameterizedTest(name = "{index} ==> ''{0}''")
4848
@CsvSource(
4949
"PrimitiveInteger, 0, 0",
50-
"PrimitiveShort, 0, 0",
50+
"PrimitiveShort, 0.toShort(), 0",
5151
"PrimitiveLong, 0L, 0",
52-
"PrimitiveByte, 0, 0",
52+
"PrimitiveByte, 0.toByte(), 0",
5353
"PrimitiveFloat, 0f, 0",
5454
"PrimitiveDouble, 0.0, 0",
5555
"PrimitiveBoolean, false, false",

0 commit comments

Comments
 (0)