Skip to content

Commit 7f890c1

Browse files
committed
fix: Moved content header checks to client utils
1 parent b7eee79 commit 7f890c1

207 files changed

Lines changed: 2484 additions & 5314 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,34 @@ using System.Net.Http.Headers;
407407
throw new JsonException("The specified discriminator was not found.");
408408
}
409409

410+
/// <summary>
411+
/// Determines if the provided header is a content header
412+
/// </summary>
413+
/// <param name="header">The header to check</param>
414+
/// <returns>True if a content header; False otherwise</returns>
415+
public static bool IsContentHeader(string header)
416+
{
417+
return ContentHeaders.Contains(header.ToLowerInvariant());
418+
}
419+
420+
/// <summary>
421+
/// The collection of content headers as per
422+
/// https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.headers
423+
/// </summary>
424+
private static readonly string[] ContentHeaders =
425+
[
426+
"allow",
427+
"content-encoding",
428+
"content-language",
429+
"content-length",
430+
"content-location",
431+
"content-md5",
432+
"content-range",
433+
"content-type",
434+
"expires",
435+
"last-modified"
436+
];
437+
410438
/// <summary>
411439
/// The base path of the API
412440
/// </summary>

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

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,6 @@ namespace {{packageName}}.{{apiPackage}}
160160
{
161161
private JsonSerializerOptions _jsonSerializerOptions;
162162
163-
private readonly string[] _contentHeaders =
164-
[
165-
"allow",
166-
"content-encoding",
167-
"content-language",
168-
"content-length",
169-
"content-location",
170-
"content-md5",
171-
"content-range",
172-
"content-type",
173-
"expires",
174-
"last-modified",
175-
"extension-header"
176-
];
177-
178163
/// <summary>
179164
/// The logger factory
180165
/// </summary>
@@ -559,38 +544,16 @@ namespace {{packageName}}.{{apiPackage}}
559544
{{/formParams}}
560545
{{#bodyParam}}
561546
{{#required}}
562-
if (({{paramName}}{{^required}}.Value{{/required}} as object) is System.IO.Stream stream)
563-
{
564-
httpRequestMessageLocalVar.Content = new StreamContent(stream);
565-
}
566-
{{#isFile}}
567-
else if (({{paramName}}{{^required}}.Value{{/required}} as object) is {{packageName}}.{{clientPackage}}.FileParameter fileParameterLocalVar)
568-
{
569-
httpRequestMessageLocalVar.Content = new StreamContent(fileParameterLocalVar.Content);
570-
}
571-
{{/isFile}}
572-
else
573-
{
574-
httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize({{paramName}}{{^required}}.Value{{/required}}, _jsonSerializerOptions));
575-
}
547+
httpRequestMessageLocalVar.Content = ({{paramName}}{{^required}}.Value{{/required}} as object) is {{packageName}}.{{clientPackage}}.FileParameter fileParameterLocalVar
548+
? httpRequestMessageLocalVar.Content = new StreamContent(fileParameterLocalVar.Content)
549+
: httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize({{paramName}}{{^required}}.Value{{/required}}, _jsonSerializerOptions));
576550
{{/required}}
577551
{{^required}}
578552
if ({{paramName}}.IsSet)
579553
{
580-
if (({{paramName}}{{^required}}.Value{{/required}} as object) is System.IO.Stream stream)
581-
{
582-
httpRequestMessageLocalVar.Content = new StreamContent(stream);
583-
}
584-
{{#isFile}}
585-
else if (({{paramName}}{{^required}}.Value{{/required}} as object) is {{packageName}}.{{clientPackage}}.FileParameter fileParameterLocalVar)
586-
{
587-
httpRequestMessageLocalVar.Content = new StreamContent(fileParameterLocalVar.Content);
588-
}
589-
{{/isFile}}
590-
else
591-
{
592-
httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize({{paramName}}{{^required}}.Value{{/required}}, _jsonSerializerOptions));
593-
}
554+
httpRequestMessageLocalVar.Content = ({{paramName}}{{^required}}.Value{{/required}} as object) is {{packageName}}.{{clientPackage}}.FileParameter fileParameterLocalVar
555+
? httpRequestMessageLocalVar.Content = new StreamContent(fileParameterLocalVar.Content)
556+
: httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize({{paramName}}{{^required}}.Value{{/required}}, _jsonSerializerOptions));
594557
}
595558
{{/required}}
596559

@@ -599,7 +562,7 @@ namespace {{packageName}}.{{apiPackage}}
599562
{{#constantParams}}
600563
{{#isHeaderParam}}
601564
// Set client side default value of Header Param "{{baseName}}".
602-
if (_contentHeaders.Contains("{{baseName}}".ToLowerInvariant()))
565+
if (ClientUtils.IsContentHeader("{{baseName}}"))
603566
{
604567
httpRequestMessageLocalVar.Content.Headers.Add("{{baseName}}", ClientUtils.ParameterToString({{#_enum}}"{{{.}}}"{{/_enum}})); // Constant header parameter
605568
}
@@ -613,7 +576,7 @@ namespace {{packageName}}.{{apiPackage}}
613576
{{#headerParams}}
614577
{{#required}}
615578
// Set client side default value of Header Param "{{baseName}}".
616-
if (_contentHeaders.Contains("{{baseName}}".ToLowerInvariant()))
579+
if (ClientUtils.IsContentHeader("{{baseName}}"))
617580
{
618581
httpRequestMessageLocalVar.Content.Headers.Add("{{baseName}}", ClientUtils.ParameterToString({{paramName}}));
619582
}

samples/client/petstore/csharp/generichost/latest/ComposedEnum/src/Org.OpenAPITools/Client/ClientUtils.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,34 @@ public static bool IsJsonMime(string mime)
307307
throw new JsonException("The specified discriminator was not found.");
308308
}
309309

310+
/// <summary>
311+
/// Determines if the provided header is a content header
312+
/// </summary>
313+
/// <param name="header">The header to check</param>
314+
/// <returns>True if a content header; False otherwise</returns>
315+
public static bool IsContentHeader(string header)
316+
{
317+
return ContentHeaders.Contains(header.ToLowerInvariant());
318+
}
319+
320+
/// <summary>
321+
/// The collection of content headers as per
322+
/// https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.headers
323+
/// </summary>
324+
private static readonly string[] ContentHeaders =
325+
[
326+
"allow",
327+
"content-encoding",
328+
"content-language",
329+
"content-length",
330+
"content-location",
331+
"content-md5",
332+
"content-range",
333+
"content-type",
334+
"expires",
335+
"last-modified"
336+
];
337+
310338
/// <summary>
311339
/// The base path of the API
312340
/// </summary>

samples/client/petstore/csharp/generichost/latest/HelloWorld/src/Org.OpenAPITools/Api/DefaultApi.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,6 @@ public sealed partial class DefaultApi : IDefaultApi
112112
{
113113
private JsonSerializerOptions _jsonSerializerOptions;
114114

115-
private readonly string[] _contentHeaders =
116-
[
117-
"allow",
118-
"content-encoding",
119-
"content-language",
120-
"content-length",
121-
"content-location",
122-
"content-md5",
123-
"content-range",
124-
"content-type",
125-
"expires",
126-
"last-modified",
127-
"extension-header"
128-
];
129-
130115
/// <summary>
131116
/// The logger factory
132117
/// </summary>
@@ -265,14 +250,9 @@ public async Task<IHelloWorldPostApiResponse> HelloWorldPostAsync(Option<HelloWo
265250

266251
if (helloWorldPostRequest.IsSet)
267252
{
268-
if ((helloWorldPostRequest.Value as object) is System.IO.Stream stream)
269-
{
270-
httpRequestMessageLocalVar.Content = new StreamContent(stream);
271-
}
272-
else
273-
{
274-
httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(helloWorldPostRequest.Value, _jsonSerializerOptions));
275-
}
253+
httpRequestMessageLocalVar.Content = (helloWorldPostRequest.Value as object) is Org.OpenAPITools.Client.FileParameter fileParameterLocalVar
254+
? httpRequestMessageLocalVar.Content = new StreamContent(fileParameterLocalVar.Content)
255+
: httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(helloWorldPostRequest.Value, _jsonSerializerOptions));
276256
}
277257

278258
httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

samples/client/petstore/csharp/generichost/latest/HelloWorld/src/Org.OpenAPITools/Client/ClientUtils.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,34 @@ public static bool IsJsonMime(string mime)
301301
throw new JsonException("The specified discriminator was not found.");
302302
}
303303

304+
/// <summary>
305+
/// Determines if the provided header is a content header
306+
/// </summary>
307+
/// <param name="header">The header to check</param>
308+
/// <returns>True if a content header; False otherwise</returns>
309+
public static bool IsContentHeader(string header)
310+
{
311+
return ContentHeaders.Contains(header.ToLowerInvariant());
312+
}
313+
314+
/// <summary>
315+
/// The collection of content headers as per
316+
/// https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.headers
317+
/// </summary>
318+
private static readonly string[] ContentHeaders =
319+
[
320+
"allow",
321+
"content-encoding",
322+
"content-language",
323+
"content-length",
324+
"content-location",
325+
"content-md5",
326+
"content-range",
327+
"content-type",
328+
"expires",
329+
"last-modified"
330+
];
331+
304332
/// <summary>
305333
/// The base path of the API
306334
/// </summary>

samples/client/petstore/csharp/generichost/latest/InlineEnumAnyOf/src/Org.OpenAPITools/Api/DefaultApi.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,6 @@ public sealed partial class DefaultApi : IDefaultApi
106106
{
107107
private JsonSerializerOptions _jsonSerializerOptions;
108108

109-
private readonly string[] _contentHeaders =
110-
[
111-
"allow",
112-
"content-encoding",
113-
"content-language",
114-
"content-length",
115-
"content-location",
116-
"content-md5",
117-
"content-range",
118-
"content-type",
119-
"expires",
120-
"last-modified",
121-
"extension-header"
122-
];
123-
124109
/// <summary>
125110
/// The logger factory
126111
/// </summary>

samples/client/petstore/csharp/generichost/latest/InlineEnumAnyOf/src/Org.OpenAPITools/Client/ClientUtils.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,34 @@ public static bool IsJsonMime(string mime)
303303
throw new JsonException("The specified discriminator was not found.");
304304
}
305305

306+
/// <summary>
307+
/// Determines if the provided header is a content header
308+
/// </summary>
309+
/// <param name="header">The header to check</param>
310+
/// <returns>True if a content header; False otherwise</returns>
311+
public static bool IsContentHeader(string header)
312+
{
313+
return ContentHeaders.Contains(header.ToLowerInvariant());
314+
}
315+
316+
/// <summary>
317+
/// The collection of content headers as per
318+
/// https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.headers
319+
/// </summary>
320+
private static readonly string[] ContentHeaders =
321+
[
322+
"allow",
323+
"content-encoding",
324+
"content-language",
325+
"content-length",
326+
"content-location",
327+
"content-md5",
328+
"content-range",
329+
"content-type",
330+
"expires",
331+
"last-modified"
332+
];
333+
306334
/// <summary>
307335
/// The base path of the API
308336
/// </summary>

samples/client/petstore/csharp/generichost/latest/OneOfList/src/Org.OpenAPITools/Api/DefaultApi.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,6 @@ public sealed partial class DefaultApi : IDefaultApi
106106
{
107107
private JsonSerializerOptions _jsonSerializerOptions;
108108

109-
private readonly string[] _contentHeaders =
110-
[
111-
"allow",
112-
"content-encoding",
113-
"content-language",
114-
"content-length",
115-
"content-location",
116-
"content-md5",
117-
"content-range",
118-
"content-type",
119-
"expires",
120-
"last-modified",
121-
"extension-header"
122-
];
123-
124109
/// <summary>
125110
/// The logger factory
126111
/// </summary>
@@ -246,14 +231,9 @@ public async Task<IOneOfArrayApiResponse> OneOfArrayAsync(Option<OneOfArrayReque
246231

247232
if (oneOfArrayRequest.IsSet)
248233
{
249-
if ((oneOfArrayRequest.Value as object) is System.IO.Stream stream)
250-
{
251-
httpRequestMessageLocalVar.Content = new StreamContent(stream);
252-
}
253-
else
254-
{
255-
httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(oneOfArrayRequest.Value, _jsonSerializerOptions));
256-
}
234+
httpRequestMessageLocalVar.Content = (oneOfArrayRequest.Value as object) is Org.OpenAPITools.Client.FileParameter fileParameterLocalVar
235+
? httpRequestMessageLocalVar.Content = new StreamContent(fileParameterLocalVar.Content)
236+
: httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(oneOfArrayRequest.Value, _jsonSerializerOptions));
257237
}
258238

259239
httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

samples/client/petstore/csharp/generichost/latest/OneOfList/src/Org.OpenAPITools/Client/ClientUtils.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,34 @@ public static bool IsJsonMime(string mime)
301301
throw new JsonException("The specified discriminator was not found.");
302302
}
303303

304+
/// <summary>
305+
/// Determines if the provided header is a content header
306+
/// </summary>
307+
/// <param name="header">The header to check</param>
308+
/// <returns>True if a content header; False otherwise</returns>
309+
public static bool IsContentHeader(string header)
310+
{
311+
return ContentHeaders.Contains(header.ToLowerInvariant());
312+
}
313+
314+
/// <summary>
315+
/// The collection of content headers as per
316+
/// https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.headers
317+
/// </summary>
318+
private static readonly string[] ContentHeaders =
319+
[
320+
"allow",
321+
"content-encoding",
322+
"content-language",
323+
"content-length",
324+
"content-location",
325+
"content-md5",
326+
"content-range",
327+
"content-type",
328+
"expires",
329+
"last-modified"
330+
];
331+
304332
/// <summary>
305333
/// The base path of the API
306334
/// </summary>

samples/client/petstore/csharp/generichost/latest/Tags/src/Org.OpenAPITools/Api/APIKEYSApi.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,6 @@ public sealed partial class APIKEYSApi : IAPIKEYSApi
106106
{
107107
private JsonSerializerOptions _jsonSerializerOptions;
108108

109-
private readonly string[] _contentHeaders =
110-
[
111-
"allow",
112-
"content-encoding",
113-
"content-language",
114-
"content-length",
115-
"content-location",
116-
"content-md5",
117-
"content-range",
118-
"content-type",
119-
"expires",
120-
"last-modified",
121-
"extension-header"
122-
];
123-
124109
/// <summary>
125110
/// The logger factory
126111
/// </summary>

0 commit comments

Comments
 (0)