Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clients parse datetime offsets #681

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ public String timestampShape(TimestampShape shape) {
Location.DOCUMENT,
shape,
format,
requiresNumericEpochSecondsInPayload());
requiresNumericEpochSecondsInPayload(),
context.getSettings().generateClient());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2484,7 +2484,7 @@ private String getOutputValue(
Format format = httpIndex.determineTimestampFormat(member, bindingType, getDocumentTimestampFormat());
return HttpProtocolGeneratorUtils.getTimestampOutputParam(
context.getWriter(), dataSource, bindingType, member, format,
requiresNumericEpochSecondsInPayload());
requiresNumericEpochSecondsInPayload(), context.getSettings().generateClient());
} else if (target instanceof BlobShape) {
return getBlobOutputParam(bindingType, dataSource);
} else if (target instanceof CollectionShape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,30 @@ public static String getTimestampInputParam(
* @param format The timestamp format to provide.
* @param requireNumericEpochSecondsInPayload if true, paylaod epoch seconds are not allowed to be coerced
* from strings.
* @param isClient true if generating a client.
* @return Returns a value or expression of the output timestamp.
*/
public static String getTimestampOutputParam(TypeScriptWriter writer,
String dataSource,
Location bindingType,
Shape shape,
Format format,
boolean requireNumericEpochSecondsInPayload) {
boolean requireNumericEpochSecondsInPayload,
boolean isClient) {
// This has always explicitly wrapped the dataSource in "new Date(..)", so it could never generate
// an expression that evaluates to null. Codegen relies on this.
writer.addImport("expectNonNull", "__expectNonNull", "@aws-sdk/smithy-client");
switch (format) {
case DATE_TIME:
writer.addImport("parseRfc3339DateTime", "__parseRfc3339DateTime", "@aws-sdk/smithy-client");
return String.format("__expectNonNull(__parseRfc3339DateTime(%s))", dataSource);
// Clients should be able to handle offsets and normalize the datetime to an offset of zero.
if (isClient) {
writer.addImport("parseRfc3339DateTimeWithOffset", "__parseRfc3339DateTimeWithOffset",
"@aws-sdk/smithy-client");
return String.format("__expectNonNull(__parseRfc3339DateTimeWithOffset(%s))", dataSource);
} else {
writer.addImport("parseRfc3339DateTime", "__parseRfc3339DateTime", "@aws-sdk/smithy-client");
return String.format("__expectNonNull(__parseRfc3339DateTime(%s))", dataSource);
}
case HTTP_DATE:
writer.addImport("parseRfc7231DateTime", "__parseRfc7231DateTime", "@aws-sdk/smithy-client");
return String.format("__expectNonNull(__parseRfc7231DateTime(%s))", dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import software.amazon.smithy.model.traits.MediaTypeTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.ArtifactType;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
import software.amazon.smithy.utils.ListUtils;

Expand All @@ -46,12 +48,16 @@ public class DocumentMemberDeserVisitorTest {
private static final String PROTOCOL = "TestProtocol";
private static final Format FORMAT = Format.EPOCH_SECONDS;
private static GenerationContext mockContext;
private static TypeScriptSettings mockSettings;

static {
mockContext = new GenerationContext();
mockSettings = new TypeScriptSettings();
mockContext.setProtocolName(PROTOCOL);
mockContext.setSymbolProvider(new MockProvider());
mockContext.setWriter(new TypeScriptWriter("foo"));
mockSettings.setArtifactType(ArtifactType.SSDK);
mockContext.setSettings(mockSettings);
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ public void givesCorrectTimestampDeserialization() {
TimestampShape shape = TimestampShape.builder().id("com.smithy.example#Foo").build();
TypeScriptWriter writer = new TypeScriptWriter("foo");

assertThat("__expectNonNull(__parseRfc3339DateTimeWithOffset(" + DATA_SOURCE + "))",
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.DATE_TIME, false, true)));
assertThat("__expectNonNull(__parseRfc3339DateTime(" + DATA_SOURCE + "))",
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.DATE_TIME, false)));
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.DATE_TIME, false, false)));
assertThat("__expectNonNull(__parseEpochTimestamp(__expectNumber(" + DATA_SOURCE + ")))",
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.EPOCH_SECONDS, true)));
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.EPOCH_SECONDS, true, false)));
assertThat("__expectNonNull(__parseEpochTimestamp(" + DATA_SOURCE + "))",
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.EPOCH_SECONDS, false)));
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.EPOCH_SECONDS, false, false)));
assertThat("__expectNonNull(__parseRfc7231DateTime(" + DATA_SOURCE + "))",
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.HTTP_DATE, false)));
equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.HTTP_DATE, false, false)));
}

@Test
Expand Down