Skip to content

Commit aa60bad

Browse files
authored
Correctly serialize Strings in XML (#1832)
The logic that keeps track of whether we're working with a borrowed or an owned value is flawed. It just so happened to work when requesting owned values from borrowed values because we called `autoDeref` before creating the value expression. That logic should instead be used _within_ `ValueExpression` to appease Clippy there too. Fixes #1831.
1 parent 7866477 commit aa60bad

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

codegen-client-test/model/rest-xml-extras.smithy

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ service RestXmlExtras {
2020
ChecksumRequired,
2121
StringHeader,
2222
CreateFoo,
23+
RequiredMember,
2324
]
2425
}
2526

@@ -242,5 +243,17 @@ structure StringHeaderOutput {
242243
field: String,
243244

244245
@httpHeader("x-enum")
245-
enumHeader: StringEnum
246+
enumHeader: StringEnum,
247+
}
248+
249+
/// This operation tests that we can serialize `required` members.
250+
@http(uri: "/required-member", method: "GET")
251+
operation RequiredMember {
252+
input: RequiredMemberInputOutput
253+
output: RequiredMemberInputOutput
254+
}
255+
256+
structure RequiredMemberInputOutput {
257+
@required
258+
requiredString: String
246259
}

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/ValueExpression.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55

66
package software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize
77

8+
import software.amazon.smithy.rust.codegen.core.rustlang.autoDeref
9+
810
sealed class ValueExpression {
911
abstract val name: String
1012

1113
data class Reference(override val name: String) : ValueExpression()
1214
data class Value(override val name: String) : ValueExpression()
1315

1416
fun asValue(): String = when (this) {
15-
is Reference -> "*$name"
17+
is Reference -> autoDeref(name)
1618
is Value -> name
1719
}
1820

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import software.amazon.smithy.model.shapes.StringShape
1919
import software.amazon.smithy.model.shapes.StructureShape
2020
import software.amazon.smithy.model.shapes.TimestampShape
2121
import software.amazon.smithy.model.shapes.UnionShape
22-
import software.amazon.smithy.model.traits.EnumTrait
2322
import software.amazon.smithy.model.traits.TimestampFormatTrait
2423
import software.amazon.smithy.model.traits.XmlFlattenedTrait
2524
import software.amazon.smithy.model.traits.XmlNamespaceTrait
@@ -285,11 +284,9 @@ class XmlBindingTraitSerializerGenerator(
285284
}
286285

287286
private fun RustWriter.serializeRawMember(member: MemberShape, input: String) {
288-
when (val shape = model.expectShape(member.target)) {
289-
is StringShape -> if (shape.hasTrait<EnumTrait>()) {
287+
when (model.expectShape(member.target)) {
288+
is StringShape -> {
290289
rust("$input.as_str()")
291-
} else {
292-
rust("$input.as_ref()")
293290
}
294291
is BooleanShape, is NumberShape -> {
295292
rust(
@@ -444,7 +441,7 @@ class XmlBindingTraitSerializerGenerator(
444441
* ```
445442
*
446443
* If [member] is not an optional shape, generate code like:
447-
* `{ .. Block }`
444+
* `{ .. BLOCK }`
448445
*
449446
* [inner] is passed a new `ctx` object to use for code generation which handles the
450447
* potentially new name of the input.
@@ -462,7 +459,12 @@ class XmlBindingTraitSerializerGenerator(
462459
}
463460
} else {
464461
with(util) {
465-
ignoreZeroValues(member, ValueExpression.Value(autoDeref(ctx.input))) {
462+
val valueExpression = if (ctx.input.startsWith("&")) {
463+
ValueExpression.Reference(ctx.input)
464+
} else {
465+
ValueExpression.Value(ctx.input)
466+
}
467+
ignoreZeroValues(member, valueExpression) {
466468
inner(ctx)
467469
}
468470
}

0 commit comments

Comments
 (0)