Skip to content

Update HTTP binding protocol generation #465

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

Merged
merged 1 commit into from
Nov 25, 2019
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ build
codegen/build
codegen/sdk-codegen/smithy-build.json
.gradle
*/out/
*/*/out/
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public class AddProtocols implements TypeScriptIntegration {

@Override
public List<ProtocolGenerator> getProtocolGenerators() {
return ListUtils.of(new AwsRestJson1_1());
return ListUtils.of(new AwsRestJson1_0(), new AwsRestJson1_1());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.typescript.codegen;

/**
* Handles generating the aws.rest-json-1.0 protocol for services.
*
* @inheritDoc
*
* @see RestJsonProtocolGenerator
*/
public final class AwsRestJson1_0 extends RestJsonProtocolGenerator {

@Override
public String getName() {
return "aws.rest-json-1.0";
}

@Override
protected String getDocumentContentType() {
return "application/x-amz-json-1.0";
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.typescript.codegen;

import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.model.shapes.BigDecimalShape;
import software.amazon.smithy.model.shapes.BigIntegerShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberDeserVisitor;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;

/**
* Overrides the default implementation of BigDecimal and BigInteger shape
* deserialization to throw when encountered in AWS JSON based protocols.
*/
final class JsonMemberDeserVisitor extends DocumentMemberDeserVisitor {

/**
* @inheritDoc
*/
JsonMemberDeserVisitor(
GenerationContext context,
String dataSource,
Format defaultTimestampFormat
) {
super(context, dataSource, defaultTimestampFormat);
}

@Override
public String bigDecimalShape(BigDecimalShape shape) {
// Fail instead of losing precision through Number.
return unsupportedShape(shape);
}

@Override
public String bigIntegerShape(BigIntegerShape shape) {
// Fail instead of losing precision through Number.
return unsupportedShape(shape);
}

private String unsupportedShape(Shape shape) {
throw new CodegenException(String.format("Cannot deserialize shape type %s on protocol, shape: %s.",
shape.getType(), shape.getId()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.typescript.codegen;

import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.model.shapes.BigDecimalShape;
import software.amazon.smithy.model.shapes.BigIntegerShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberSerVisitor;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;

/**
* Overrides the default implementation of BigDecimal and BigInteger shape
* serialization to throw when encountered in AWS JSON based protocols.
*/
final class JsonMemberSerVisitor extends DocumentMemberSerVisitor {

/**
* @inheritDoc
*/
JsonMemberSerVisitor(
GenerationContext context,
String dataSource,
Format defaultTimestampFormat
) {
super(context, dataSource, defaultTimestampFormat);
}

@Override
public String bigDecimalShape(BigDecimalShape shape) {
// Fail instead of losing precision through Number.
return unsupportedShape(shape);
}

@Override
public String bigIntegerShape(BigIntegerShape shape) {
// Fail instead of losing precision through Number.
return unsupportedShape(shape);
}

private String unsupportedShape(Shape shape) {
throw new CodegenException(String.format("Cannot deserialize shape type %s on protocol, shape: %s.",
shape.getType(), shape.getId()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.typescript.codegen;

import java.util.Map;
import java.util.TreeMap;
import software.amazon.smithy.model.shapes.CollectionShape;
import software.amazon.smithy.model.shapes.DocumentShape;
import software.amazon.smithy.model.shapes.MapShape;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeIndex;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.JsonNameTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberDeserVisitor;
import software.amazon.smithy.typescript.codegen.integration.DocumentShapeDeserVisitor;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;

/**
* Visitor to generate deserialization functions for shapes in AWS JSON protocol
* document bodies.
*
* No standard visitation methods are overridden; function body generation for all
* expected deserializers is handled by this class.
*
* Timestamps are deserialized from {@link Format}.EPOCH_SECONDS by default.
*/
final class JsonShapeDeserVisitor extends DocumentShapeDeserVisitor {

JsonShapeDeserVisitor(GenerationContext context) {
super(context);
}

private DocumentMemberDeserVisitor getMemberVisitor(String dataSource) {
return new JsonMemberDeserVisitor(getContext(), dataSource, Format.EPOCH_SECONDS);
}

@Override
protected void deserializeCollection(GenerationContext context, CollectionShape shape) {
TypeScriptWriter writer = context.getWriter();
Shape target = context.getModel().getShapeIndex().getShape(shape.getMember().getTarget()).get();

// Dispatch to the output value provider for any additional handling.
writer.openBlock("return (output || []).map((entry: any) =>", ");", () -> {
writer.write(target.accept(getMemberVisitor("entry")));
});
}

@Override
protected void deserializeDocument(GenerationContext context, DocumentShape shape) {
TypeScriptWriter writer = context.getWriter();
// Documents are JSON content, so don't modify.
writer.write("return output;");
}

@Override
protected void deserializeMap(GenerationContext context, MapShape shape) {
TypeScriptWriter writer = context.getWriter();
Shape target = context.getModel().getShapeIndex().getShape(shape.getValue().getTarget()).get();

// Get the right serialization for each entry in the map. Undefined
// outputs won't have this deserializer invoked.
writer.write("let mapParams: any = {};");
writer.openBlock("Object.keys(output).forEach(key => {", "});", () -> {
// Dispatch to the output value provider for any additional handling.
writer.write("mapParams[key] = $L;", target.accept(getMemberVisitor("output[key]")));
});
writer.write("return mapParams;");
}

@Override
protected void deserializeStructure(GenerationContext context, StructureShape shape) {
TypeScriptWriter writer = context.getWriter();

// Prepare the document contents structure.
// Use a TreeMap to sort the members.
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
writer.openBlock("let contents: any = {", "};", () -> {
writer.write("__type: $S,", shape.getId().getName());
if (shape.hasTrait(ErrorTrait.class)) {
writer.write("$$fault: $S,", shape.getTrait(ErrorTrait.class).get().getValue());
}
// Set all the members to undefined to meet type constraints.
members.forEach((memberName, memberShape) -> writer.write("$L: undefined,", memberName));
});
members.forEach((memberName, memberShape) -> {
// Use the jsonName trait value if present, otherwise use the member name.
String locationName = memberShape.getTrait(JsonNameTrait.class)
.map(JsonNameTrait::getValue)
.orElse(memberName);
Shape target = context.getModel().getShapeIndex().getShape(memberShape.getTarget()).get();

// Generate an if statement to set the bodyParam if the member is set.
writer.openBlock("if (output.$L !== undefined) {", "}", locationName, () -> {
writer.write("contents.$L = $L;", memberName,
// Dispatch to the output value provider for any additional handling.
target.accept(getMemberVisitor("output." + locationName)));
});
});

writer.write("return contents;");
}

@Override
protected void deserializeUnion(GenerationContext context, UnionShape shape) {
TypeScriptWriter writer = context.getWriter();
ShapeIndex index = context.getModel().getShapeIndex();

// Check for any known union members and return when we find one.
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
members.forEach((memberName, memberShape) -> {
Shape target = index.getShape(memberShape.getTarget()).get();
// Use the jsonName trait value if present, otherwise use the member name.
String locationName = memberShape.getTrait(JsonNameTrait.class)
.map(JsonNameTrait::getValue)
.orElse(memberName);
writer.openBlock("if (output.$L !== undefined) {", "}", locationName, () -> {
writer.openBlock("return {", "};", () -> {
// Dispatch to the output value provider for any additional handling.
writer.write("$L: $L", memberName, target.accept(getMemberVisitor("output." + locationName)));
});
});
});
// Or write to the unknown member the element in the output.
writer.write("return { $$unknown: output[Object.keys(output)[0]] };");
}
}
Loading