Skip to content

Commit e371005

Browse files
authored
Implicit event stream payload members (#4535)
2 parents a02bab9 + 8d0b0b0 commit e371005

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ class HttpBoundProtocolPayloadGenerator(
9595
// need to take ownership.
9696
return if (payloadMemberName == null) {
9797
ProtocolPayloadGenerator.PayloadMetadata(takesOwnership = false)
98-
} else if (operationShape.isInputEventStream(model)) {
98+
} else if (
99+
when (httpMessageType) {
100+
HttpMessageType.REQUEST -> operationShape.isInputEventStream(model)
101+
HttpMessageType.RESPONSE -> operationShape.isOutputEventStream(model)
102+
}
103+
) {
99104
ProtocolPayloadGenerator.PayloadMetadata(takesOwnership = true)
100105
} else {
101106
val member = shape.expectMember(payloadMemberName)

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/EventStreamUnmarshallerGenerator.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,26 @@ class EventStreamUnmarshallerGenerator(
243243
}
244244
}
245245
}
246+
// Handle implicit payload members: members with neither @eventPayload nor @eventHeader.
247+
// Per the Smithy spec, these are collectively serialized as a protocol-specific document
248+
// in the message body.
249+
val implicitMembers =
250+
unionStruct.members().filter {
251+
!it.hasTrait<EventPayloadTrait>() && !it.hasTrait<EventHeaderTrait>()
252+
}
253+
if (implicitMembers.isNotEmpty() && payloadMember == null) {
254+
val parser = protocol.structuredDataParser().errorParser(unionStruct)
255+
if (parser != null) {
256+
rustTemplate(
257+
"""
258+
builder = #{parser}(&message.payload()[..], builder)
259+
.map_err(|err| #{Error}::unmarshalling(format!("failed to unmarshall: {err}")))?;
260+
""",
261+
"parser" to parser,
262+
*codegenScope,
263+
)
264+
}
265+
}
246266
rustTemplate(
247267
"Ok(#{UnmarshalledMessage}::Event(#{Output}::$unionMemberName(builder.build())))",
248268
"Output" to unionSymbol,

0 commit comments

Comments
 (0)