Skip to content

Commit 5a242c7

Browse files
committed
Handle more complex Accept header values
Accept values can have wildcards, and multiple types can be specified in the header. Use a helper function to handle all the possible permutations specified in https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
1 parent 5cc4d25 commit 5a242c7

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,9 @@ private void handleContentType(
17461746
* text/plain for a string.
17471747
* - If the output shape has no members with the httpPayload trait, the accept header must match
17481748
* the default protocol document content type if present.
1749+
*
1750+
* Note: matching is performed based on the rules in https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
1751+
* and any match is considered acceptable, regardless of the supplied accept-params.
17491752
*/
17501753
private void handleAccept(
17511754
GenerationContext context,
@@ -1766,16 +1769,16 @@ private void handleAccept(
17661769
writer.addImport("NotAcceptableException",
17671770
"__NotAcceptableException",
17681771
"@aws-smithy/server-common");
1772+
writer.addImport("acceptMatches", "__acceptMatches", "@aws-smithy/server-common");
17691773
writer.write("const acceptHeaderKey: string | undefined = Object.keys(output.headers)"
17701774
+ ".find(key => key.toLowerCase() === 'accept');");
17711775
writer.openBlock("if (acceptHeaderKey !== undefined && acceptHeaderKey !== null) {", "};", () -> {
17721776
writer.write("const accept = output.headers[acceptHeaderKey];");
17731777
String contentType = optionalContentType.orElse(getDocumentContentType());
17741778
// Validate that the content type matches the protocol default, or what's modeled if there's
17751779
// a modeled type.
1776-
writer.openBlock("if (accept !== undefined && accept !== $S) {", "};", contentType, () -> {
1777-
writer.write("throw new __NotAcceptableException();");
1778-
});
1780+
writer.openBlock("if (!__acceptMatches(accept, $S)) {", "};", contentType,
1781+
() -> writer.write("throw new __NotAcceptableException();"));
17791782
});
17801783
}
17811784

0 commit comments

Comments
 (0)