From 1086d6f389b58dcb6d9cf339916791d76b7ed49f Mon Sep 17 00:00:00 2001 From: fossand Date: Fri, 23 Sep 2022 13:52:28 -0700 Subject: [PATCH] feat(codegen): add query compatible header error code handling for JSON protocols --- codegen/build.gradle.kts | 2 +- .../aws/typescript/codegen/AwsProtocolUtils.java | 10 ++++++++++ .../codegen/JsonRpcProtocolGenerator.java | 13 +++++++++++++ ...ulate-body-with-query-compatibility-code-stub.ts | 8 ++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/populate-body-with-query-compatibility-code-stub.ts diff --git a/codegen/build.gradle.kts b/codegen/build.gradle.kts index 0f7212571bedf..126405d652136 100644 --- a/codegen/build.gradle.kts +++ b/codegen/build.gradle.kts @@ -31,7 +31,7 @@ allprojects { version = "0.12.0" } -extra["smithyVersion"] = "[1.25.0,1.26.0[" +extra["smithyVersion"] = "[1.25.1,1.26.0[" // The root project doesn't produce a JAR. tasks["jar"].enabled = false diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java index 9691fc13c43dd..bc6ea0c81b2b5 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java @@ -43,6 +43,7 @@ import software.amazon.smithy.typescript.codegen.integration.HttpProtocolGeneratorUtils; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext; +import software.amazon.smithy.utils.IoUtils; import software.amazon.smithy.utils.SmithyInternalApi; /** @@ -117,6 +118,15 @@ static void generateJsonParseBody(GenerationContext context) { writer.write(""); } + static void generateJsonParseBodyWithQueryHeader(GenerationContext context) { + TypeScriptWriter writer = context.getWriter(); + writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types"); + writer.addImport("HttpResponse", "__HttpResponse", "@aws-sdk/protocol-http"); + writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types"); + writer.write(IoUtils.readUtf8Resource( + AwsProtocolUtils.class, "populate-body-with-query-compatibility-code-stub.ts")); + } + /** * Writes a response body parser function for XML protocols. This * will parse a present body after converting it to utf-8. diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java index 714c7a03de066..4a2029355b1d9 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java @@ -16,6 +16,7 @@ package software.amazon.smithy.aws.typescript.codegen; import java.util.Set; +import software.amazon.smithy.aws.traits.protocols.AwsQueryCompatibleTrait; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.Shape; @@ -85,6 +86,10 @@ public void generateSharedComponents(GenerationContext context) { TypeScriptWriter writer = context.getWriter(); writer.addUseImports(getApplicationProtocol().getResponseType()); writer.write(IoUtils.readUtf8Resource(getClass(), "load-json-error-code-stub.ts")); + + if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) { + AwsProtocolUtils.generateJsonParseBodyWithQueryHeader(context); + } } @Override @@ -127,6 +132,14 @@ protected boolean writeUndefinedInputBody(GenerationContext context, OperationSh protected void writeErrorCodeParser(GenerationContext context) { TypeScriptWriter writer = context.getWriter(); + if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) { + // Populate parsedOutput.body with 'Code' and 'Type' fields + // "x-amzn-query-error" header is available when AwsQueryCompatibleTrait is applied to a service + // The header value contains query error Code and Type joined by ';' + // E.g. "MalformedInput;Sender" or "InternalFailure;Receiver" + writer.write("populateBodyWithQueryCompatibility(parsedOutput, output.headers);"); + } + // Outsource error code parsing since it's complex for this protocol. writer.write("const errorCode = loadRestJsonErrorCode(output, parsedOutput.body);"); } diff --git a/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/populate-body-with-query-compatibility-code-stub.ts b/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/populate-body-with-query-compatibility-code-stub.ts new file mode 100644 index 0000000000000..e66725f360d98 --- /dev/null +++ b/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/populate-body-with-query-compatibility-code-stub.ts @@ -0,0 +1,8 @@ +const populateBodyWithQueryCompatibility = (parsedOutput: any, headers: __HeaderBag) => { + const queryErrorHeader = headers["x-amzn-query-error"]; + if (parsedOutput.body !== undefined && queryErrorHeader != null) { + const codeAndType = queryErrorHeader.split(";"); + parsedOutput.body.Code = codeAndType[0]; + parsedOutput.body.Type = codeAndType[1]; + } +};