From 2234a508a1e01c2cedf3121d31291554c3597c7c Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Wed, 8 Jan 2020 14:35:52 -0800 Subject: [PATCH] move isErrorCodeInBody flag to constructor and class member --- .../HttpBindingProtocolGenerator.java | 43 +++++++++++-------- .../integration/HttpRpcProtocolGenerator.java | 32 +++++++------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java index 661d0b0678f..47c5640ea43 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java @@ -71,6 +71,16 @@ public abstract class HttpBindingProtocolGenerator implements ProtocolGenerator private final Set serializingDocumentShapes = new TreeSet<>(); private final Set deserializingDocumentShapes = new TreeSet<>(); private final Set deserializingErrorShapes = new TreeSet<>(); + private final boolean isErrorCodeInBody; + + /** + * Creates a Http binding protocol generator. + * + * @param isErrorCodeInBody A boolean indicates whether error code is located in error response body. + */ + public HttpBindingProtocolGenerator(boolean isErrorCodeInBody) { + this.isErrorCodeInBody = isErrorCodeInBody; + } @Override public ApplicationProtocol getApplicationProtocol() { @@ -595,7 +605,7 @@ private void generateOperationDeserializer( // Write out the error deserialization dispatcher. Set errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher( - context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody()); + context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody); deserializingErrorShapes.addAll(errorShapes); } @@ -607,13 +617,12 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape Symbol errorSymbol = symbolProvider.toSymbol(error); String errorDeserMethodName = ProtocolGenerator.getDeserFunctionName(errorSymbol, context.getProtocolName()) + "Response"; - boolean isBodyParsed = this.isErrorCodeInBody(); writer.openBlock("const $L = async (\n" + " $L: any,\n" + " context: __SerdeContext\n" + "): Promise<$T> => {", "};", - errorDeserMethodName, isBodyParsed ? "parsedOutput" : "output", errorSymbol, () -> { + errorDeserMethodName, this.isErrorCodeInBody ? "parsedOutput" : "output", errorSymbol, () -> { writer.openBlock("const contents: $T = {", "};", errorSymbol, () -> { writer.write("__type: $S,", error.getId().getName()); writer.write("$$fault: $S,", error.getTrait(ErrorTrait.class).get().getValue()); @@ -624,7 +633,8 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape }); readHeaders(context, error, bindingIndex); - List documentBindings = readErrorResponseBody(context, error, bindingIndex, isBodyParsed); + List documentBindings = readErrorResponseBody( + context, error, bindingIndex); // Track all shapes bound to the document so their deserializers may be generated. documentBindings.forEach(binding -> { Shape target = model.expectShape(binding.getMember().getTarget()); @@ -639,11 +649,10 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape private List readErrorResponseBody( GenerationContext context, Shape error, - HttpBindingIndex bindingIndex, - boolean isBodyParsed + HttpBindingIndex bindingIndex ) { TypeScriptWriter writer = context.getWriter(); - if (isBodyParsed) { + if (this.isErrorCodeInBody) { // Body is already parsed in error dispatcher, simply assign body to data. writer.write("const data: any = output.body;"); return ListUtils.of(); @@ -912,10 +921,16 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha * Writes the code that loads an {@code errorCode} String with the content used * to dispatch errors to specific serializers. * - *

Three variables will be in scope: + *

Two variables will be in scope: *

    - *
  • {@code output}: a value of the HttpResponse type.
  • - *
  • {@code data}: the contents of the response body.
  • + *
  • {@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type. + *
      + *
    • {@code errorOutput} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to + * {@code false}
    • + *
    • {@code parsedOutput} is a HttpResponse type with body parsed to JavaScript object, available + * when {@code isErrorCodeInBody} is set to {@code true}
    • + *
    + *
  • *
  • {@code context}: the SerdeContext.
  • *
* @@ -929,14 +944,6 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha */ protected abstract void writeErrorCodeParser(GenerationContext context); - /** - * A boolean indicates whether body is collected and parsed in error code parser. - * If so, each error shape deserializer should not parse body again. - * - * @return returns whether the error code exists in response body - */ - protected abstract boolean isErrorCodeInBody(); - /** * Writes the code needed to deserialize the output document of a response. * diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java index 363d05070ff..28c5513a16d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java @@ -37,6 +37,16 @@ public abstract class HttpRpcProtocolGenerator implements ProtocolGenerator { private final Set serializingDocumentShapes = new TreeSet<>(); private final Set deserializingDocumentShapes = new TreeSet<>(); private final Set deserializingErrorShapes = new TreeSet<>(); + private final boolean isErrorCodeInBody; + + /** + * Creates a Http RPC protocol generator. + * + * @param isErrorCodeInBody A boolean indicates whether error code is located in error response body. + */ + public HttpRpcProtocolGenerator(boolean isErrorCodeInBody) { + this.isErrorCodeInBody = isErrorCodeInBody; + } @Override public ApplicationProtocol getApplicationProtocol() { @@ -254,7 +264,7 @@ private void generateOperationDeserializer(GenerationContext context, OperationS // Write out the error deserialization dispatcher. Set errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher( - context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody()); + context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody); deserializingErrorShapes.addAll(errorShapes); } @@ -314,9 +324,12 @@ private void readResponseBody(GenerationContext context, OperationShape operatio *

Two variables will be in scope: *

    *
  • {@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type. - * {@code errorOutput} is a raw HttpResponse whereas {@code parsedOutput} is a HttpResponse type with - * body parsed to JavaScript object. - * The actual value available is determined by {@link #isErrorCodeInBody} + *
      + *
    • {@code errorOutput} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to + * {@code false}
    • + *
    • {@code parsedOutput} is a HttpResponse type with body parsed to JavaScript object, available + * when {@code isErrorCodeInBody} is set to {@code true}
    • + *
    *
  • *
  • {@code context}: the SerdeContext.
  • *
@@ -331,17 +344,6 @@ private void readResponseBody(GenerationContext context, OperationShape operatio */ protected abstract void writeErrorCodeParser(GenerationContext context); - /** - * Indicates whether body is collected and parsed in error dispatcher. - * - *

If returns true, {@link #writeErrorCodeParser} will have {@code parsedOutput} in scope - * - *

If returns false, {@link #writeErrorCodeParser} will have {@code errorOutput} in scope - * - * @return returns whether the error code exists in response body - */ - protected abstract boolean isErrorCodeInBody(); - /** * Writes the code needed to deserialize the output document of a response. *