Skip to content

Commit 817ca04

Browse files
committed
bring isSerializableHeaderValue into smithy-client
1 parent 8f6a8c0 commit 817ca04

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

.changeset/small-gifts-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": patch
3+
---
4+
5+
serialize empty strings and collections in headers
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { isSerializableHeaderValue } from "./is-serializable-header-value";
2+
3+
describe(isSerializableHeaderValue.name, () => {
4+
it("considers empty strings serializable", () => {
5+
expect(isSerializableHeaderValue("")).toBe(true);
6+
});
7+
8+
it("considers empty collections serializable", () => {
9+
expect(isSerializableHeaderValue(new Set())).toBe(true);
10+
expect(isSerializableHeaderValue([])).toBe(true);
11+
});
12+
13+
it("considers most falsy data values to be serializable", () => {
14+
expect(isSerializableHeaderValue(false)).toBe(true);
15+
expect(isSerializableHeaderValue(0)).toBe(true);
16+
expect(isSerializableHeaderValue(new Date(0))).toBe(true);
17+
});
18+
19+
it("considered undefined and null to be unserializable", () => {
20+
expect(isSerializableHeaderValue(undefined)).toBe(false);
21+
expect(isSerializableHeaderValue(null)).toBe(false);
22+
});
23+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @internal
3+
* @returns whether the header value is serializable.
4+
*/
5+
export const isSerializableHeaderValue = (value: any) => {
6+
return value != null;
7+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ public void generateSharedComponents(GenerationContext context) {
201201
generateDocumentBodyShapeDeserializers(context, deserializingDocumentShapes);
202202
HttpProtocolGeneratorUtils.generateMetadataDeserializer(context, getApplicationProtocol().getResponseType());
203203
HttpProtocolGeneratorUtils.generateCollectBodyString(context);
204-
HttpProtocolGeneratorUtils.generateHttpBindingUtils(context);
205204

206205
writer.write(
207206
context.getStringStore().flushVariableDeclarationCode()
@@ -965,6 +964,7 @@ private void writeRequestHeaders(
965964
opening = "const headers: any = {";
966965
closing = "};";
967966
} else {
967+
writer.addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
968968
opening = normalHeaderCount > 0
969969
? "const headers: any = map({}, isSerializableHeaderValue, {"
970970
: "const headers: any = map({";
@@ -1035,6 +1035,7 @@ private void writeNormalHeader(GenerationContext context, HttpBinding binding) {
10351035
: headerValue + defaultValue;
10361036

10371037
// evaluated value has a function or method call attached
1038+
writer.addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
10381039
headerBuffer.put(headerKey, String.format(
10391040
"[%s]: [() => isSerializableHeaderValue(%s), () => %s],",
10401041
context.getStringStore().var(headerKey),
@@ -1093,6 +1094,7 @@ private void writeResponseHeaders(
10931094
TypeScriptWriter writer = context.getWriter();
10941095

10951096
// Headers are always present either from the default document or the payload.
1097+
writer.addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
10961098
writer.openBlock("let headers: any = map({}, isSerializableHeaderValue, {", "});", () -> {
10971099
writeContentTypeHeader(context, operationOrError, false);
10981100
injectExtraHeaders.run();

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,6 @@ public static void generateCollectBodyString(GenerationContext context) {
279279
writer.write("");
280280
}
281281

282-
/**
283-
* Writes any additional utils needed for HTTP protocols with bindings.
284-
*
285-
* @param context The generation context.
286-
*/
287-
static void generateHttpBindingUtils(GenerationContext context) {
288-
TypeScriptWriter writer = context.getWriter();
289-
writer.write(IoUtils.readUtf8Resource(HttpProtocolGeneratorUtils.class, "http-binding-utils.ts"));
290-
}
291-
292282
/**
293283
* Writes $retryable key for error if it contains RetryableTrait.
294284
*

smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/integration/http-binding-utils.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)