Skip to content

Commit c07537f

Browse files
committed
feat(codegen): add Smithy RPCv2 CBOR protocol as an option
1 parent ed000cc commit c07537f

File tree

13 files changed

+1134
-2039
lines changed

13 files changed

+1134
-2039
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddProtocols.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.List;
1919
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
2020
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
21-
import software.amazon.smithy.typescript.codegen.protocols.cbor.SmithyRpcV2Cbor;
2221
import software.amazon.smithy.utils.ListUtils;
2322
import software.amazon.smithy.utils.SmithyInternalApi;
2423

@@ -28,13 +27,20 @@
2827
@SmithyInternalApi
2928
public class AddProtocols implements TypeScriptIntegration {
3029

30+
/**
31+
* This order differs from the base protocol selection specification
32+
* in that for JavaScript runtimes, JSON-based protocols have higher default priority than CBOR-based.
33+
* This behavior may be fine-tuned at the service level in a case-by-case basis.
34+
*
35+
* @return a list of ProtocolGenerators in the default priority order, highest first.
36+
*/
3137
@Override
3238
public List<ProtocolGenerator> getProtocolGenerators() {
3339
return ListUtils.of(
34-
new SmithyRpcV2Cbor(),
3540
new AwsJsonRpc1_0(),
3641
new AwsJsonRpc1_1(),
3742
new AwsRestJson1(),
43+
new AwsSmithyRpcV2Cbor(),
3844
new AwsRestXml(),
3945
new AwsQuery(),
4046
new AwsEc2()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.aws.typescript.codegen;
7+
8+
import software.amazon.smithy.aws.traits.protocols.AwsQueryCompatibleTrait;
9+
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
10+
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
11+
import software.amazon.smithy.typescript.codegen.protocols.cbor.SmithyRpcV2Cbor;
12+
13+
/**
14+
* Extension of the Smithy RPCv2 CBOR protocol generator, adding
15+
* AWSQuery compatibility.
16+
*/
17+
public final class AwsSmithyRpcV2Cbor extends SmithyRpcV2Cbor {
18+
@Override
19+
public void generateSharedComponents(GenerationContext context) {
20+
super.generateSharedComponents(context);
21+
if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
22+
TypeScriptWriter writer = context.getWriter();
23+
writer.addImport("HeaderBag", "__HeaderBag", TypeScriptDependency.SMITHY_TYPES);
24+
writer.write("""
25+
const populateBodyWithQueryCompatibility = (parsedOutput: any, headers: __HeaderBag) => {
26+
const queryErrorHeader = headers["x-amzn-query-error"];
27+
if (parsedOutput.body !== undefined && queryErrorHeader != null) {
28+
const codeAndType = queryErrorHeader.split(";");
29+
parsedOutput.body.Code = codeAndType[0];
30+
parsedOutput.body.Type = codeAndType[1];
31+
}
32+
};
33+
""");
34+
}
35+
}
36+
37+
@Override
38+
protected void writeErrorCodeParser(GenerationContext generationContext) {
39+
super.writeErrorCodeParser(generationContext);
40+
TypeScriptWriter writer = generationContext.getWriter();
41+
42+
if (generationContext.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
43+
// Populate parsedOutput.body with 'Code' and 'Type' fields
44+
// "x-amzn-query-error" header is available when AwsQueryCompatibleTrait is applied to a service
45+
// The header value contains query error Code and Type joined by ';'
46+
// E.g. "MalformedInput;Sender" or "InternalFailure;Receiver"
47+
writer.write("populateBodyWithQueryCompatibility(parsedOutput, output.headers);");
48+
}
49+
}
50+
}

private/aws-protocoltests-ec2/test/functional/ec2query.spec.ts

Lines changed: 87 additions & 111 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)