From 0228d5ed46c85a4ad74de3bd83cad2b432f73c71 Mon Sep 17 00:00:00 2001 From: Matteo Bigoi <1781140+crisidev@users.noreply.github.com> Date: Tue, 8 Feb 2022 17:14:27 +0000 Subject: [PATCH] Fix payload deserialization where we need to return an Optional type when the member is non-streaming (#1166) --- codegen-server-test/model/simple.smithy | 24 +++++++++++++++++++ .../generators/http/HttpBindingGenerator.kt | 7 ++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/codegen-server-test/model/simple.smithy b/codegen-server-test/model/simple.smithy index 70dee327f..d4420787f 100644 --- a/codegen-server-test/model/simple.smithy +++ b/codegen-server-test/model/simple.smithy @@ -16,6 +16,7 @@ service SimpleService { ], operations: [ Healthcheck, + StoreServiceBlob, ], } @@ -105,3 +106,26 @@ structure HealthcheckInputRequest { structure HealthcheckOutputResponse { } + +@readonly +@http(method: "GET", uri: "/service/{id}/blob") +@documentation("Stores a blob for a service id") +operation StoreServiceBlob { + input: StoreServiceBlobInput, + output: StoreServiceBlobOutput +} + +@documentation("Store a blob for a service id input structure") +structure StoreServiceBlobInput { + @required + @httpLabel + id: ServiceId, + @required + @httpPayload + content: Blob, +} + +@documentation("Store a blob for a service id output structure") +structure StoreServiceBlobOutput { + +} diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/http/HttpBindingGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/http/HttpBindingGenerator.kt index fc85fb6ca..dfb146f3a 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/http/HttpBindingGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/http/HttpBindingGenerator.kt @@ -170,7 +170,7 @@ class HttpBindingGenerator( } /** - * Generate a function to deserialize `[binding]` from the response payload. + * Generate a function to deserialize `[binding]` from the request / response payload. */ fun generateDeserializePayloadFn( operationShape: OperationShape, @@ -181,10 +181,10 @@ class HttpBindingGenerator( httpMessageType: HttpMessageType = HttpMessageType.RESPONSE ): RuntimeType { check(binding.location == HttpBinding.Location.PAYLOAD) - val outputT = symbolProvider.toSymbol(binding.member) val fnName = "deser_payload_${fnName(operationShape, binding)}" return RuntimeType.forInlineFun(fnName, httpSerdeModule) { rustWriter -> if (binding.member.isStreaming(model)) { + val outputT = symbolProvider.toSymbol(binding.member) rustWriter.rustBlock( "pub fn $fnName(body: &mut #T) -> std::result::Result<#T, #T>", RuntimeType.sdkBody(runtimeConfig), @@ -200,6 +200,9 @@ class HttpBindingGenerator( } } } else { + // 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() rustWriter.rustBlock("pub fn $fnName(body: &[u8]) -> std::result::Result<#T, #T>", outputT, errorT) { deserializePayloadBody( binding, -- GitLab