Skip to content

Commit 541c4fd

Browse files
authored
Add RetryableTrait support for Errors (#183)
* Add $retryable to Error is RetryableTrait present * Write HttpProtocolGeneratorUtils.writeRetryableTrait * Add tests for RetryableTrait * Remove formatting introduced by editor.formatOnSave in VSCode * Call writeRetryableTrait from StructureGenerator
1 parent ad6bd28 commit 541c4fd

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructureGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import software.amazon.smithy.model.Model;
2424
import software.amazon.smithy.model.shapes.StructureShape;
2525
import software.amazon.smithy.model.traits.ErrorTrait;
26+
import software.amazon.smithy.typescript.codegen.integration.HttpProtocolGeneratorUtils;
2627

2728
/**
2829
* Generates normal structures and error structures.
@@ -158,6 +159,7 @@ private void renderErrorStructure() {
158159
writer.openBlock("export interface $L extends $L {", symbol.getName(), extendsFrom);
159160
writer.write("name: $S;", shape.getId().getName());
160161
writer.write("$$fault: $S;", errorTrait.getValue());
162+
HttpProtocolGeneratorUtils.writeRetryableTrait(writer, shape, ";");
161163
StructuredMemberWriter structuredMemberWriter = new StructuredMemberWriter(
162164
model, symbolProvider, shape.getAllMembers().values());
163165
structuredMemberWriter.writeMembers(writer, shape);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
912912
writer.openBlock("const contents: $T = {", "};", errorSymbol, () -> {
913913
writer.write("name: $S,", error.getId().getName());
914914
writer.write("$$fault: $S,", error.getTrait(ErrorTrait.class).get().getValue());
915+
HttpProtocolGeneratorUtils.writeRetryableTrait(writer, error, ",");
915916
writer.write("$$metadata: deserializeMetadata($L),", outputName);
916917
// Set all the members to undefined to meet type constraints.
917918
new TreeMap<>(error.getAllMembers())

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import software.amazon.smithy.model.shapes.StructureShape;
3636
import software.amazon.smithy.model.traits.EndpointTrait;
3737
import software.amazon.smithy.model.traits.MediaTypeTrait;
38+
import software.amazon.smithy.model.traits.RetryableTrait;
3839
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
3940
import software.amazon.smithy.typescript.codegen.CodegenUtils;
4041
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
@@ -241,6 +242,25 @@ static void generateHttpBindingUtils(GenerationContext context) {
241242
writer.write(IoUtils.readUtf8Resource(HttpProtocolGeneratorUtils.class, "http-binding-utils.ts"));
242243
}
243244

245+
/**
246+
* Writes $retryable key for error if it contains RetryableTrait.
247+
*
248+
* @param writer The code writer.
249+
* @param error The error to write retryableTrait for.
250+
* @param separator The string to be used after emitting key-value pair for retryableTrait.
251+
*/
252+
public static void writeRetryableTrait(TypeScriptWriter writer, StructureShape error, String separator) {
253+
Optional<RetryableTrait> retryableTrait = error.getTrait(RetryableTrait.class);
254+
if (retryableTrait.isPresent()) {
255+
String textAfterBlock = String.format("}%s", separator);
256+
writer.openBlock("$$retryable: {", textAfterBlock, () -> {
257+
if (retryableTrait.get().getThrottling()) {
258+
writer.write("throttling: true,");
259+
}
260+
});
261+
}
262+
}
263+
244264
/**
245265
* Writes a function used to dispatch to the proper error deserializer
246266
* for each error that the operation can return. The generated function

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
379379
writer.openBlock("const contents: $T = {", "};", errorSymbol, () -> {
380380
writer.write("name: $S,", error.getId().getName());
381381
writer.write("$$fault: $S,", error.getTrait(ErrorTrait.class).get().getValue());
382+
HttpProtocolGeneratorUtils.writeRetryableTrait(writer, error, ",");
382383
writer.write("$$metadata: deserializeMetadata($L),", outputReference);
383384
writer.write("...deserialized,");
384385
});

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ public void properlyGeneratesRequiredMessageMemberOfException() {
4343
+ "}");
4444
}
4545

46+
@Test
47+
public void generatesEmptyRetryableTrait() {
48+
testErrorStructureCodegen("error-test-retryable.smithy",
49+
"export interface Err extends __SmithyException, $MetadataBearer {\n"
50+
+ " name: \"Err\";\n"
51+
+ " $fault: \"client\";\n"
52+
+ " $retryable: {\n"
53+
+ " };\n"
54+
+ "}");
55+
}
56+
57+
@Test
58+
public void generatesRetryableTraitWithThrottling() {
59+
testErrorStructureCodegen("error-test-retryable-throttling.smithy",
60+
"export interface Err extends __SmithyException, $MetadataBearer {\n"
61+
+ " name: \"Err\";\n"
62+
+ " $fault: \"client\";\n"
63+
+ " $retryable: {\n"
64+
+ " throttling: true,\n"
65+
+ " };\n"
66+
+ "}");
67+
}
68+
4669
@Test
4770
public void filtersSensitiveSimpleShape() {
4871
testStructureCodegen("test-sensitive-simple-shape.smithy",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [DoSomething]
7+
}
8+
9+
operation DoSomething() errors [Err]
10+
11+
@error("client")
12+
@retryable(throttling: true)
13+
structure Err {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [DoSomething]
7+
}
8+
9+
operation DoSomething() errors [Err]
10+
11+
@error("client")
12+
@retryable
13+
structure Err {}

0 commit comments

Comments
 (0)