From 5a1e547891051cd452ffb4a251712af197fb4191 Mon Sep 17 00:00:00 2001 From: smilkuri Date: Mon, 14 Apr 2025 18:38:00 +0000 Subject: [PATCH 1/3] fix: reject content-type header for empty body requests --- .../codegen/integration/HttpBindingProtocolGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java index fab8e968606..ab16fbc8331 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java @@ -1868,7 +1868,7 @@ private void handleContentType( if (optionalContentType.isPresent() || operation.getInput().isPresent()) { String contentType = optionalContentType.orElse(getDocumentContentType()); // If the operation accepts a content type, it must be either unset or the expected value. - writer.openBlock("if (contentType !== undefined && contentType !== $S) {", "};", contentType, () -> { + writer.openBlock("if (contentType !== undefined) {", "};", () -> { writer.write("throw new __UnsupportedMediaTypeException();"); }); } else { From 02796f710d4527b4abc464ac36ac5f78c939886c Mon Sep 17 00:00:00 2001 From: smilkuri Date: Wed, 16 Apr 2025 02:46:45 +0000 Subject: [PATCH 2/3] fix: modify conditional check --- .../codegen/integration/HttpBindingProtocolGenerator.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java index ab16fbc8331..9264cf340b8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java @@ -1861,14 +1861,18 @@ private void handleContentType( TypeScriptDependency.SERVER_COMMON); Optional optionalContentType = bindingIndex.determineRequestContentType( operation, getDocumentContentType()); + boolean hasInputWithMembers = operation.getInput() + .map(inputId -> context.getModel().getShape(inputId).get() + .members().size() > 0) + .orElse(false); writer.write("const contentTypeHeaderKey: string | undefined = Object.keys(output.headers)" + ".find(key => key.toLowerCase() === 'content-type');"); writer.openBlock("if (contentTypeHeaderKey != null) {", "};", () -> { writer.write("const contentType = output.headers[contentTypeHeaderKey];"); - if (optionalContentType.isPresent() || operation.getInput().isPresent()) { + if (optionalContentType.isPresent() || hasInputWithMembers) { String contentType = optionalContentType.orElse(getDocumentContentType()); // If the operation accepts a content type, it must be either unset or the expected value. - writer.openBlock("if (contentType !== undefined) {", "};", () -> { + writer.openBlock("if (contentType !== undefined && contentType !== $S) {", "};", contentType, () -> { writer.write("throw new __UnsupportedMediaTypeException();"); }); } else { From e4243345c7ee00b7d7e3a682e81fc1b5ed16ae62 Mon Sep 17 00:00:00 2001 From: smilkuri Date: Wed, 16 Apr 2025 16:11:03 +0000 Subject: [PATCH 3/3] fix: use HttpBindingIndex.hasRequestBody for content-type validation --- .../codegen/integration/HttpBindingProtocolGenerator.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java index 9264cf340b8..703d3e58012 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java @@ -1861,15 +1861,11 @@ private void handleContentType( TypeScriptDependency.SERVER_COMMON); Optional optionalContentType = bindingIndex.determineRequestContentType( operation, getDocumentContentType()); - boolean hasInputWithMembers = operation.getInput() - .map(inputId -> context.getModel().getShape(inputId).get() - .members().size() > 0) - .orElse(false); writer.write("const contentTypeHeaderKey: string | undefined = Object.keys(output.headers)" + ".find(key => key.toLowerCase() === 'content-type');"); writer.openBlock("if (contentTypeHeaderKey != null) {", "};", () -> { writer.write("const contentType = output.headers[contentTypeHeaderKey];"); - if (optionalContentType.isPresent() || hasInputWithMembers) { + if (optionalContentType.isPresent() || HttpBindingIndex.of(context.getModel()).hasRequestBody(operation)) { String contentType = optionalContentType.orElse(getDocumentContentType()); // If the operation accepts a content type, it must be either unset or the expected value. writer.openBlock("if (contentType !== undefined && contentType !== $S) {", "};", contentType, () -> {