Skip to content

Commit 1e1e786

Browse files
authored
created partial method to format arguments (OpenAPITools#15677)
1 parent d7d57e2 commit 1e1e786

File tree

25 files changed

+797
-1099
lines changed

25 files changed

+797
-1099
lines changed

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,22 @@ namespace {{packageName}}.{{apiPackage}}
135135
}
136136
{{#operation}}
137137

138+
{{#allParams}}
139+
{{#-first}}
140+
partial void Format{{operationId}}({{#allParams}}{{#isPrimitiveType}}ref {{/isPrimitiveType}}{{#requiredAndNotNullable}}{{#lambda.required}}{{{dataType}}}{{/lambda.required}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredAndNotNullable}}{{^requiredAndNotNullable}}{{#lambda.optional}}{{{dataType}}}{{/lambda.optional}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredAndNotNullable}}{{/allParams}});
141+
142+
{{/-first}}
143+
{{/allParams}}
144+
{{#requiredAndNotNullableParams}}
145+
{{#-first}}
138146
/// <summary>
139147
/// Validates the request parameters
140148
/// </summary>
141149
{{#allParams}}
142150
/// <param name="{{paramName}}"></param>
143151
{{/allParams}}
144152
/// <returns></returns>
145-
protected virtual {{^allParams}}void{{/allParams}}{{#allParams}}{{#-first}}{{^-last}}({{/-last}}{{/-first}}{{#requiredAndNotNullable}}{{#lambda.required}}{{{dataType}}}{{/lambda.required}}{{^-last}}, {{/-last}}{{/requiredAndNotNullable}}{{^requiredAndNotNullable}}{{#lambda.optional}}{{{dataType}}}{{/lambda.optional}}{{^-last}}, {{/-last}}{{/requiredAndNotNullable}}{{#-last}}{{^-first}}){{/-first}}{{/-last}}{{/allParams}} On{{operationId}}({{#allParams}}{{#requiredAndNotNullable}}{{#lambda.required}}{{{dataType}}}{{/lambda.required}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredAndNotNullable}}{{^requiredAndNotNullable}}{{#lambda.optional}}{{{dataType}}}{{/lambda.optional}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredAndNotNullable}}{{/allParams}})
153+
private void Validate{{operationId}}({{#requiredAndNotNullableParams}}{{#lambda.required}}{{{dataType}}}{{/lambda.required}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredAndNotNullableParams}})
146154
{
147155
{{#requiredAndNotNullableParams}}
148156
{{#-first}}
@@ -163,14 +171,14 @@ namespace {{packageName}}.{{apiPackage}}
163171
{{/vendorExtensions.x-csharp-value-type}}
164172
{{/nrt}}
165173
{{#-last}}
166-
#pragma warning disable CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
167-
#pragma warning disable CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
168-
174+
#pragma warning restore CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
175+
#pragma warning restore CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
169176
{{/-last}}
170177
{{/requiredAndNotNullableParams}}
171-
return{{#allParams}} {{#-first}}{{^-last}}({{/-last}}{{/-first}}{{paramName}}{{^-last}},{{/-last}}{{#-last}}{{^-first}}){{/-first}}{{/-last}}{{/allParams}};
172178
}
173179

180+
{{/-first}}
181+
{{/requiredAndNotNullableParams}}
174182
/// <summary>
175183
/// Processes the server response
176184
/// </summary>
@@ -231,17 +239,18 @@ namespace {{packageName}}.{{apiPackage}}
231239
232240
try
233241
{
234-
{{#allParams}}{{#-first}}{{#-last}}{{paramName}} = {{/-last}}{{^-last}}var validatedParameterLocalVars = {{/-last}}{{/-first}}{{/allParams}}On{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
235-
{{#allParams}}
242+
{{#requiredAndNotNullableParams}}
236243
{{#-first}}
237-
{{^-last}}
244+
Validate{{operationId}}({{#requiredAndNotNullableParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredAndNotNullableParams}});
245+
246+
{{/-first}}
247+
{{/requiredAndNotNullableParams}}
238248
{{#allParams}}
239-
{{paramName}} = validatedParameterLocalVars.Item{{-index}};
240-
{{/allParams}}
241-
{{/-last}}
249+
{{#-first}}
250+
Format{{operationId}}({{#allParams}}{{#isPrimitiveType}}ref {{/isPrimitiveType}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
251+
242252
{{/-first}}
243253
{{/allParams}}
244-
245254
using (HttpRequestMessage httpRequestMessageLocalVar = new HttpRequestMessage())
246255
{
247256
{{^servers}}

samples/client/petstore/csharp-netcore/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/AnotherFakeApi.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,23 @@ protected virtual void OnApiResponded(ApiResponseEventArgs args)
128128
Logger.LogInformation("{0,-9} | {1} | {3}", (args.ReceivedAt - args.RequestedAt).TotalSeconds, args.HttpStatus, args.Path);
129129
}
130130

131+
partial void FormatCall123TestSpecialTags(ModelClient modelClient);
132+
131133
/// <summary>
132134
/// Validates the request parameters
133135
/// </summary>
134136
/// <param name="modelClient"></param>
135137
/// <returns></returns>
136-
protected virtual ModelClient OnCall123TestSpecialTags(ModelClient modelClient)
138+
private void ValidateCall123TestSpecialTags(ModelClient modelClient)
137139
{
138140
#pragma warning disable CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
139141
#pragma warning disable CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
140142

141143
if (modelClient == null)
142144
throw new ArgumentNullException(nameof(modelClient));
143145

144-
#pragma warning disable CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
145-
#pragma warning disable CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
146-
147-
return modelClient;
146+
#pragma warning restore CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
147+
#pragma warning restore CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
148148
}
149149

150150
/// <summary>
@@ -199,7 +199,9 @@ public async Task<ApiResponse<ModelClient>> Call123TestSpecialTagsAsync(ModelCli
199199

200200
try
201201
{
202-
modelClient = OnCall123TestSpecialTags(modelClient);
202+
ValidateCall123TestSpecialTags(modelClient);
203+
204+
FormatCall123TestSpecialTags(modelClient);
203205

204206
using (HttpRequestMessage httpRequestMessageLocalVar = new HttpRequestMessage())
205207
{

samples/client/petstore/csharp-netcore/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/DefaultApi.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,6 @@ protected virtual void OnApiResponded(ApiResponseEventArgs args)
170170
Logger.LogInformation("{0,-9} | {1} | {3}", (args.ReceivedAt - args.RequestedAt).TotalSeconds, args.HttpStatus, args.Path);
171171
}
172172

173-
/// <summary>
174-
/// Validates the request parameters
175-
/// </summary>
176-
/// <returns></returns>
177-
protected virtual void OnFooGet()
178-
{
179-
return;
180-
}
181-
182173
/// <summary>
183174
/// Processes the server response
184175
/// </summary>
@@ -227,8 +218,6 @@ public async Task<ApiResponse<FooGetDefaultResponse>> FooGetAsync(System.Threadi
227218

228219
try
229220
{
230-
OnFooGet();
231-
232221
using (HttpRequestMessage httpRequestMessageLocalVar = new HttpRequestMessage())
233222
{
234223
uriBuilderLocalVar.Host = HttpClient.BaseAddress!.Host;
@@ -274,23 +263,23 @@ public async Task<ApiResponse<FooGetDefaultResponse>> FooGetAsync(System.Threadi
274263
}
275264
}
276265

266+
partial void FormatGetCountry(ref string country);
267+
277268
/// <summary>
278269
/// Validates the request parameters
279270
/// </summary>
280271
/// <param name="country"></param>
281272
/// <returns></returns>
282-
protected virtual string OnGetCountry(string country)
273+
private void ValidateGetCountry(string country)
283274
{
284275
#pragma warning disable CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
285276
#pragma warning disable CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
286277

287278
if (country == null)
288279
throw new ArgumentNullException(nameof(country));
289280

290-
#pragma warning disable CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
291-
#pragma warning disable CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
292-
293-
return country;
281+
#pragma warning restore CS0472 // The result of the expression is always the same since a value of this type is never equal to 'null'
282+
#pragma warning restore CS8073 // The result of the expression is always the same since a value of this type is never equal to 'null'
294283
}
295284

296285
/// <summary>
@@ -345,7 +334,9 @@ public async Task<ApiResponse<object>> GetCountryAsync(string country, System.Th
345334

346335
try
347336
{
348-
country = OnGetCountry(country);
337+
ValidateGetCountry(country);
338+
339+
FormatGetCountry(ref country);
349340

350341
using (HttpRequestMessage httpRequestMessageLocalVar = new HttpRequestMessage())
351342
{
@@ -402,15 +393,6 @@ public async Task<ApiResponse<object>> GetCountryAsync(string country, System.Th
402393
}
403394
}
404395

405-
/// <summary>
406-
/// Validates the request parameters
407-
/// </summary>
408-
/// <returns></returns>
409-
protected virtual void OnHello()
410-
{
411-
return;
412-
}
413-
414396
/// <summary>
415397
/// Processes the server response
416398
/// </summary>
@@ -459,8 +441,6 @@ public async Task<ApiResponse<List<Guid>>> HelloAsync(System.Threading.Cancellat
459441

460442
try
461443
{
462-
OnHello();
463-
464444
using (HttpRequestMessage httpRequestMessageLocalVar = new HttpRequestMessage())
465445
{
466446
uriBuilderLocalVar.Host = HttpClient.BaseAddress!.Host;

0 commit comments

Comments
 (0)