Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server code generation of @httpPayload-bound constrained shapes #2584

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ message = "Fix generation of constrained shapes reaching `@sensitive` shapes"
references = ["smithy-rs#2582", "smithy-rs#2585"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "server" }
author = "david-perez"

[[smithy-rs]]
message = "Fix server code generation bug affecting constrained shapes bound with `@httpPayload`"
references = ["smithy-rs#2583", "smithy-rs#2584"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "server" }
author = "david-perez"
15 changes: 15 additions & 0 deletions codegen-core/common-test-models/constraints.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ service ConstraintsService {
operations: [
ConstrainedShapesOperation,
ConstrainedHttpBoundShapesOperation,
ConstrainedHttpPayloadBoundShapeOperation,
ConstrainedRecursiveShapesOperation,

// `httpQueryParams` and `httpPrefixHeaders` are structurually
// exclusive, so we need one operation per target shape type
// combination.
Expand Down Expand Up @@ -59,6 +61,13 @@ operation ConstrainedHttpBoundShapesOperation {
errors: [ValidationException]
}

@http(uri: "/constrained-http-payload-bound-shape-operation", method: "POST")
operation ConstrainedHttpPayloadBoundShapeOperation {
input: ConstrainedHttpPayloadBoundShapeOperationInputOutput,
output: ConstrainedHttpPayloadBoundShapeOperationInputOutput,
errors: [ValidationException]
}

@http(uri: "/constrained-recursive-shapes-operation", method: "POST")
operation ConstrainedRecursiveShapesOperation {
input: ConstrainedRecursiveShapesOperationInputOutput,
Expand Down Expand Up @@ -311,6 +320,12 @@ structure ConstrainedHttpBoundShapesOperationInputOutput {
enumStringListQuery: ListOfEnumString,
}

structure ConstrainedHttpPayloadBoundShapeOperationInputOutput {
@required
@httpPayload
httpPayloadBoundConstrainedShape: ConA
}

structure QueryParamsTargetingMapOfPatternStringOperationInputOutput {
@httpQueryParams
mapOfPatternString: MapOfPatternString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class HttpBindingGenerator(
// The output needs to be Optional when deserializing the payload body or the caller signature
// will not match.
val outputT = symbolProvider.toSymbol(binding.member).makeOptional()
rustBlock("pub fn $fnName(body: &[u8]) -> std::result::Result<#T, #T>", outputT, errorSymbol) {
rustBlock("pub(crate) fn $fnName(body: &[u8]) -> std::result::Result<#T, #T>", outputT, errorSymbol) {
deserializePayloadBody(
binding,
errorSymbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,11 @@ class HttpBoundProtocolPayloadGenerator(
) {
val ref = if (payloadMetadata.takesOwnership) "" else "&"
val serializer = protocolFunctions.serializeFn(member, fnNameSuffix = "http_payload") { fnName ->
val outputT = if (member.isStreaming(model)) symbolProvider.toSymbol(member) else RuntimeType.ByteSlab.toSymbol()
val outputT = if (member.isStreaming(model)) {
symbolProvider.toSymbol(member)
} else {
RuntimeType.ByteSlab.toSymbol()
}
Comment on lines +242 to +246
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated style change.

rustBlockTemplate(
"pub fn $fnName(payload: $ref#{Member}) -> Result<#{outputT}, #{BuildError}>",
"Member" to symbolProvider.toSymbol(member),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ class JsonParserGenerator(

override fun payloadParser(member: MemberShape): RuntimeType {
val shape = model.expectShape(member.target)
val returnSymbolToParse = returnSymbolToParse(shape)
check(shape is UnionShape || shape is StructureShape || shape is DocumentShape) {
"payload parser should only be used on structures & unions"
"Payload parser should only be used on structure shapes, union shapes, and document shapes."
}
return protocolFunctions.deserializeFn(shape, fnNameSuffix = "payload") { fnName ->
rustBlockTemplate(
"pub fn $fnName(input: &[u8]) -> Result<#{Shape}, #{Error}>",
"pub(crate) fn $fnName(input: &[u8]) -> Result<#{ReturnType}, #{Error}>",
*codegenScope,
"Shape" to symbolProvider.toSymbol(shape),
"ReturnType" to returnSymbolToParse.symbol,
) {
val input = if (shape is DocumentShape) {
"input"
Expand Down