Skip to content

Commit 9879fa5

Browse files
author
Bryan Aldrich
committed
alter the c# templates to allow net core and net framework targets to exist in the same output. (uses defines in the c# instead of directives in the mustache processor).
1 parent 423ba67 commit 9879fa5

File tree

10 files changed

+65
-30
lines changed

10 files changed

+65
-30
lines changed

modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ using System.Text;
1414
using System.Threading;
1515
using System.Text.RegularExpressions;
1616
using System.Threading.Tasks;
17-
{{^net60OrLater}}
17+
#if !NET6_0_OR_GREATER
1818
using System.Web;
19-
{{/net60OrLater}}
19+
#endif
2020
using Newtonsoft.Json;
2121
using Newtonsoft.Json.Serialization;
2222
using RestSharp;

modules/openapi-generator/src/main/resources/csharp/ApiClient.v790.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ using System.Text;
1414
using System.Threading;
1515
using System.Text.RegularExpressions;
1616
using System.Threading.Tasks;
17-
{{^net60OrLater}}
17+
#if !NET6_0_OR_GREATER
1818
using System.Web;
19-
{{/net60OrLater}}
19+
#endif
2020
using Newtonsoft.Json;
2121
using Newtonsoft.Json.Serialization;
2222
using RestSharp;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ namespace {{packageName}}.Client
114114
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
115115
// For example: 2009-06-15T13:45:30.0000000
116116
return dateTimeOffset.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
117-
{{#net60OrLater}}
117+
#if NET6_0_OR_GREATER
118118
if (obj is DateOnly dateOnly)
119119
// Return a formatted date string - Can be customized with Configuration.DateTimeFormat
120120
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
121121
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
122122
// For example: 2009-06-15
123123
return dateOnly.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
124-
{{/net60OrLater}}
124+
#endif
125125
if (obj is bool boolean)
126126
return boolean ? "true" : "false";
127127
if (obj is ICollection collection) {

modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ namespace {{packageName}}.Client
344344
/// <returns>ECDSA signature</returns>
345345
private string GetECDSASignature(byte[] dataToSign)
346346
{
347-
{{#net60OrLater}}
347+
#if NET6_0_OR_GREATER
348348
if (!File.Exists(KeyFilePath) && string.IsNullOrEmpty(KeyString))
349349
{
350350
throw new Exception("No API key has been provided.");
@@ -382,10 +382,10 @@ namespace {{packageName}}.Client
382382
var signedString = System.Convert.ToBase64String(derBytes);
383383

384384
return signedString;
385-
{{/net60OrLater}}
386-
{{^net60OrLater}}
385+
#endif
386+
#if !NET6_0_OR_GREATER
387387
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
388-
{{/net60OrLater}}
388+
#endif
389389
}
390390

391391
/// <summary>

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ApiResponse`1.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ namespace {{packageName}}.{{clientPackage}}
164164
/// </summary>
165165
/// <param name="result"></param>
166166
/// <returns></returns>
167-
bool Try{{.}}({{#net60OrLater}}[NotNullWhen(true)]{{/net60OrLater}}out TType{{nrt?}} result);
167+
bool Try{{.}}(
168+
#if NET6_0_OR_GREATER
169+
[NotNullWhen(true)]
170+
#endif
171+
out TType{{nrt?}} result);
168172
}
169173
{{/x-http-statuses-with-return}}
170174
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
// This logic may be modified with the AsModel.mustache template
22
return Is{{vendorExtensions.x-http-status}}
33
? System.Text.Json.JsonSerializer.Deserialize<{{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}>(RawContent, _jsonSerializerOptions)
4-
: {{#net60OrLater}}null{{/net60OrLater}}{{^net60OrLater}}default{{/net60OrLater}};
4+
:
5+
#if NET6_0_OR_GREATER
6+
null
7+
#else
8+
default
9+
#endif;

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,15 @@ using System.Runtime.CompilerServices;
107107
/// <param name="options"></param>
108108
/// <param name="result"></param>
109109
/// <returns></returns>
110-
public static bool TryDeserialize<T>(string json, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
110+
public static bool TryDeserialize<T>(string json, JsonSerializerOptions options,
111+
#if NET6_0_OR_GREATER
112+
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
113+
#endif
114+
out T{{#nrt}}
115+
#if NET6_0_OR_GREATER
116+
?
117+
#endif
118+
{{/nrt}} result)
111119
{
112120
try
113121
{
@@ -129,7 +137,15 @@ using System.Runtime.CompilerServices;
129137
/// <param name="options"></param>
130138
/// <param name="result"></param>
131139
/// <returns></returns>
132-
public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
140+
public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOptions options,
141+
#if NET6_0_OR_GREATER
142+
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
143+
#endif
144+
out T{{#nrt}}
145+
#if NET6_0_OR_GREATER
146+
?
147+
#endif
148+
{{/nrt}} result)
133149
{
134150
try
135151
{
@@ -176,10 +192,10 @@ using System.Runtime.CompilerServices;
176192
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
177193
// For example: 2009-06-15T13:45:30.0000000
178194
return dateTimeOffset.ToString(format);
179-
{{#net60OrLater}}
195+
#if NET6_0_OR_GREATER
180196
if (obj is DateOnly dateOnly)
181197
return dateOnly.ToString(format);
182-
{{/net60OrLater}}
198+
#endif
183199
if (obj is bool boolean)
184200
return boolean
185201
? "true"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace {{packageName}}.{{clientPackage}}
5454
{{/isEnum}}
5555
{{/model}}
5656
{{/models}}
57-
JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new{{^net60OrLater}} JsonSerializerOptionsProvider{{/net60OrLater}}(_jsonOptions);
57+
JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new JsonSerializerOptionsProvider(_jsonOptions);
5858
_services.AddSingleton(jsonSerializerOptionsProvider);
5959
{{#useSourceGeneration}}
6060

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ namespace {{packageName}}.{{clientPackage}}
272272
/// <returns></returns>
273273
private string GetECDSASignature(byte[] dataToSign)
274274
{
275-
{{#net60OrLater}}
275+
#if NET6_0_OR_GREATER
276276
if (!File.Exists(KeyFilePath))
277277
throw new Exception("key file path does not exist.");
278278
@@ -310,10 +310,10 @@ namespace {{packageName}}.{{clientPackage}}
310310
var signedString = System.Convert.ToBase64String(derBytes);
311311

312312
return signedString;
313-
{{/net60OrLater}}
314-
{{^net60OrLater}}
313+
#endif
314+
#if !NET6_0_OR_GREATER
315315
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
316-
{{/net60OrLater}}
316+
#endif
317317
}
318318

319319
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,11 @@ namespace {{packageName}}.{{apiPackage}}
562562
tokenBaseLocalVars.Add(httpSignatureTokenLocalVar{{-index}});
563563

564564
if (httpRequestMessageLocalVar.Content != null) {
565-
string requestBodyLocalVar = await httpRequestMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
565+
string requestBodyLocalVar = await httpRequestMessageLocalVar.Content.ReadAsStringAsync(
566+
#if NET6_0_OR_GREATER
567+
cancellationToken
568+
#endif
569+
).ConfigureAwait(false);
566570
567571
httpSignatureTokenLocalVar{{-index}}.UseInHeader(httpRequestMessageLocalVar, requestBodyLocalVar, cancellationToken);
568572
}
@@ -607,23 +611,25 @@ namespace {{packageName}}.{{apiPackage}}
607611
httpRequestMessageLocalVar.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptLocalVar));
608612
{{/-first}}
609613
{{/produces}}
610-
{{#net60OrLater}}
611-
614+
#if NET6_0_OR_GREATER
612615
httpRequestMessageLocalVar.Method = HttpMethod.{{#lambda.titlecase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.titlecase}};
613-
{{/net60OrLater}}
614-
{{^net60OrLater}}
616+
#else
615617
httpRequestMessageLocalVar.Method = new HttpMethod("{{#lambda.uppercase}}{{httpMethod}}{{/lambda.uppercase}}");
616-
{{/net60OrLater}}
618+
#endif
617619

618620
DateTime requestedAtLocalVar = DateTime.UtcNow;
619621

620622
using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken).ConfigureAwait(false))
621623
{
622-
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
624+
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync(
625+
#if NET6_0_OR_GREATER
626+
cancellationToken
627+
#endif
628+
).ConfigureAwait(false);
623629
624630
ILogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse>();
625631

626-
{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);
632+
{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new {{operationId}}ApiResponse(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);
627633

628634
After{{operationId}}DefaultImplementation({{#lambda.joinWithComma}}apiResponseLocalVar {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
629635

@@ -761,7 +767,11 @@ namespace {{packageName}}.{{apiPackage}}
761767
/// </summary>
762768
/// <param name="result"></param>
763769
/// <returns></returns>
764-
public bool Try{{vendorExtensions.x-http-status}}({{#net60OrLater}}[NotNullWhen(true)]{{/net60OrLater}}out {{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}{{#nrt}}?{{/nrt}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} result)
770+
public bool Try{{vendorExtensions.x-http-status}}(
771+
#if NET6_0_OR_GREATER
772+
[NotNullWhen(true)]
773+
#endif
774+
out {{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}{{#nrt}}?{{/nrt}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} result)
765775
{
766776
result = null;
767777

0 commit comments

Comments
 (0)