Skip to content

Commit 0859533

Browse files
committed
Fix union serialization tests based on reviewer feedback
1 parent eba7b22 commit 0859533

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGeneratorTest.kt

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class JsonSerializerGeneratorTest {
3939
use aws.protocols#restJson1
4040
4141
union TestUnion {
42-
unitMember: Unit,
43-
dataMember: String
42+
unitMember: Unit
4443
}
4544
45+
// Empty unit struct that doesn't need serialization parameters
4646
structure Unit {}
4747
4848
@http(uri: "/test", method: "POST")
@@ -370,19 +370,13 @@ class JsonSerializerGeneratorTest {
370370
@Test
371371
fun `union with unit struct doesn't cause unused variable warning`() {
372372
// Regression test for https://github.com/smithy-lang/smithy-rs/issues/4308
373-
//
374-
// PROBLEM BEFORE FIX: JSON serializer generated `UnitMember(_inner)` but tried to use `inner`
375-
// This caused compilation errors: "cannot find value `inner` in this scope"
376-
//
377-
// SOLUTION AFTER FIX: Generate `UnitMember(inner)` so variable name matches usage
378-
// This test verifies the FIXED behavior compiles and works correctly
373+
// This test ensures that union serialization with unit structs compiles without unused variable warnings.
379374
val model = RecursiveShapeBoxer().transform(OperationNormalizer.transform(unionWithUnitStructModel))
380375

381376
val codegenContext = testCodegenContext(model)
382377
val symbolProvider = codegenContext.symbolProvider
383378
val project = TestWorkspace.testProject(symbolProvider)
384379

385-
// Generate the JSON serializer with our FIX applied
386380
val jsonSerializer =
387381
JsonSerializerGenerator(
388382
codegenContext,
@@ -399,31 +393,26 @@ class JsonSerializerGeneratorTest {
399393
UnionGenerator(model, symbolProvider, this, model.lookup("test#TestUnion")).render()
400394
}
401395

402-
// Test that demonstrates our fix works - generates correct variable names
403396
project.lib {
404397
unitTest(
405-
"json_union_serialization_fix_works",
398+
"test_union_serialization",
406399
"""
407400
use test_model::{TestUnion, Unit};
408401
409-
// Generate the FIXED serialization function (uses 'inner' consistently)
410402
${format(operationGenerator!!)};
411403
412-
// Test the union serialization with unit struct - this works with our fix
413-
// BEFORE: would fail with "cannot find value `inner`" compilation error
414-
// AFTER: compiles and works because variable names match
415-
let test_union = TestUnion::UnitMember(Unit {});
416-
let mut buffer = String::new();
417-
let mut object_writer = ::aws_smithy_json::serialize::JsonObjectWriter::new(&mut buffer);
418-
crate::protocol_serde::shape_test_union::ser_test_union(&mut object_writer, &test_union).unwrap();
419-
420-
// Verify the serialization produces expected JSON
421-
assert!(buffer.contains("unitMember"));
404+
let input = crate::test_input::TestOpInput::builder()
405+
.union(TestUnion::UnitMember(Unit {}))
406+
.build()
407+
.unwrap();
408+
let serialized = ${format(operationGenerator)}(&input).unwrap();
409+
let output = std::str::from_utf8(serialized.bytes().unwrap()).unwrap();
410+
assert!(output.contains("unitMember"));
422411
""",
423412
)
424413
}
425414

426-
// The test passes if the FIXED version compiles and works correctly
415+
// The test passes if the generated code compiles without unused variable warnings
427416
project.compileAndTest()
428417
}
429418
}

codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/QuerySerializerGeneratorTest.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ class QuerySerializerGeneratorTest {
2525
@Test
2626
fun `union with unit struct doesn't cause unused variable warning`() {
2727
// Regression test for https://github.com/smithy-lang/smithy-rs/issues/4308
28-
//
29-
// This test demonstrates the BEFORE/AFTER behavior for Query serialization
28+
// This test ensures that Query serialization with unit structs compiles without unused variable warnings.
29+
// Query serialization for unit structs generates empty blocks where the variable would be unused,
30+
// so the fix uses '_inner' (with underscore prefix) to suppress the unused variable warning.
3031
val model =
3132
RecursiveShapeBoxer().transform(
3233
OperationNormalizer.transform(
3334
"""
3435
namespace test
3536
3637
union TestUnion {
37-
unitMember: Unit,
38-
dataMember: String
38+
unitMember: Unit
3939
}
4040
4141
structure Unit {}
@@ -55,7 +55,6 @@ class QuerySerializerGeneratorTest {
5555
val codegenContext = testCodegenContext(model)
5656
val symbolProvider = codegenContext.symbolProvider
5757

58-
// Generate the Query serializer with our FIX applied
5958
val parserSerializer = AwsQuerySerializerGenerator(codegenContext)
6059
val operationGenerator = parserSerializer.operationInputSerializer(model.lookup("test#TestOp"))
6160

@@ -69,29 +68,27 @@ class QuerySerializerGeneratorTest {
6968
UnionGenerator(model, symbolProvider, this, model.lookup("test#TestUnion")).render()
7069
}
7170

72-
// Test that demonstrates our fix works - generates correct variable names for unit structs
7371
project.lib {
7472
unitTest(
7573
"query_union_serialization_fix_works",
7674
"""
7775
use test_model::{TestUnion, Unit};
7876
79-
// Generate the FIXED serialization function (uses '_inner' for unit structs)
8077
${format(operationGenerator!!)};
8178
82-
// Test the union serialization with unit struct - this works with our fix
83-
// BEFORE: would generate `UnitMember(inner)` but never use `inner` -> unused variable warning
84-
// AFTER: generates `UnitMember(_inner)` for unit structs -> no warning
85-
let test_union = TestUnion::UnitMember(Unit {});
86-
let mut output = String::new();
87-
let writer = ::aws_smithy_query::QueryValueWriter::new(&mut output, "test".into());
88-
crate::protocol_serde::shape_test_union::ser_test_union(writer, &test_union).unwrap();
89-
90-
// The test passes if the code compiles without unused variable warnings
79+
let input = crate::test_input::TestOpInput::builder()
80+
.union(TestUnion::UnitMember(Unit {}))
81+
.build()
82+
.unwrap();
83+
let serialized = ${format(operationGenerator)}(&input).unwrap();
84+
let output = std::str::from_utf8(serialized.bytes().unwrap()).unwrap();
85+
// Query serialization for unit structs produces empty output
86+
assert!(output.is_empty() || !output.contains("error"));
9187
""",
9288
)
9389
}
9490

91+
// The test passes if the generated code compiles without unused variable warnings
9592
project.compileAndTest()
9693
}
9794
}

0 commit comments

Comments
 (0)