Skip to content

Commit 2bc4a16

Browse files
authored
[csharp] Convert numbers to long when min/max is too big (#19290)
* convert numbers to long when min/max is too big * handle exclusives
1 parent 4c163fe commit 2bc4a16

61 files changed

Lines changed: 2765 additions & 99 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/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,46 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
480480
property.isInnerEnum = false;
481481
property.isString = false;
482482
}
483+
484+
Double maximum = asDouble(property.maximum);
485+
if (property.dataType.equals("int") && maximum != null) {
486+
if ((!property.exclusiveMaximum && asInteger(property.maximum) == null) || (property.exclusiveMaximum && asInteger((maximum + 1) + "") == null)) {
487+
property.dataType = "long";
488+
property.datatypeWithEnum = "long";
489+
}
490+
}
491+
492+
Double minimum = asDouble(property.minimum);
493+
if (property.dataType.equals("int") && minimum != null) {
494+
if ((!property.exclusiveMinimum && asInteger(property.minimum) == null) || (property.exclusiveMinimum && asInteger((minimum - 1) + "") == null)) {
495+
property.dataType = "long";
496+
property.datatypeWithEnum = "long";
497+
}
498+
}
499+
}
500+
501+
/** If the value can be parsed as a double, returns the value, otherwise returns null */
502+
public static Double asDouble(String strNum) {
503+
if (strNum == null) {
504+
return null;
505+
}
506+
try {
507+
return Double.parseDouble(strNum);
508+
} catch (NumberFormatException nfe) {
509+
return null;
510+
}
511+
}
512+
513+
/** If the value can be parsed as an integer, returns the value, otherwise returns null */
514+
public static Integer asInteger(String strNum) {
515+
if (strNum == null) {
516+
return null;
517+
}
518+
try {
519+
return Integer.parseInt(strNum);
520+
} catch (NumberFormatException nfe) {
521+
return null;
522+
}
483523
}
484524

485525
@Override

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ namespace {{packageName}}.{{apiPackage}}
436436
{{#queryParams}}
437437
{{^required}}
438438
if ({{paramName}}.IsSet)
439-
// here too
440439
parseQueryStringLocalVar["{{baseName}}"] = ClientUtils.ParameterToString({{paramName}}.Value);
441440

442441
{{/required}}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@
7878
{{/minLength}}
7979
{{#maximum}}
8080
// {{{name}}} ({{{dataType}}}) maximum
81-
if ({{#useGenericHost}}{{^required}}this.{{{name}}}Option.IsSet && {{/required}}{{/useGenericHost}}this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}} > ({{{dataType}}}){{maximum}})
81+
if ({{#useGenericHost}}{{^required}}this.{{{name}}}Option.IsSet && {{/required}}{{/useGenericHost}}this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}} {{#exclusiveMaximum}}<={{/exclusiveMaximum}}{{^exclusiveMaximum}}>{{/exclusiveMaximum}} ({{{dataType}}}){{maximum}})
8282
{
83-
yield return new ValidationResult("Invalid value for {{{name}}}, must be a value less than or equal to {{maximum}}.", new [] { "{{{name}}}" });
83+
yield return new ValidationResult("Invalid value for {{{name}}}, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.", new [] { "{{{name}}}" });
8484
}
8585

8686
{{/maximum}}
8787
{{#minimum}}
8888
// {{{name}}} ({{{dataType}}}) minimum
89-
if ({{#useGenericHost}}{{^required}}this.{{{name}}}Option.IsSet && {{/required}}{{/useGenericHost}}this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}} < ({{{dataType}}}){{minimum}})
89+
if ({{#useGenericHost}}{{^required}}this.{{{name}}}Option.IsSet && {{/required}}{{/useGenericHost}}this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}} {{#exclusiveMaximum}}>={{/exclusiveMaximum}}{{^exclusiveMaximum}}<{{/exclusiveMaximum}} ({{{dataType}}}){{minimum}})
9090
{
91-
yield return new ValidationResult("Invalid value for {{{name}}}, must be a value greater than or equal to {{minimum}}.", new [] { "{{{name}}}" });
91+
yield return new ValidationResult("Invalid value for {{{name}}}, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.", new [] { "{{{name}}}" });
9292
}
9393

9494
{{/minimum}}

modules/openapi-generator/src/test/resources/3_0/csharp/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,24 @@ components:
16841684
format: int32
16851685
maximum: 200
16861686
minimum: 20
1687+
int32Range:
1688+
type: integer
1689+
maximum: 2147483647
1690+
minimum: -2147483648
1691+
int64Positive:
1692+
type: integer
1693+
minimum: 2147483648 # int.MaxValue + 1
1694+
int64Negative:
1695+
type: integer
1696+
maximum: -2147483649 # int.MinValue - 1
1697+
int64PositiveExclusive:
1698+
type: integer
1699+
minimum: 2147483647
1700+
exclusiveMinimum: true
1701+
int64NegativeExclusive:
1702+
type: integer
1703+
maximum: -2147483648
1704+
exclusiveMaximum: true
16871705
unsigned_integer:
16881706
type: integer
16891707
format: int32

samples/client/petstore/csharp/generichost/net4.7/FormModels/api/openapi.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,24 @@ components:
15761576
maximum: 200
15771577
minimum: 20
15781578
type: integer
1579+
int32Range:
1580+
maximum: 2147483647
1581+
minimum: -2147483648
1582+
type: integer
1583+
int64Positive:
1584+
minimum: 2147483648
1585+
type: integer
1586+
int64Negative:
1587+
maximum: -2147483649
1588+
type: integer
1589+
int64PositiveExclusive:
1590+
exclusiveMinimum: true
1591+
minimum: 2147483647
1592+
type: integer
1593+
int64NegativeExclusive:
1594+
exclusiveMaximum: true
1595+
maximum: -2147483648
1596+
type: integer
15791597
unsigned_integer:
15801598
format: int32
15811599
maximum: 200

samples/client/petstore/csharp/generichost/net4.7/FormModels/docs/models/FormatTest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ Name | Type | Description | Notes
1414
**Double** | **double** | | [optional]
1515
**Float** | **float** | | [optional]
1616
**Int32** | **int** | | [optional]
17+
**Int32Range** | **int** | | [optional]
1718
**Int64** | **long** | | [optional]
19+
**Int64Negative** | **long** | | [optional]
20+
**Int64NegativeExclusive** | **long** | | [optional]
21+
**Int64Positive** | **long** | | [optional]
22+
**Int64PositiveExclusive** | **long** | | [optional]
1823
**Integer** | **int** | | [optional]
1924
**PatternWithBackslash** | **string** | None | [optional]
2025
**PatternWithDigits** | **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]

samples/client/petstore/csharp/generichost/net4.7/FormModels/src/Org.OpenAPITools.Test/Model/FormatTestTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ public void Int32Test()
143143
// TODO unit test for the property 'Int32'
144144
}
145145

146+
/// <summary>
147+
/// Test the property 'Int32Range'
148+
/// </summary>
149+
[Fact]
150+
public void Int32RangeTest()
151+
{
152+
// TODO unit test for the property 'Int32Range'
153+
}
154+
146155
/// <summary>
147156
/// Test the property 'Int64'
148157
/// </summary>
@@ -152,6 +161,42 @@ public void Int64Test()
152161
// TODO unit test for the property 'Int64'
153162
}
154163

164+
/// <summary>
165+
/// Test the property 'Int64Negative'
166+
/// </summary>
167+
[Fact]
168+
public void Int64NegativeTest()
169+
{
170+
// TODO unit test for the property 'Int64Negative'
171+
}
172+
173+
/// <summary>
174+
/// Test the property 'Int64NegativeExclusive'
175+
/// </summary>
176+
[Fact]
177+
public void Int64NegativeExclusiveTest()
178+
{
179+
// TODO unit test for the property 'Int64NegativeExclusive'
180+
}
181+
182+
/// <summary>
183+
/// Test the property 'Int64Positive'
184+
/// </summary>
185+
[Fact]
186+
public void Int64PositiveTest()
187+
{
188+
// TODO unit test for the property 'Int64Positive'
189+
}
190+
191+
/// <summary>
192+
/// Test the property 'Int64PositiveExclusive'
193+
/// </summary>
194+
[Fact]
195+
public void Int64PositiveExclusiveTest()
196+
{
197+
// TODO unit test for the property 'Int64PositiveExclusive'
198+
}
199+
155200
/// <summary>
156201
/// Test the property 'Integer'
157202
/// </summary>

samples/client/petstore/csharp/generichost/net4.7/FormModels/src/Org.OpenAPITools/Api/FakeApi.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,19 +4281,15 @@ public async Task<ITestEnumParametersApiResponse> TestEnumParametersAsync(Option
42814281
System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty);
42824282

42834283
if (enumQueryStringArray.IsSet)
4284-
// here too
42854284
parseQueryStringLocalVar["enum_query_string_array"] = ClientUtils.ParameterToString(enumQueryStringArray.Value);
42864285

42874286
if (enumQueryString.IsSet)
4288-
// here too
42894287
parseQueryStringLocalVar["enum_query_string"] = ClientUtils.ParameterToString(enumQueryString.Value);
42904288

42914289
if (enumQueryDouble.IsSet)
4292-
// here too
42934290
parseQueryStringLocalVar["enum_query_double"] = ClientUtils.ParameterToString(enumQueryDouble.Value);
42944291

42954292
if (enumQueryInteger.IsSet)
4296-
// here too
42974293
parseQueryStringLocalVar["enum_query_integer"] = ClientUtils.ParameterToString(enumQueryInteger.Value);
42984294

42994295
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();
@@ -4530,11 +4526,9 @@ public async Task<ITestGroupParametersApiResponse> TestGroupParametersAsync(bool
45304526
parseQueryStringLocalVar["required_int64_group"] = ClientUtils.ParameterToString(requiredInt64Group);
45314527

45324528
if (stringGroup.IsSet)
4533-
// here too
45344529
parseQueryStringLocalVar["string_group"] = ClientUtils.ParameterToString(stringGroup.Value);
45354530

45364531
if (int64Group.IsSet)
4537-
// here too
45384532
parseQueryStringLocalVar["int64_group"] = ClientUtils.ParameterToString(int64Group.Value);
45394533

45404534
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();
@@ -5398,11 +5392,9 @@ public async Task<ITestQueryParameterCollectionFormatApiResponse> TestQueryParam
53985392
parseQueryStringLocalVar["requiredNullable"] = ClientUtils.ParameterToString(requiredNullable);
53995393

54005394
if (notRequiredNotNullable.IsSet)
5401-
// here too
54025395
parseQueryStringLocalVar["notRequiredNotNullable"] = ClientUtils.ParameterToString(notRequiredNotNullable.Value);
54035396

54045397
if (notRequiredNullable.IsSet)
5405-
// here too
54065398
parseQueryStringLocalVar["notRequiredNullable"] = ClientUtils.ParameterToString(notRequiredNullable.Value);
54075399

54085400
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

0 commit comments

Comments
 (0)