@@ -353,40 +353,49 @@ private void writeHeaders(
353
353
SymbolProvider symbolProvider = context .getSymbolProvider ();
354
354
355
355
// Headers are always present either from the default document or the payload.
356
- writer .write ("const headers: any = {};" );
357
- writer . write ( "headers['Content-Type'] = $S;" , bindingIndex . determineRequestContentType (
358
- operation , getDocumentContentType ()));
359
- writeDefaultHeaders ( context , operation );
360
-
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
- writer . openBlock ( "if (isSerializableHeaderValue($1L)) {" , "}" , memberLocation , () -> {
366
- Shape target = model .expectShape (binding .getMember ().getTarget ());
367
- String headerValue = getInputValue (context , binding .getLocation (), memberLocation + "!" ,
356
+ writer .openBlock ("const headers: any = {" , " };",
357
+ () -> {
358
+ writer . write ( "'Content-Type': $S," , bindingIndex . determineRequestContentType (
359
+ operation , getDocumentContentType ()) );
360
+ writeDefaultHeaders ( context , operation );
361
+
362
+ operation . getInput (). ifPresent ( outputId -> {
363
+ Model model = context . getModel ();
364
+ for ( HttpBinding binding : bindingIndex . getRequestBindings ( operation , Location . HEADER )) {
365
+ String memberLocation = "input." + symbolProvider . toMemberName ( binding . getMember ());
366
+ Shape target = model .expectShape (binding .getMember ().getTarget ());
367
+ String headerValue = getInputValue (context , binding .getLocation (), memberLocation + "!" ,
368
368
binding .getMember (), target );
369
- writer .write ("headers[$S] = $L;" , binding .getLocationName (), headerValue );
370
- });
371
- }
372
-
373
- // Handle assembling prefix headers.
374
- for (HttpBinding binding : bindingIndex .getRequestBindings (operation , Location .PREFIX_HEADERS )) {
375
- String memberLocation = "input." + symbolProvider .toMemberName (binding .getMember ());
376
- writer .openBlock ("if ($L !== undefined) {" , "}" , memberLocation , () -> {
377
- MapShape prefixMap = model .expectShape (binding .getMember ().getTarget ()).asMapShape ().get ();
378
- Shape target = model .expectShape (prefixMap .getValue ().getTarget ());
379
- // Iterate through each entry in the member.
380
- writer .openBlock ("Object.keys($L).forEach(suffix => {" , "});" , memberLocation , () -> {
381
- // Use a ! since we already validated the input member is defined above.
382
- String headerValue = getInputValue (context , binding .getLocation (),
383
- memberLocation + "![suffix]" , binding .getMember (), target );
384
- // Append the suffix to the defined prefix and serialize the value in to that key.
385
- writer .write ("headers[$S + suffix] = $L;" , binding .getLocationName (), headerValue );
386
- });
369
+ writer .write ("...isSerializableHeaderValue($L) && { $S: $L }," ,
370
+ memberLocation , binding .getLocationName (), headerValue );
371
+ }
372
+
373
+ // Handle assembling prefix headers.
374
+ for (HttpBinding binding : bindingIndex .getRequestBindings (operation , Location .PREFIX_HEADERS )) {
375
+ String memberLocation = "input." + symbolProvider .toMemberName (binding .getMember ());
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 (
380
+ "...($L !== undefined) && Object.keys($L).reduce(" ,
381
+ ")," ,
382
+ memberLocation , memberLocation ,
383
+ () -> {
384
+ writer .openBlock ("(acc: any, suffix: string) => {" , "}, {}" ,
385
+ () -> {
386
+ // Use a ! since we already validated the input member is defined above.
387
+ String headerValue = getInputValue (context , binding .getLocation (),
388
+ memberLocation + "![suffix]" , binding .getMember (), target );
389
+ // Append the prefix to key.
390
+ writer .write ("acc[$S + suffix] = $L;" , binding .getLocationName (), headerValue );
391
+ writer .write ("return acc;" );
392
+ });
393
+ }
394
+ );
395
+ }
387
396
});
388
397
}
389
- } );
398
+ );
390
399
}
391
400
392
401
private List <HttpBinding > writeRequestBody (
0 commit comments