Skip to content

Commit b1b9371

Browse files
committed
Fix XML union serialization for empty struct members
When building tests, discovered that serializeUnion was using incorrect variable name for empty struct members. The pattern match correctly used '_inner' but serializeMember was called with 'inner', causing compilation errors. Fixed by passing the correct variable name based on whether the struct is empty.
1 parent f133b0e commit b1b9371

File tree

4 files changed

+377
-337
lines changed

4 files changed

+377
-337
lines changed

codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQueryTest.kt

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,53 +40,53 @@ class AwsQueryTest {
4040
}
4141
""".asSmithyModel()
4242

43+
private val modelWithEmptyStruct =
44+
"""
45+
namespace test
46+
use aws.protocols#awsQuery
47+
48+
@awsQuery
49+
@xmlNamespace(uri: "https://example.com/")
50+
service TestService {
51+
version: "2019-12-16",
52+
operations: [TestOp]
53+
}
54+
55+
operation TestOp {
56+
input: TestInput,
57+
output: TestOutput
58+
}
59+
60+
structure TestInput {
61+
testUnion: TestUnion
62+
}
63+
64+
structure TestOutput {
65+
testUnion: TestUnion
66+
}
67+
68+
union TestUnion {
69+
// Empty struct - should generate _inner to avoid unused variable warning
70+
emptyStruct: EmptyStruct,
71+
72+
// Normal struct - should generate inner (without underscore)
73+
normalStruct: NormalStruct
74+
}
75+
76+
structure EmptyStruct {}
77+
78+
structure NormalStruct {
79+
value: String
80+
}
81+
""".asSmithyModel()
82+
4383
@Test
4484
fun `generate an aws query service that compiles`() {
4585
clientIntegrationTest(model) { _, _ -> }
4686
}
4787

4888
@Test
4989
fun `union with empty struct generates warning-free code`() {
50-
val modelWithEmptyStruct =
51-
"""
52-
namespace test
53-
use aws.protocols#awsQuery
54-
55-
@awsQuery
56-
@xmlNamespace(uri: "https://example.com/")
57-
service TestService {
58-
version: "2019-12-16",
59-
operations: [TestOp]
60-
}
61-
62-
operation TestOp {
63-
input: TestInput,
64-
output: TestOutput
65-
}
66-
67-
structure TestInput {
68-
testUnion: TestUnion
69-
}
70-
71-
structure TestOutput {
72-
testUnion: TestUnion
73-
}
74-
75-
union TestUnion {
76-
// Empty struct - should generate _inner to avoid unused variable warning
77-
emptyStruct: EmptyStruct,
78-
79-
// Normal struct - should generate inner (without underscore)
80-
normalStruct: NormalStruct
81-
}
82-
83-
structure EmptyStruct {}
84-
85-
structure NormalStruct {
86-
value: String
87-
}
88-
""".asSmithyModel()
89-
9090
// This test will fail with unused variable warnings if the fix is not applied
9191
// clientIntegrationTest enforces -D warnings via codegenIntegrationTest
9292
clientIntegrationTest(modelWithEmptyStruct) { _, _ -> }

codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJsonTest.kt

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
1111

1212
internal class RestJsonTest {
1313
val model =
14-
"""
14+
"""
1515
namespace test
1616
use aws.protocols#restJson1
1717
use aws.api#service
@@ -41,55 +41,55 @@ internal class RestJsonTest {
4141
}
4242
""".asSmithyModel()
4343

44+
private val modelWithEmptyStruct =
45+
"""
46+
namespace test
47+
use aws.protocols#restJson1
48+
use aws.api#service
49+
50+
@service(sdkId: "Rest Json Empty Struct")
51+
@restJson1
52+
service RestJsonEmptyStruct {
53+
version: "2019-12-16",
54+
operations: [TestOp]
55+
}
56+
57+
@http(uri: "/test", method: "POST")
58+
operation TestOp {
59+
input: TestInput,
60+
output: TestOutput
61+
}
62+
63+
structure TestInput {
64+
testUnion: TestUnion
65+
}
66+
67+
structure TestOutput {
68+
testUnion: TestUnion
69+
}
70+
71+
union TestUnion {
72+
// Empty struct - RestJson ALWAYS uses inner variable, no warning
73+
emptyStruct: EmptyStruct,
74+
75+
// Normal struct - RestJson uses inner variable
76+
normalStruct: NormalStruct
77+
}
78+
79+
structure EmptyStruct {}
80+
81+
structure NormalStruct {
82+
value: String
83+
}
84+
""".asSmithyModel()
85+
4486
@Test
4587
fun `generate a rest json service that compiles`() {
4688
clientIntegrationTest(model) { _, _ -> }
4789
}
4890

4991
@Test
5092
fun `union with empty struct always uses inner variable`() {
51-
val modelWithEmptyStruct =
52-
"""
53-
namespace test
54-
use aws.protocols#restJson1
55-
use aws.api#service
56-
57-
@service(sdkId: "Rest Json Empty Struct")
58-
@restJson1
59-
service RestJsonEmptyStruct {
60-
version: "2019-12-16",
61-
operations: [TestOp]
62-
}
63-
64-
@http(uri: "/test", method: "POST")
65-
operation TestOp {
66-
input: TestInput,
67-
output: TestOutput
68-
}
69-
70-
structure TestInput {
71-
testUnion: TestUnion
72-
}
73-
74-
structure TestOutput {
75-
testUnion: TestUnion
76-
}
77-
78-
union TestUnion {
79-
// Empty struct - RestJson ALWAYS uses inner variable, no warning
80-
emptyStruct: EmptyStruct,
81-
82-
// Normal struct - RestJson uses inner variable
83-
normalStruct: NormalStruct
84-
}
85-
86-
structure EmptyStruct {}
87-
88-
structure NormalStruct {
89-
value: String
90-
}
91-
""".asSmithyModel()
92-
9393
// This test documents that RestJson protocol is immune to unused variable issues.
9494
// Unlike RestXml/AwsQuery, RestJson serializers always reference the inner variable
9595
// even for empty structs, so no underscore prefix is needed.

codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXmlTest.kt

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,55 +44,55 @@ internal class RestXmlTest {
4444
}
4545
""".asSmithyModel()
4646

47+
private val modelWithEmptyStruct =
48+
"""
49+
namespace test
50+
use aws.protocols#restXml
51+
use aws.api#service
52+
53+
@service(sdkId: "Rest XML Empty Struct")
54+
@restXml
55+
service RestXmlEmptyStruct {
56+
version: "2019-12-16",
57+
operations: [TestOp]
58+
}
59+
60+
@http(uri: "/test", method: "POST")
61+
operation TestOp {
62+
input: TestInput,
63+
output: TestOutput
64+
}
65+
66+
structure TestInput {
67+
testUnion: TestUnion
68+
}
69+
70+
structure TestOutput {
71+
testUnion: TestUnion
72+
}
73+
74+
union TestUnion {
75+
// Empty struct - should generate _inner to avoid unused variable warning
76+
emptyStruct: EmptyStruct,
77+
78+
// Normal struct - should generate inner (without underscore)
79+
normalStruct: NormalStruct
80+
}
81+
82+
structure EmptyStruct {}
83+
84+
structure NormalStruct {
85+
value: String
86+
}
87+
""".asSmithyModel()
88+
4789
@Test
4890
fun `generate a rest xml service that compiles`() {
4991
clientIntegrationTest(model) { _, _ -> }
5092
}
5193

5294
@Test
5395
fun `union with empty struct generates warning-free code`() {
54-
val modelWithEmptyStruct =
55-
"""
56-
namespace test
57-
use aws.protocols#restXml
58-
use aws.api#service
59-
60-
@service(sdkId: "Rest XML Empty Struct")
61-
@restXml
62-
service RestXmlEmptyStruct {
63-
version: "2019-12-16",
64-
operations: [TestOp]
65-
}
66-
67-
@http(uri: "/test", method: "POST")
68-
operation TestOp {
69-
input: TestInput,
70-
output: TestOutput
71-
}
72-
73-
structure TestInput {
74-
testUnion: TestUnion
75-
}
76-
77-
structure TestOutput {
78-
testUnion: TestUnion
79-
}
80-
81-
union TestUnion {
82-
// Empty struct - should generate _inner to avoid unused variable warning
83-
emptyStruct: EmptyStruct,
84-
85-
// Normal struct - should generate inner (without underscore)
86-
normalStruct: NormalStruct
87-
}
88-
89-
structure EmptyStruct {}
90-
91-
structure NormalStruct {
92-
value: String
93-
}
94-
""".asSmithyModel()
95-
9696
// This test will fail with unused variable warnings if the fix is not applied
9797
// clientIntegrationTest enforces -D warnings via codegenIntegrationTest
9898
clientIntegrationTest(modelWithEmptyStruct) { _, _ -> }

0 commit comments

Comments
 (0)