Skip to content

Commit

Permalink
feat(codegen): update to Smithy-1.16.3 (#3215)
Browse files Browse the repository at this point in the history
Includes removal of @JsonName support from AWS JSON protocols, which is a
change made to the spec in 1.16.
  • Loading branch information
adamthom-amzn committed Jan 24, 2022
1 parent 4839121 commit f8db067
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ allprojects {
version = "0.8.0"
}

extra["smithyVersion"] = "[1.15.0,1.16.0["
extra["smithyVersion"] = "[1.16.3,1.17.0["

// The root project doesn't produce a JAR.
tasks["jar"].enabled = false
Expand Down
2 changes: 1 addition & 1 deletion codegen/generic-client-test-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ buildscript {
}

plugins {
id("software.amazon.smithy") version "0.5.3"
id("software.amazon.smithy") version "0.6.0"
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion codegen/protocol-test-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ buildscript {
}

plugins {
id("software.amazon.smithy") version "0.5.3"
id("software.amazon.smithy") version "0.6.0"
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion codegen/sdk-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ buildscript {
}

plugins {
id("software.amazon.smithy") version "0.5.3"
id("software.amazon.smithy") version "0.6.0"
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,6 @@ private static boolean filterProtocolTests(
return true;
}

// TODO: remove when there's a decision on behavior for list of timestamps.
// https://github.com/awslabs/smithy/issues/1015
if (testCase.getId().equals("RestJsonInputAndOutputWithTimestampHeaders")) {
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ protected Format getDocumentTimestampFormat() {

@Override
protected void generateDocumentBodyShapeSerializers(GenerationContext context, Set<Shape> shapes) {
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes, new JsonShapeSerVisitor(context));
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes,
// AWS JSON does not support jsonName
new JsonShapeSerVisitor(context, (shape, name) -> name));
}

@Override
protected void generateDocumentBodyShapeDeserializers(GenerationContext context, Set<Shape> shapes) {
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes, new JsonShapeDeserVisitor(context));
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes,
// AWS JSON does not support jsonName
new JsonShapeDeserVisitor(context, (shape, name) -> name));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiFunction;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.CollectionShape;
Expand Down Expand Up @@ -51,8 +52,19 @@
@SmithyInternalApi
final class JsonShapeDeserVisitor extends DocumentShapeDeserVisitor {

private final BiFunction<MemberShape, String, String> memberNameStrategy;

JsonShapeDeserVisitor(GenerationContext context) {
this(context,
// Use the jsonName trait value if present, otherwise use the member name.
(memberShape, memberName) -> memberShape.getTrait(JsonNameTrait.class)
.map(JsonNameTrait::getValue)
.orElse(memberName));
}

JsonShapeDeserVisitor(GenerationContext context, BiFunction<MemberShape, String, String> memberNameStrategy) {
super(context);
this.memberNameStrategy = memberNameStrategy;
}

private DocumentMemberDeserVisitor getMemberVisitor(MemberShape memberShape, String dataSource) {
Expand Down Expand Up @@ -154,10 +166,7 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
writer.openBlock("return {", "} as any;", () -> {
// Set all the members to undefined to meet type constraints.
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);
String locationName = memberNameStrategy.apply(memberShape, memberName);
Shape target = context.getModel().expectShape(memberShape.getTarget());

if (usesExpect(target)) {
Expand Down Expand Up @@ -195,10 +204,7 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
members.forEach((memberName, memberShape) -> {
Shape target = model.expectShape(memberShape.getTarget());
// Use the jsonName trait value if present, otherwise use the member name.
String locationName = memberShape.getTrait(JsonNameTrait.class)
.map(JsonNameTrait::getValue)
.orElse(memberName);
String locationName = memberNameStrategy.apply(memberShape, memberName);

String memberValue = target.accept(getMemberVisitor(memberShape, "output." + locationName));
if (usesExpect(target)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiFunction;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.CollectionShape;
Expand Down Expand Up @@ -51,10 +52,22 @@
final class JsonShapeSerVisitor extends DocumentShapeSerVisitor {
private static final Format TIMESTAMP_FORMAT = Format.EPOCH_SECONDS;

private final BiFunction<MemberShape, String, String> memberNameStrategy;

JsonShapeSerVisitor(GenerationContext context) {
this(context,
// Use the jsonName trait value if present, otherwise use the member name.
(memberShape, memberName) -> memberShape.getTrait(JsonNameTrait.class)
.map(JsonNameTrait::getValue)
.orElse(memberName));
}

JsonShapeSerVisitor(GenerationContext context, BiFunction<MemberShape, String, String> memberNameStrategy) {
super(context);
this.memberNameStrategy = memberNameStrategy;
}


private DocumentMemberSerVisitor getMemberVisitor(String dataSource) {
return new JsonMemberSerVisitor(getContext(), dataSource, TIMESTAMP_FORMAT);
}
Expand Down Expand Up @@ -125,10 +138,7 @@ public void serializeStructure(GenerationContext context, StructureShape shape)
// Use a TreeMap to sort the members.
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
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);
String locationName = memberNameStrategy.apply(memberShape, memberName);
Shape target = context.getModel().expectShape(memberShape.getTarget());
String inputLocation = "input." + memberName;

Expand Down Expand Up @@ -160,10 +170,7 @@ public void serializeUnion(GenerationContext context, UnionShape shape) {
// Use a TreeMap to sort the members.
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
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);
String locationName = memberNameStrategy.apply(memberShape, memberName);
Shape target = model.expectShape(memberShape.getTarget());
// Dispatch to the input value provider for any additional handling.
writer.write("$L: value => ({ $S: $L }),", memberName, locationName,
Expand Down

0 comments on commit f8db067

Please sign in to comment.