From e4b8b208a554e46826d11ce45d266eb58372fc70 Mon Sep 17 00:00:00 2001 From: Guy Margalit Date: Mon, 7 Feb 2022 10:41:51 +0200 Subject: [PATCH] Fix duplicate content-type in server response Signed-off-by: Guy Margalit --- .../protocols/ServerHttpProtocolGenerator.kt | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt index 64fb78bc18..69b5ff766c 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt @@ -492,6 +492,21 @@ private class ServerHttpProtocolImplGenerator( * The `Content-Type` header is also set according to the protocol and the contents of the shape to be serialized. */ private fun RustWriter.serverRenderResponseHeaders(operationShape: OperationShape, errorShape: StructureShape? = null) { + val bindingGenerator = ServerResponseBindingGenerator(protocol, codegenContext, operationShape) + val addHeadersFn = bindingGenerator.generateAddHeadersFn(errorShape ?: operationShape) + if (addHeadersFn != null) { + // notice that we need to borrow the output only for output shapes but not for error shapes + val outputOwnedOrBorrow = if (errorShape == null) "&output" else "output" + rust( + """ + builder = #{T}($outputOwnedOrBorrow, builder)?; + """.trimIndent(), + addHeadersFn + ) + } + + // set the content type header *after* the response bindings headers have been set + // to allow operations that bind a member to content-type to take precedence val contentType = httpBindingResolver.responseContentType(operationShape) if (contentType != null) { rustTemplate( @@ -505,19 +520,6 @@ private class ServerHttpProtocolImplGenerator( *codegenScope ) } - - val bindingGenerator = ServerResponseBindingGenerator(protocol, codegenContext, operationShape) - val addHeadersFn = bindingGenerator.generateAddHeadersFn(errorShape ?: operationShape) - if (addHeadersFn != null) { - // notice that we need to borrow the output only for output shapes but not for error shapes - val outputOwnedOrBorrow = if (errorShape == null) "&output" else "output" - rust( - """ - builder = #{T}($outputOwnedOrBorrow, builder)?; - """.trimIndent(), - addHeadersFn - ) - } } private fun serverRenderBindingSerializer(