Skip to content

Commit de82c5f

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

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
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: 21 additions & 5 deletions
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,8 @@ private void writeNormalHeader(GenerationContext context, HttpBinding binding) {
10351035
: headerValue + defaultValue;
10361036

10371037
// evaluated value has a function or method call attached
1038+
context.getWriter()
1039+
.addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
10381040
headerBuffer.put(headerKey, String.format(
10391041
"[%s]: [() => isSerializableHeaderValue(%s), () => %s],",
10401042
context.getStringStore().var(headerKey),
@@ -1093,6 +1095,7 @@ private void writeResponseHeaders(
10931095
TypeScriptWriter writer = context.getWriter();
10941096

10951097
// Headers are always present either from the default document or the payload.
1098+
writer.addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
10961099
writer.openBlock("let headers: any = map({}, isSerializableHeaderValue, {", "});", () -> {
10971100
writeContentTypeHeader(context, operationOrError, false);
10981101
injectExtraHeaders.run();
@@ -1373,7 +1376,13 @@ private String getCollectionInputParam(
13731376
dataSource = "Array.from(" + dataSource + ".values())";
13741377
}
13751378
String collectionTargetValue = getInputValue(context, bindingType, "_entry", targetMember, collectionTarget);
1376-
String iteratedParam = "(" + dataSource + " || []).map(_entry => " + collectionTargetValue + " as any)";
1379+
String iteratedParam;
1380+
if (collectionTargetValue.equals("_entry")) {
1381+
iteratedParam = "(" + dataSource + " || [])";
1382+
} else {
1383+
iteratedParam = "(" + dataSource + " || []).map(_entry => " + collectionTargetValue + " as any)";
1384+
}
1385+
13771386
switch (bindingType) {
13781387
case HEADER:
13791388
return iteratedParam + ".join(', ')";
@@ -2689,20 +2698,25 @@ private String getCollectionOutputParam(
26892698
switch (bindingType) {
26902699
case QUERY_PARAMS:
26912700
case QUERY:
2701+
if (collectionTargetValue.equals("_entry")) {
2702+
return String.format("%1$s", dataSource);
2703+
}
26922704
return String.format("%1$s.map(_entry => %2$s as any)", dataSource, collectionTargetValue);
26932705
case LABEL:
26942706
dataSource = "(" + dataSource + " || \"\")";
26952707
// Split these values on slashes.
26962708
outputParam = "" + dataSource + ".split('/')";
26972709

26982710
// Iterate over each entry and do deser work.
2699-
outputParam += ".map(_entry => " + collectionTargetValue + " as any)";
2711+
if (!collectionTargetValue.equals("_entry")) {
2712+
outputParam += ".map(_entry => " + collectionTargetValue + " as any)";
2713+
}
27002714

27012715
return outputParam;
27022716
case HEADER:
27032717
dataSource = "(" + dataSource + " || \"\")";
27042718
// Split these values on commas.
2705-
outputParam = "" + dataSource + ".split(',')";
2719+
outputParam = dataSource + ".split(',')";
27062720

27072721
// Headers that have HTTP_DATE formatted timestamps already contain a ","
27082722
// in their formatted entry, so split on every other "," instead.
@@ -2719,7 +2733,9 @@ private String getCollectionOutputParam(
27192733
}
27202734

27212735
// Iterate over each entry and do deser work.
2722-
outputParam += ".map(_entry => " + collectionTargetValue + " as any)";
2736+
if (!collectionTargetValue.equals("_entry")) {
2737+
outputParam += ".map(_entry => " + collectionTargetValue + " as any)";
2738+
}
27232739

27242740
return outputParam;
27252741
default:

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
4848
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
4949
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
50-
import software.amazon.smithy.utils.IoUtils;
5150
import software.amazon.smithy.utils.SmithyInternalApi;
5251
import software.amazon.smithy.utils.SmithyUnstableApi;
5352

@@ -279,16 +278,6 @@ public static void generateCollectBodyString(GenerationContext context) {
279278
writer.write("");
280279
}
281280

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-
292281
/**
293282
* Writes $retryable key for error if it contains RetryableTrait.
294283
*

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)