@@ -119,8 +119,6 @@ public void generateSharedComponents(GenerationContext context) {
119
119
generateDocumentBodyShapeSerializers (context , serializingDocumentShapes );
120
120
generateDocumentBodyShapeDeserializers (context , deserializingDocumentShapes );
121
121
HttpProtocolGeneratorUtils .generateMetadataDeserializer (context , getApplicationProtocol ().getResponseType ());
122
- HttpProtocolGeneratorUtils .generateCollectBody (context );
123
- HttpProtocolGeneratorUtils .generateCollectBodyString (context );
124
122
}
125
123
126
124
/**
@@ -595,7 +593,7 @@ private void generateOperationDeserializer(
595
593
596
594
// Write out the error deserialization dispatcher.
597
595
Set <StructureShape > errorShapes = HttpProtocolGeneratorUtils .generateErrorDispatcher (
598
- context , operation , responseType , this ::writeErrorCodeParser , this . isErrorCodeInBody () );
596
+ context , operation , responseType , this ::writeErrorCodeParser );
599
597
deserializingErrorShapes .addAll (errorShapes );
600
598
}
601
599
@@ -607,13 +605,11 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
607
605
Symbol errorSymbol = symbolProvider .toSymbol (error );
608
606
String errorDeserMethodName = ProtocolGenerator .getDeserFunctionName (errorSymbol ,
609
607
context .getProtocolName ()) + "Response" ;
610
- boolean isBodyParsed = this .isErrorCodeInBody ();
611
608
612
609
writer .openBlock ("const $L = async (\n "
613
- + " $L : any,\n "
610
+ + " output : any,\n "
614
611
+ " context: __SerdeContext\n "
615
- + "): Promise<$T> => {" , "};" ,
616
- errorDeserMethodName , isBodyParsed ? "parsedOutput" : "output" , errorSymbol , () -> {
612
+ + "): Promise<$T> => {" , "};" , errorDeserMethodName , errorSymbol , () -> {
617
613
writer .openBlock ("const contents: $T = {" , "};" , errorSymbol , () -> {
618
614
writer .write ("__type: $S," , error .getId ().getName ());
619
615
writer .write ("$$fault: $S," , error .getTrait (ErrorTrait .class ).get ().getValue ());
@@ -624,7 +620,7 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
624
620
});
625
621
626
622
readHeaders (context , error , bindingIndex );
627
- List <HttpBinding > documentBindings = readErrorResponseBody (context , error , bindingIndex , isBodyParsed );
623
+ List <HttpBinding > documentBindings = readResponseBody (context , error , bindingIndex );
628
624
// Track all shapes bound to the document so their deserializers may be generated.
629
625
documentBindings .forEach (binding -> {
630
626
Shape target = model .expectShape (binding .getMember ().getTarget ());
@@ -636,23 +632,6 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
636
632
writer .write ("" );
637
633
}
638
634
639
- private List <HttpBinding > readErrorResponseBody (
640
- GenerationContext context ,
641
- Shape error ,
642
- HttpBindingIndex bindingIndex ,
643
- boolean isBodyParsed
644
- ) {
645
- TypeScriptWriter writer = context .getWriter ();
646
- if (isBodyParsed ) {
647
- // Body is already parsed in error dispatcher, simply assign body to data.
648
- writer .write ("const data: any = output.body;" );
649
- return ListUtils .of ();
650
- } else {
651
- // Deserialize response body just like in normal response.
652
- return readResponseBody (context , error , bindingIndex );
653
- }
654
- }
655
-
656
635
private void readHeaders (
657
636
GenerationContext context ,
658
637
Shape operationOrError ,
@@ -711,44 +690,42 @@ private List<HttpBinding> readResponseBody(
711
690
documentBindings .sort (Comparator .comparing (HttpBinding ::getMemberName ));
712
691
List <HttpBinding > payloadBindings = bindingIndex .getResponseBindings (operationOrError , Location .PAYLOAD );
713
692
714
- OperationIndex operationIndex = context .getModel ().getKnowledge (OperationIndex .class );
715
- StructureShape operationOutputOrError = operationOrError .asStructureShape ()
716
- .orElseGet (() -> operationIndex .getOutput (operationOrError ).orElse (null ));
717
- boolean hasStreamingComponent = Optional .ofNullable (operationOutputOrError )
718
- .map (structure -> structure .getAllMembers ().values ().stream ()
719
- .anyMatch (memberShape -> memberShape .hasTrait (StreamingTrait .class )))
720
- .orElse (false );
721
-
722
693
if (!documentBindings .isEmpty ()) {
723
- // If response has document binding, the body can be parsed to JavaScript object.
724
- writer .write ("const data: any = await parseBody(output.body, context);" );
694
+ readReponseBodyData (context , operationOrError );
725
695
deserializeOutputDocument (context , operationOrError , documentBindings );
726
696
return documentBindings ;
727
697
}
728
698
if (!payloadBindings .isEmpty ()) {
699
+ readReponseBodyData (context , operationOrError );
729
700
// There can only be one payload binding.
730
701
HttpBinding binding = payloadBindings .get (0 );
731
702
Shape target = context .getModel ().expectShape (binding .getMember ().getTarget ());
732
- if (hasStreamingComponent ) {
733
- // If payload is streaming, return raw low-level stream directly.
734
- writer .write ("const data: any = output.body;" );
735
- } else if (target instanceof BlobShape ) {
736
- // If payload is blob, only need to collect stream to binary data(Uint8Array).
737
- writer .write ("const data: any = await collectBody(output.body, context);" );
738
- } else if (target instanceof StructureShape || target instanceof UnionShape ) {
739
- // If body is Structure or Union, they we need to parse the string into JavaScript object.
740
- writer .write ("const data: any = await parseBody(output.body, context);" );
741
- } else {
742
- // If payload is string, we need to collect body and encode binary to string.
743
- writer .write ("const data: any = await collectBodyString(output.body, context);" );
744
- }
745
703
writer .write ("contents.$L = $L;" , binding .getMemberName (), getOutputValue (context ,
746
704
Location .PAYLOAD , "data" , binding .getMember (), target ));
747
705
return payloadBindings ;
748
706
}
749
707
return ListUtils .of ();
750
708
}
751
709
710
+ private void readReponseBodyData (GenerationContext context , Shape operationOrError ) {
711
+ TypeScriptWriter writer = context .getWriter ();
712
+ // Prepare response body for deserializing.
713
+ OperationIndex operationIndex = context .getModel ().getKnowledge (OperationIndex .class );
714
+ StructureShape operationOutputOrError = operationOrError .asStructureShape ()
715
+ .orElseGet (() -> operationIndex .getOutput (operationOrError ).orElse (null ));
716
+ boolean hasStreamingComponent = Optional .ofNullable (operationOutputOrError )
717
+ .map (structure -> structure .getAllMembers ().values ().stream ()
718
+ .anyMatch (memberShape -> memberShape .hasTrait (StreamingTrait .class )))
719
+ .orElse (false );
720
+ if (hasStreamingComponent ) {
721
+ // For operations with streaming output or errors with streaming body we keep the body intact.
722
+ writer .write ("const data: any = output.body;" );
723
+ } else {
724
+ // Otherwise, we collect the response body to structured object with parseBody().
725
+ writer .write ("const data: any = await parseBody(output.body, context);" );
726
+ }
727
+ }
728
+
752
729
/**
753
730
* Given context and a source of data, generate an output value provider for the
754
731
* shape. This may use native types (like generating a Date for timestamps,)
@@ -929,14 +906,6 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
929
906
*/
930
907
protected abstract void writeErrorCodeParser (GenerationContext context );
931
908
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
-
940
909
/**
941
910
* Writes the code needed to deserialize the output document of a response.
942
911
*
0 commit comments