Skip to content

Commit 764047e

Browse files
kuhetrivikr
andauthored
fix: serialize milliseconds for DateTime and EpochSeconds timestamp formats (#1289)
* fix: serialize milliseconds for DateTime and EpochSeconds timestamp formats * fix(smithy-client): add date-time serializer function * use truncating serializer function for datetime * no truncation * remove brackets Co-authored-by: Trivikram Kamat <[email protected]> * update Smithy link --------- Co-authored-by: Trivikram Kamat <[email protected]>
1 parent 659a690 commit 764047e

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

.changeset/sweet-years-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": minor
3+
---
4+
5+
add dateTime serializer function

packages/smithy-client/src/ser-utils.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { serializeFloat } from "./ser-utils";
1+
import { serializeDateTime, serializeFloat } from "./ser-utils";
22

33
describe("serializeFloat", () => {
44
it("handles non-numerics", () => {
@@ -12,3 +12,16 @@ describe("serializeFloat", () => {
1212
expect(serializeFloat(1.1)).toEqual(1.1);
1313
});
1414
});
15+
16+
describe("serializeDateTime", () => {
17+
it("should not truncate at the top of the second", () => {
18+
const date = new Date(1716476757761);
19+
date.setMilliseconds(0);
20+
expect(serializeDateTime(date)).toEqual("2024-05-23T15:05:57.000Z");
21+
});
22+
23+
it("should not truncate in general", () => {
24+
const date = new Date(1716476757761);
25+
expect(serializeDateTime(date)).toEqual("2024-05-23T15:05:57.761Z");
26+
});
27+
});

packages/smithy-client/src/ser-utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ export const serializeFloat = (value: number): string | number => {
2020
return value;
2121
}
2222
};
23+
24+
/**
25+
* @param date - to be serialized.
26+
* @returns https://smithy.io/2.0/spec/protocol-traits.html#timestampformat-trait date-time format.
27+
*/
28+
export const serializeDateTime = (date: Date): string => date.toISOString();

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,23 @@ public static String getTimestampInputParam(
7979
) {
8080
switch (format) {
8181
case DATE_TIME:
82-
// Use the split to not serialize milliseconds.
83-
return "(" + dataSource + ".toISOString().split('.')[0]+\"Z\")";
82+
context.getWriter().addImport(
83+
"serializeDateTime",
84+
"__serializeDateTime",
85+
TypeScriptDependency.AWS_SMITHY_CLIENT
86+
);
87+
return "__serializeDateTime(" + dataSource + ")";
8488
case EPOCH_SECONDS:
85-
return "Math.round(" + dataSource + ".getTime() / 1000)";
89+
return "(" + dataSource + ".getTime() / 1_000)";
8690
case HTTP_DATE:
87-
context.getWriter().addImport("dateToUtcString", "__dateToUtcString",
88-
TypeScriptDependency.AWS_SMITHY_CLIENT);
91+
context.getWriter().addImport(
92+
"dateToUtcString",
93+
"__dateToUtcString",
94+
TypeScriptDependency.AWS_SMITHY_CLIENT
95+
);
8996
return "__dateToUtcString(" + dataSource + ")";
9097
default:
91-
throw new CodegenException("Unexpected timestamp format `" + format.toString() + "` on " + shape);
98+
throw new CodegenException("Unexpected timestamp format `" + format + "` on " + shape);
9299
}
93100
}
94101

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public void givesCorrectTimestampSerialization() {
2828
mockContext.setWriter(writer);
2929
TimestampShape shape = TimestampShape.builder().id("com.smithy.example#Foo").build();
3030

31-
assertThat("(" + DATA_SOURCE + ".toISOString().split('.')[0]+\"Z\")",
31+
assertThat("__serializeDateTime(" + DATA_SOURCE + ")",
3232
equalTo(HttpProtocolGeneratorUtils.getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.DATE_TIME)));
33-
assertThat("Math.round(" + DATA_SOURCE + ".getTime() / 1000)",
33+
assertThat("(" + DATA_SOURCE + ".getTime() / 1_000)",
3434
equalTo(HttpProtocolGeneratorUtils.getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.EPOCH_SECONDS)));
3535
assertThat("__dateToUtcString(" + DATA_SOURCE + ")",
3636
equalTo(HttpProtocolGeneratorUtils.getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.HTTP_DATE)));

0 commit comments

Comments
 (0)