Skip to content

Commit a0be9e4

Browse files
authored
fix: unions with member names matching auto-imported Kotlin symbols (#1242)
1 parent ad4d2a3 commit a0be9e4

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
154154
val fullyQualifiedValueType = "${reference.fullName}$valueSuffix"
155155
return createSymbolBuilder(shape, "List<$valueType>")
156156
.addReferences(reference)
157-
.putProperty(SymbolProperty.FULLY_QUALIFIED_NAME_HINT, "List<$fullyQualifiedValueType>")
157+
.putProperty(SymbolProperty.FULLY_QUALIFIED_NAME_HINT, "kotlin.collections.List<$fullyQualifiedValueType>")
158158
.putProperty(SymbolProperty.MUTABLE_COLLECTION_FUNCTION, "mutableListOf<$valueType>")
159159
.putProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION, "listOf<$valueType>")
160160
.build()
@@ -173,7 +173,7 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
173173
return createSymbolBuilder(shape, "Map<$keyType, $valueType>")
174174
.addReferences(keyReference)
175175
.addReferences(valueReference)
176-
.putProperty(SymbolProperty.FULLY_QUALIFIED_NAME_HINT, "Map<$fullyQualifiedKeyType, $fullyQualifiedValueType>")
176+
.putProperty(SymbolProperty.FULLY_QUALIFIED_NAME_HINT, "kotlin.collections.Map<$fullyQualifiedKeyType, $fullyQualifiedValueType>")
177177
.putProperty(SymbolProperty.MUTABLE_COLLECTION_FUNCTION, "mutableMapOf<$keyType, $valueType>")
178178
.putProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION, "mapOf<$keyType, $valueType>")
179179
.putProperty(SymbolProperty.ENTRY_EXPRESSION, "Map.Entry<$keyType, $valueType>")

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
@@ -481,8 +481,8 @@ class SymbolProviderTest {
481481
assertEquals("Record", sparseListSymbol.references[0].symbol.name)
482482

483483
// check the fully qualified name hint is set
484-
assertEquals("List<foo.bar.model.Record>", listSymbol.fullNameHint)
485-
assertEquals("List<foo.bar.model.Record?>", sparseListSymbol.fullNameHint)
484+
assertEquals("kotlin.collections.List<foo.bar.model.Record>", listSymbol.fullNameHint)
485+
assertEquals("kotlin.collections.List<foo.bar.model.Record?>", sparseListSymbol.fullNameHint)
486486
}
487487

488488
@Test
@@ -544,8 +544,8 @@ class SymbolProviderTest {
544544
assertTrue("com.test.model.Record" in sparseRefNames)
545545

546546
// check the fully qualified name hint is set
547-
assertEquals("Map<kotlin.String, com.test.model.Record>", mapSymbol.fullNameHint)
548-
assertEquals("Map<kotlin.String, com.test.model.Record?>", sparseMapSymbol.fullNameHint)
547+
assertEquals("kotlin.collections.Map<kotlin.String, com.test.model.Record>", mapSymbol.fullNameHint)
548+
assertEquals("kotlin.collections.Map<kotlin.String, com.test.model.Record?>", sparseMapSymbol.fullNameHint)
549549
}
550550

551551
@Test

tests/compile/src/test/kotlin/software/amazon/smithy/kotlin/codegen/SmithySdkTest.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,68 @@ class SmithySdkTest {
263263

264264
assertEquals(KotlinCompilation.ExitCode.OK, compilationResult.exitCode, compileOutputStream.toString())
265265
}
266+
267+
// https://github.com/smithy-lang/smithy-kotlin/issues/1127
268+
@Test
269+
fun `it compiles models with union member names that match their types`() {
270+
val model = """
271+
namespace aws.sdk.kotlin.test
272+
273+
use aws.protocols#awsJson1_0
274+
use smithy.rules#operationContextParams
275+
use smithy.rules#endpointRuleSet
276+
use aws.api#service
277+
278+
@awsJson1_0
279+
@service(sdkId: "UnionOperationTest")
280+
service TestService {
281+
operations: [DeleteObjects],
282+
version: "1"
283+
}
284+
285+
operation DeleteObjects {
286+
input: DeleteObjectsRequest
287+
}
288+
289+
structure DeleteObjectsRequest {
290+
Delete: Foo
291+
}
292+
293+
union Foo {
294+
list: BarList
295+
map: IntegerMap
296+
instant: Timestamp
297+
byteArray: Blob
298+
boolean: Boolean
299+
string: String
300+
bigInteger: BigInteger
301+
bigDecimal: BigDecimal
302+
double: Double
303+
float: Float
304+
long: Long
305+
short: Short
306+
int: Integer
307+
byte: Byte
308+
}
309+
310+
list BarList {
311+
member: Bar
312+
}
313+
314+
map IntegerMap {
315+
key: String
316+
value: Integer
317+
}
318+
319+
string Bar
320+
""".asSmithy()
321+
322+
val compileOutputStream = ByteArrayOutputStream()
323+
val compilationResult = compileSdkAndTest(model = model, outputSink = compileOutputStream, emitSourcesToTmp = Debug.emitSourcesToTemp)
324+
compileOutputStream.flush()
325+
326+
assertEquals(KotlinCompilation.ExitCode.OK, compilationResult.exitCode, compileOutputStream.toString())
327+
}
266328
}
267329

268330
/**

0 commit comments

Comments
 (0)