@@ -352,40 +352,46 @@ private void writeHeaders(
352352 SymbolProvider symbolProvider = context .getSymbolProvider ();
353353
354354 // Headers are always present either from the default document or the payload.
355- writer .write ("const headers: any = {};" );
356- writer .write ("headers['Content-Type'] = $S;" , bindingIndex .determineRequestContentType (
357- operation , getDocumentContentType ()));
358- writeDefaultHeaders (context , operation );
355+ writer .openBlock ("const headers: any = {" , "};" ,
356+ () -> {
357+ writer .write ("'Content-Type': $S," , bindingIndex .determineRequestContentType (
358+ operation , getDocumentContentType ()));
359+ writeDefaultHeaders (context , operation );
359360
360- operation .getInput ().ifPresent (outputId -> {
361- Model model = context .getModel ();
362- for (HttpBinding binding : bindingIndex .getRequestBindings (operation , Location .HEADER )) {
363- String memberLocation = "input." + symbolProvider .toMemberName (binding .getMember ());
364- writer .openBlock ("if (isSerializableHeaderValue($1L)) {" , "}" , memberLocation , () -> {
365- Shape target = model .expectShape (binding .getMember ().getTarget ());
366- String headerValue = getInputValue (context , binding .getLocation (), memberLocation + "!" ,
367- binding .getMember (), target );
368- writer .write ("headers[$S] = $L;" , binding .getLocationName (), headerValue );
369- });
370- }
371-
372- // Handle assembling prefix headers.
373- for (HttpBinding binding : bindingIndex .getRequestBindings (operation , Location .PREFIX_HEADERS )) {
374- String memberLocation = "input." + symbolProvider .toMemberName (binding .getMember ());
375- writer .openBlock ("if ($L !== undefined) {" , "}" , memberLocation , () -> {
376- MapShape prefixMap = model .expectShape (binding .getMember ().getTarget ()).asMapShape ().get ();
377- Shape target = model .expectShape (prefixMap .getValue ().getTarget ());
378- // Iterate through each entry in the member.
379- writer .openBlock ("Object.keys($L).forEach(suffix => {" , "});" , memberLocation , () -> {
380- // Use a ! since we already validated the input member is defined above.
381- String headerValue = getInputValue (context , binding .getLocation (),
382- memberLocation + "![suffix]" , binding .getMember (), target );
383- // Append the suffix to the defined prefix and serialize the value in to that key.
384- writer .write ("headers[$S + suffix] = $L;" , binding .getLocationName (), headerValue );
385- });
361+ operation .getInput ().ifPresent (outputId -> {
362+ Model model = context .getModel ();
363+ for (HttpBinding binding : bindingIndex .getRequestBindings (operation , Location .HEADER )) {
364+ String memberLocation = "input." + symbolProvider .toMemberName (binding .getMember ());
365+ Shape target = model .expectShape (binding .getMember ().getTarget ());
366+ String headerValue = getInputValue (context , binding .getLocation (), memberLocation + "!" ,
367+ binding .getMember (), target );
368+ writer .write ("...isSerializableHeaderValue($L) && { $S: $L }," ,
369+ memberLocation , binding .getLocationName (), headerValue );
370+ }
371+
372+ // Handle assembling prefix headers.
373+ for (HttpBinding binding : bindingIndex .getRequestBindings (operation , Location .PREFIX_HEADERS )) {
374+ String memberLocation = "input." + symbolProvider .toMemberName (binding .getMember ());
375+ MapShape prefixMap = model .expectShape (binding .getMember ().getTarget ()).asMapShape ().get ();
376+ Shape target = model .expectShape (prefixMap .getValue ().getTarget ());
377+ // Iterate through each entry in the member.
378+ writer .openBlock ("...($1L !== undefined) && Object.keys($1L).reduce(" , ")," , memberLocation ,
379+ () -> {
380+ writer .openBlock ("(acc: any, suffix: string) => {" , "}, {}" ,
381+ () -> {
382+ // Use a ! since we already validated the input member is defined above.
383+ String headerValue = getInputValue (context , binding .getLocation (),
384+ memberLocation + "![suffix]" , binding .getMember (), target );
385+ // Append the prefix to key.
386+ writer .write ("acc[$S + suffix] = $L;" , binding .getLocationName (), headerValue );
387+ writer .write ("return acc;" );
388+ });
389+ }
390+ );
391+ }
386392 });
387393 }
388- } );
394+ );
389395 }
390396
391397 private List <HttpBinding > writeRequestBody (
@@ -601,7 +607,7 @@ private String getTimestampInputParam(
601607 * <p>For example:
602608 *
603609 * <pre>{@code
604- * headers['foo'] = " This is a custom header";
610+ * "foo": " This is a custom header",
605611 * }</pre>
606612 *
607613 * @param context The generation context.
0 commit comments