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