@@ -120,6 +120,8 @@ public void generateSharedComponents(GenerationContext context) {
120
120
generateDocumentBodyShapeSerializers (context , serializingDocumentShapes );
121
121
generateDocumentBodyShapeDeserializers (context , deserializingDocumentShapes );
122
122
HttpProtocolGeneratorUtils .generateMetadataDeserializer (context , getApplicationProtocol ().getResponseType ());
123
+ HttpProtocolGeneratorUtils .generateCollectBody (context );
124
+ HttpProtocolGeneratorUtils .generateCollectBodyString (context );
123
125
}
124
126
125
127
/**
@@ -690,43 +692,44 @@ private List<HttpBinding> readResponseBody(
690
692
List <HttpBinding > documentBindings = bindingIndex .getResponseBindings (operationOrError , Location .DOCUMENT );
691
693
documentBindings .sort (Comparator .comparing (HttpBinding ::getMemberName ));
692
694
List <HttpBinding > payloadBindings = bindingIndex .getResponseBindings (operationOrError , Location .PAYLOAD );
695
+ OperationIndex operationIndex = context .getModel ().getKnowledge (OperationIndex .class );
696
+ StructureShape operationOutputOrError = operationOrError .asStructureShape ()
697
+ .orElseGet (() -> operationIndex .getOutput (operationOrError ).orElse (null ));
698
+ boolean hasStreamingComponent = Optional .ofNullable (operationOutputOrError )
699
+ .map (structure -> structure .getAllMembers ().values ().stream ()
700
+ .anyMatch (memberShape -> memberShape .hasTrait (StreamingTrait .class )))
701
+ .orElse (false );
693
702
694
703
if (!documentBindings .isEmpty ()) {
695
- readReponseBodyData ( context , operationOrError );
704
+ writer . write ( "const data: any = await parseBody(output.body, context);" );
696
705
deserializeOutputDocument (context , operationOrError , documentBindings );
697
706
return documentBindings ;
698
707
}
699
708
if (!payloadBindings .isEmpty ()) {
700
- readReponseBodyData (context , operationOrError );
701
709
// There can only be one payload binding.
702
710
HttpBinding binding = payloadBindings .get (0 );
703
711
Shape target = context .getModel ().expectShape (binding .getMember ().getTarget ());
712
+ if (hasStreamingComponent ) {
713
+ // If payload is streaming, return raw low-level stream directly.
714
+ writer .write ("const data: any = output.body;" );
715
+ } else if (target instanceof BlobShape ) {
716
+ // If payload is blob, only need to collect stream to binary data(Uint8Array).
717
+ writer .write ("const data: any = await collectBody(output.body, context);" );
718
+ } else if (target instanceof CollectionShape || target instanceof StructureShape ) {
719
+ // If body is Collection or Structure, they we need to parse the string into JavaScript object.
720
+ writer .write ("const data: any = await parseBody(output.body, context);" );
721
+ } else {
722
+ // If payload is other scalar types(not Collection or Structure), because payload will be values in
723
+ // string instead of valid JSON or XML. So we need to collect body and encode binary to string.
724
+ writer .write ("const data: any = await collectBodyString(output.body, context);" );
725
+ }
704
726
writer .write ("contents.$L = $L;" , binding .getMemberName (), getOutputValue (context ,
705
727
Location .PAYLOAD , "data" , binding .getMember (), target ));
706
728
return payloadBindings ;
707
729
}
708
730
return ListUtils .of ();
709
731
}
710
732
711
- private void readReponseBodyData (GenerationContext context , Shape operationOrError ) {
712
- TypeScriptWriter writer = context .getWriter ();
713
- // Prepare response body for deserializing.
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
- if (hasStreamingComponent || operationOrError .getType ().equals (ShapeType .STRUCTURE )) {
722
- // For operations with streaming output or errors with streaming body we keep the body intact.
723
- writer .write ("const data: any = output.body;" );
724
- } else {
725
- // Otherwise, we collect the response body to structured object with parseBody().
726
- writer .write ("const data: any = await parseBody(output.body, context);" );
727
- }
728
- }
729
-
730
733
/**
731
734
* Given context and a source of data, generate an output value provider for the
732
735
* shape. This may use native types (like generating a Date for timestamps,)
0 commit comments