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