Skip to content

Commit aaf200a

Browse files
committed
move isErrorCodeInBody flag to constructor and class member
1 parent faba30a commit aaf200a

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ public abstract class HttpBindingProtocolGenerator implements ProtocolGenerator
7171
private final Set<Shape> serializingDocumentShapes = new TreeSet<>();
7272
private final Set<Shape> deserializingDocumentShapes = new TreeSet<>();
7373
private final Set<StructureShape> deserializingErrorShapes = new TreeSet<>();
74+
private final boolean isErrorCodeInBody;
75+
76+
/**
77+
* Creates a Http binding protocol generator.
78+
*
79+
* @param isErrorCodeInBody A boolean indicates whether error code is located in error response body.
80+
*/
81+
public HttpBindingProtocolGenerator(boolean isErrorCodeInBody) {
82+
this.isErrorCodeInBody = isErrorCodeInBody;
83+
}
7484

7585
@Override
7686
public ApplicationProtocol getApplicationProtocol() {
@@ -595,7 +605,7 @@ private void generateOperationDeserializer(
595605

596606
// Write out the error deserialization dispatcher.
597607
Set<StructureShape> errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher(
598-
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody());
608+
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody);
599609
deserializingErrorShapes.addAll(errorShapes);
600610
}
601611

@@ -607,13 +617,12 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
607617
Symbol errorSymbol = symbolProvider.toSymbol(error);
608618
String errorDeserMethodName = ProtocolGenerator.getDeserFunctionName(errorSymbol,
609619
context.getProtocolName()) + "Response";
610-
boolean isBodyParsed = this.isErrorCodeInBody();
611620

612621
writer.openBlock("const $L = async (\n"
613622
+ " $L: any,\n"
614623
+ " context: __SerdeContext\n"
615624
+ "): Promise<$T> => {", "};",
616-
errorDeserMethodName, isBodyParsed ? "parsedOutput" : "output", errorSymbol, () -> {
625+
errorDeserMethodName, this.isErrorCodeInBody ? "parsedOutput" : "output", errorSymbol, () -> {
617626
writer.openBlock("const contents: $T = {", "};", errorSymbol, () -> {
618627
writer.write("__type: $S,", error.getId().getName());
619628
writer.write("$$fault: $S,", error.getTrait(ErrorTrait.class).get().getValue());
@@ -624,7 +633,8 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
624633
});
625634

626635
readHeaders(context, error, bindingIndex);
627-
List<HttpBinding> documentBindings = readErrorResponseBody(context, error, bindingIndex, isBodyParsed);
636+
List<HttpBinding> documentBindings = readErrorResponseBody(
637+
context, error, bindingIndex, this.isErrorCodeInBody);
628638
// Track all shapes bound to the document so their deserializers may be generated.
629639
documentBindings.forEach(binding -> {
630640
Shape target = model.expectShape(binding.getMember().getTarget());
@@ -912,10 +922,13 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
912922
* Writes the code that loads an {@code errorCode} String with the content used
913923
* to dispatch errors to specific serializers.
914924
*
915-
* <p>Three variables will be in scope:
925+
* <p>Two variables will be in scope:
916926
* <ul>
917-
* <li>{@code output}: a value of the HttpResponse type.</li>
918-
* <li>{@code data}: the contents of the response body.</li>
927+
* <li>{@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type.
928+
* {@code errorOutput} is a raw HttpResponse whereas {@code parsedOutput} is a HttpResponse type with
929+
* body parsed to JavaScript object.
930+
* The actual value available is determined by {@link #isErrorCodeInBody}
931+
* </li>
919932
* <li>{@code context}: the SerdeContext.</li>
920933
* </ul>
921934
*
@@ -929,14 +942,6 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
929942
*/
930943
protected abstract void writeErrorCodeParser(GenerationContext context);
931944

932-
/**
933-
* A boolean indicates whether body is collected and parsed in error code parser.
934-
* If so, each error shape deserializer should not parse body again.
935-
*
936-
* @return returns whether the error code exists in response body
937-
*/
938-
protected abstract boolean isErrorCodeInBody();
939-
940945
/**
941946
* Writes the code needed to deserialize the output document of a response.
942947
*

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public abstract class HttpRpcProtocolGenerator implements ProtocolGenerator {
3737
private final Set<Shape> serializingDocumentShapes = new TreeSet<>();
3838
private final Set<Shape> deserializingDocumentShapes = new TreeSet<>();
3939
private final Set<StructureShape> deserializingErrorShapes = new TreeSet<>();
40+
private final boolean isErrorCodeInBody;
41+
42+
/**
43+
* Creates a Http RPC protocol generator.
44+
*
45+
* @param isErrorCodeInBody A boolean indicates whether error code is located in error response body.
46+
*/
47+
public HttpRpcProtocolGenerator(boolean isErrorCodeInBody) {
48+
this.isErrorCodeInBody = isErrorCodeInBody;
49+
}
4050

4151
@Override
4252
public ApplicationProtocol getApplicationProtocol() {
@@ -254,7 +264,7 @@ private void generateOperationDeserializer(GenerationContext context, OperationS
254264

255265
// Write out the error deserialization dispatcher.
256266
Set<StructureShape> errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher(
257-
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody());
267+
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody);
258268
deserializingErrorShapes.addAll(errorShapes);
259269
}
260270

@@ -331,17 +341,6 @@ private void readResponseBody(GenerationContext context, OperationShape operatio
331341
*/
332342
protected abstract void writeErrorCodeParser(GenerationContext context);
333343

334-
/**
335-
* Indicates whether body is collected and parsed in error dispatcher.
336-
*
337-
* <p>If returns true, {@link #writeErrorCodeParser} will have {@code parsedOutput} in scope
338-
*
339-
* <P>If returns false, {@link #writeErrorCodeParser} will have {@code errorOutput} in scope
340-
*
341-
* @return returns whether the error code exists in response body
342-
*/
343-
protected abstract boolean isErrorCodeInBody();
344-
345344
/**
346345
* Writes the code needed to deserialize the output document of a response.
347346
*

0 commit comments

Comments
 (0)