23
23
import java .util .List ;
24
24
import java .util .Locale ;
25
25
import java .util .Map ;
26
+ import java .util .Optional ;
26
27
import java .util .Set ;
27
28
import java .util .TreeMap ;
28
29
import java .util .TreeSet ;
53
54
import software .amazon .smithy .model .shapes .StringShape ;
54
55
import software .amazon .smithy .model .shapes .StructureShape ;
55
56
import software .amazon .smithy .model .shapes .TimestampShape ;
56
- import software .amazon .smithy .model .shapes .ToShapeId ;
57
57
import software .amazon .smithy .model .shapes .UnionShape ;
58
58
import software .amazon .smithy .model .traits .EndpointTrait ;
59
59
import software .amazon .smithy .model .traits .ErrorTrait ;
@@ -708,8 +708,7 @@ private void writeRequestHeaders(
708
708
// Headers are always present either from the default document or the payload.
709
709
writer .openBlock ("const headers: any = {" , "};" , () -> {
710
710
// Only set the content type if one can be determined.
711
- bindingIndex .determineRequestContentType (operation , getDocumentContentType ()).ifPresent (contentType ->
712
- writer .write ("'content-type': $S," , contentType ));
711
+ writeContentTypeHeader (context , operation , true );
713
712
writeDefaultHeaders (context , operation , true );
714
713
715
714
operation .getInput ().ifPresent (outputId -> {
@@ -759,17 +758,15 @@ private void writePrefixHeaders(GenerationContext context, HttpBinding binding)
759
758
760
759
private void writeResponseHeaders (
761
760
GenerationContext context ,
762
- ToShapeId operationOrError ,
761
+ Shape operationOrError ,
763
762
HttpBindingIndex bindingIndex ,
764
763
Runnable injectExtraHeaders
765
764
) {
766
765
TypeScriptWriter writer = context .getWriter ();
767
766
768
767
// Headers are always present either from the default document or the payload.
769
768
writer .openBlock ("const headers: any = {" , "};" , () -> {
770
- // Only set the content type if one can be determined.
771
- bindingIndex .determineResponseContentType (operationOrError , getDocumentContentType ())
772
- .ifPresent (contentType -> writer .write ("'content-type': $S," , contentType ));
769
+ writeContentTypeHeader (context , operationOrError , false );
773
770
injectExtraHeaders .run ();
774
771
775
772
for (HttpBinding binding : bindingIndex .getResponseBindings (operationOrError , Location .HEADER )) {
@@ -783,31 +780,66 @@ private void writeResponseHeaders(
783
780
});
784
781
}
785
782
783
+ private void writeContentTypeHeader (GenerationContext context , Shape operationOrError , boolean isInput ) {
784
+ HttpBindingIndex bindingIndex = HttpBindingIndex .of (context .getModel ());
785
+ Optional <String > optionalContentType ;
786
+ if (isInput ) {
787
+ optionalContentType = bindingIndex .determineRequestContentType (operationOrError , getDocumentContentType ());
788
+ } else {
789
+ optionalContentType = bindingIndex .determineResponseContentType (operationOrError , getDocumentContentType ());
790
+ }
791
+ // If we need to write a default body then it needs a content type.
792
+ if (!optionalContentType .isPresent () && shouldWriteDefaultBody (context , operationOrError , isInput )) {
793
+ optionalContentType = Optional .of (getDocumentContentType ());
794
+ }
795
+ optionalContentType .ifPresent (contentType -> context .getWriter ().write ("'content-type': $S," , contentType ));
796
+ }
797
+
786
798
private List <HttpBinding > writeRequestBody (
787
799
GenerationContext context ,
788
800
OperationShape operation ,
789
801
HttpBindingIndex bindingIndex
790
802
) {
791
803
List <HttpBinding > payloadBindings = bindingIndex .getRequestBindings (operation , Location .PAYLOAD );
792
804
List <HttpBinding > documentBindings = bindingIndex .getRequestBindings (operation , Location .DOCUMENT );
793
- boolean shouldWriteDefaultBody = bindingIndex . getRequestBindings ( operation ). isEmpty ( );
805
+ boolean shouldWriteDefaultBody = shouldWriteDefaultBody ( context , operation , true );
794
806
return writeBody (context , operation , payloadBindings , documentBindings , shouldWriteDefaultBody , true );
795
807
}
796
808
797
809
private List <HttpBinding > writeResponseBody (
798
810
GenerationContext context ,
799
- ToShapeId operationOrError ,
811
+ Shape operationOrError ,
800
812
HttpBindingIndex bindingIndex
801
813
) {
802
814
// We just make one up here since it's not actually used by consumers.
803
815
// TODO: remove the need for this at all
804
816
OperationShape operation = OperationShape .builder ().id ("ns.foo#bar" ).build ();
805
817
List <HttpBinding > payloadBindings = bindingIndex .getResponseBindings (operationOrError , Location .PAYLOAD );
806
818
List <HttpBinding > documentBindings = bindingIndex .getResponseBindings (operationOrError , Location .DOCUMENT );
807
- boolean shouldWriteDefaultBody = bindingIndex . getResponseBindings ( operationOrError ). isEmpty ( );
819
+ boolean shouldWriteDefaultBody = shouldWriteDefaultBody ( context , operationOrError , false );
808
820
return writeBody (context , operation , payloadBindings , documentBindings , shouldWriteDefaultBody , false );
809
821
}
810
822
823
+ /**
824
+ * Given a context, operation/error, and whether the shape is being serialized for input or output,
825
+ * should a default body be written. By default no body will be written if there are no members bound
826
+ * to the input/output/error.
827
+ *
828
+ * @param context The generation context.
829
+ * @param operationOrError The operation or error being serialized.
830
+ * @param isInput Whether the body being generated is for an input or an output.
831
+ *
832
+ * @return True if a default body should be generated.
833
+ */
834
+ protected boolean shouldWriteDefaultBody (GenerationContext context , Shape operationOrError , boolean isInput ) {
835
+ HttpBindingIndex bindingIndex = HttpBindingIndex .of (context .getModel ());
836
+ if (isInput ) {
837
+ return bindingIndex .getRequestBindings (operationOrError ).isEmpty ();
838
+ } else {
839
+ return bindingIndex .getResponseBindings (operationOrError ).isEmpty ();
840
+ }
841
+ }
842
+
811
843
private List <HttpBinding > writeBody (
812
844
GenerationContext context ,
813
845
OperationShape operation ,
0 commit comments