Skip to content

Commit be7822e

Browse files
Allow accept to be the default content type
This updates accept handling to allow it to be the default content type, even if the output has no content type. This also adds some comments detailing precisely how accept and content-type are being handled.
1 parent 59e98b1 commit be7822e

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,22 @@ private void generateOperationRequestDeserializer(
16791679
writer.write("");
16801680
}
16811681

1682+
/**
1683+
* Writes out handling for the content-type header. The following rules apply:
1684+
*
1685+
* - The content-type header may always be omitted.
1686+
* - If the input shape has a member with the httpPaylaod trait then the following apply:
1687+
* - If the target is a shape with the mediaType trait, the value of the content-type header must
1688+
* match if present.
1689+
* - If the target is a blob shape without a media type, the content-type header may have any value.
1690+
* - Otherwise the content-type header must match the implied content type of the target shape, e.g.
1691+
* text/plain for a string.
1692+
* - If the input shape has no members with the httpPayload trait, but does have members bound to
1693+
* the document, the content-type header must match the default protocol document content type if
1694+
* present.
1695+
* - If the input shape has no members bound to the payload / document, the content-type header
1696+
* must not be set.
1697+
*/
16821698
private void handleContentType(
16831699
GenerationContext context,
16841700
OperationShape operation,
@@ -1717,6 +1733,19 @@ private void handleContentType(
17171733
});
17181734
}
17191735

1736+
/**
1737+
* Writes out handling for the accept header. The following rules apply:
1738+
*
1739+
* - The accept header may always be omitted.
1740+
* - If the output shape has a member with the httpPaylaod trait then the following apply:
1741+
* - If the target is a shape with the mediaType trait, the value of the accept header must
1742+
* match if present.
1743+
* - If the target is a blob shape without a media type, the accept header may have any value.
1744+
* - Otherwise the accept header must match the implied content type of the target shape, e.g.
1745+
* text/plain for a string.
1746+
* - If the output shape has no members with the httpPayload trait, the accept header must match
1747+
* the default protocol document content type if present.
1748+
*/
17201749
private void handleAccept(
17211750
GenerationContext context,
17221751
OperationShape operation,
@@ -1740,18 +1769,12 @@ private void handleAccept(
17401769
+ ".find(key => key.toLowerCase() === 'accept');");
17411770
writer.openBlock("if (acceptHeaderKey !== undefined && acceptHeaderKey !== null) {", "};", () -> {
17421771
writer.write("const accept = output.headers[acceptHeaderKey];");
1743-
if (optionalContentType.isPresent() || operation.getOutput().isPresent()) {
1744-
String contentType = optionalContentType.orElse(getDocumentContentType());
1745-
// If the operation will return a content type, ensure that the accept matches.
1746-
writer.openBlock("if (accept !== undefined && accept !== $S) {", "};", contentType, () -> {
1747-
writer.write("throw new __NotAcceptableException();");
1748-
});
1749-
} else {
1750-
// If the operation won't return a content-type, ensure that accept isn't set.
1751-
writer.openBlock("if (accept !== undefined) {", "};", () -> {
1752-
writer.write("throw new __NotAcceptableException();");
1753-
});
1754-
}
1772+
String contentType = optionalContentType.orElse(getDocumentContentType());
1773+
// Validate that the content type matches the protocol default, or what's modeled if there's
1774+
// a modeled type.
1775+
writer.openBlock("if (accept !== undefined && accept !== $S) {", "};", contentType, () -> {
1776+
writer.write("throw new __NotAcceptableException();");
1777+
});
17551778
});
17561779
}
17571780

0 commit comments

Comments
 (0)