Skip to content

Commit eba7b22

Browse files
committed
Fix test API usage and add clear problem documentation
- Fix JsonObjectWriter and QueryValueWriter API calls - Remove unused TestInput imports - Add comments explaining what each fix addresses
1 parent 4cba820 commit eba7b22

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,19 @@ 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
373379
val model = RecursiveShapeBoxer().transform(OperationNormalizer.transform(unionWithUnitStructModel))
374380

375381
val codegenContext = testCodegenContext(model)
376382
val symbolProvider = codegenContext.symbolProvider
377383
val project = TestWorkspace.testProject(symbolProvider)
378384

379-
// Generate the JSON serializer that will create the union serialization code
385+
// Generate the JSON serializer with our FIX applied
380386
val jsonSerializer =
381387
JsonSerializerGenerator(
382388
codegenContext,
@@ -393,23 +399,31 @@ class JsonSerializerGeneratorTest {
393399
UnionGenerator(model, symbolProvider, this, model.lookup("test#TestUnion")).render()
394400
}
395401

396-
// Generate the actual protocol_serde module with union serialization
402+
// Test that demonstrates our fix works - generates correct variable names
397403
project.lib {
398404
unitTest(
399-
"json_union_serialization",
405+
"json_union_serialization_fix_works",
400406
"""
401-
use test_model::{TestInput, TestUnion, Unit};
407+
use test_model::{TestUnion, Unit};
402408
403-
// Generate the serialization function that contains union match arms with (inner)
409+
// Generate the FIXED serialization function (uses 'inner' consistently)
404410
${format(operationGenerator!!)};
405411
406-
// Test that the code compiles - this validates our fix works
407-
let _test_passed = true;
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"));
408422
""",
409423
)
410424
}
411425

412-
// The test passes if the generated code compiles without unused variable warnings
426+
// The test passes if the FIXED version compiles and works correctly
413427
project.compileAndTest()
414428
}
415429
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ 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
2830
val model =
2931
RecursiveShapeBoxer().transform(
3032
OperationNormalizer.transform(
@@ -52,6 +54,8 @@ class QuerySerializerGeneratorTest {
5254

5355
val codegenContext = testCodegenContext(model)
5456
val symbolProvider = codegenContext.symbolProvider
57+
58+
// Generate the Query serializer with our FIX applied
5559
val parserSerializer = AwsQuerySerializerGenerator(codegenContext)
5660
val operationGenerator = parserSerializer.operationInputSerializer(model.lookup("test#TestOp"))
5761

@@ -65,16 +69,24 @@ class QuerySerializerGeneratorTest {
6569
UnionGenerator(model, symbolProvider, this, model.lookup("test#TestUnion")).render()
6670
}
6771

68-
// Generate the serialization module that will contain the union serialization code
72+
// Test that demonstrates our fix works - generates correct variable names for unit structs
6973
project.lib {
7074
unitTest(
71-
"query_union_serialization",
75+
"query_union_serialization_fix_works",
7276
"""
73-
use test_model::{TestInput, TestUnion, Unit};
77+
use test_model::{TestUnion, Unit};
7478
75-
// This will generate the serialization code that should not have unused variable warnings
79+
// Generate the FIXED serialization function (uses '_inner' for unit structs)
7680
${format(operationGenerator!!)};
7781
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+
7890
// The test passes if the code compiles without unused variable warnings
7991
""",
8092
)

0 commit comments

Comments
 (0)