Skip to content

Commit c56afb2

Browse files
committed
Add support for other Accept header values or full arrays when multiple options are present.
1 parent a98c339 commit c56afb2

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ClientUtils.mustache

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using {{packageName}}.{{modelPackage}};
1818
{{/-first}}
1919
{{/models}}
2020
using System.Runtime.CompilerServices;
21+
using System.Net.Http.Headers;
2122

2223
{{>Assembly}}namespace {{packageName}}.{{clientPackage}}
2324
{
@@ -311,6 +312,26 @@ using System.Runtime.CompilerServices;
311312
return string.Join(",", accepts);
312313
}
313314

315+
316+
317+
/// <summary>
318+
/// Select the Accept header's value from the given accepts array:
319+
/// if JSON exists in the given array, use it;
320+
/// otherwise use all of them.
321+
/// </summary>
322+
/// <param name="accepts">The accepts array to select from.</param>
323+
/// <returns>The Accept header values to use.</returns>
324+
public static IEnumerable<MediaTypeWithQualityHeaderValue> SelectHeaderAcceptArray(string[] accepts)
325+
{
326+
if (accepts.Length == 0)
327+
return null;
328+
329+
if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
330+
return [MediaTypeWithQualityHeaderValue.Parse("application/json")];
331+
332+
return accepts.Select(MediaTypeWithQualityHeaderValue.Parse);
333+
}
334+
314335
/// <summary>
315336
/// Provides a case-insensitive check that a provided content type is a known JSON-like content type.
316337
/// </summary>

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{{/nrt}}
99
using System;
1010
using System.Collections.Generic;
11+
using System.Collections.ObjectModel;
1112
{{#net80OrLater}}
1213
{{#lambda.uniqueLines}}
1314
{{#operations}}
@@ -20,6 +21,7 @@ using System.Linq;
2021
{{/lambda.uniqueLines}}
2122
{{/net80OrLater}}
2223
using System.Net;
24+
using System.IO;
2325
using System.Threading.Tasks;
2426
using Microsoft.Extensions.Logging;
2527
using System.Net.Http;
@@ -605,10 +607,10 @@ namespace {{packageName}}.{{apiPackage}}
605607
{{#produces}}
606608
{{#-first}}
607609

608-
string{{nrt?}} acceptLocalVar = ClientUtils.SelectHeaderAccept(acceptLocalVars);
610+
IEnumerable<MediaTypeWithQualityHeaderValue> acceptHeaderValuesLocalVar = ClientUtils.SelectHeaderAcceptArray(acceptLocalVars);
609611

610-
if (acceptLocalVar != null)
611-
httpRequestMessageLocalVar.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptLocalVar));
612+
foreach (var acceptLocalVar in acceptHeaderValuesLocalVar)
613+
httpRequestMessageLocalVar.Headers.Accept.Add(acceptLocalVar);
612614
{{/-first}}
613615
{{/produces}}
614616
{{#net60OrLater}}

0 commit comments

Comments
 (0)