Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[csharp] Convert numbers to long when min/max is too big #19290

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,46 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
property.isInnerEnum = false;
property.isString = false;
}

Double maximum = asDouble(property.maximum);
if (property.dataType.equals("int") && maximum != null) {
if ((!property.exclusiveMaximum && asInteger(property.maximum) == null) || (property.exclusiveMaximum && asInteger((maximum + 1) + "") == null)) {
property.dataType = "long";
property.datatypeWithEnum = "long";
}
}

Double minimum = asDouble(property.minimum);
if (property.dataType.equals("int") && minimum != null) {
if ((!property.exclusiveMinimum && asInteger(property.minimum) == null) || (property.exclusiveMinimum && asInteger((minimum - 1) + "") == null)) {
property.dataType = "long";
property.datatypeWithEnum = "long";
}
}
}

/** If the value can be parsed as a double, returns the value, otherwise returns null */
public static Double asDouble(String strNum) {
if (strNum == null) {
return null;
}
try {
return Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return null;
}
}

/** If the value can be parsed as an integer, returns the value, otherwise returns null */
public static Integer asInteger(String strNum) {
if (strNum == null) {
return null;
}
try {
return Integer.parseInt(strNum);
} catch (NumberFormatException nfe) {
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ namespace {{packageName}}.{{apiPackage}}
{{#queryParams}}
{{^required}}
if ({{paramName}}.IsSet)
// here too
parseQueryStringLocalVar["{{baseName}}"] = ClientUtils.ParameterToString({{paramName}}.Value);

{{/required}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@
{{/minLength}}
{{#maximum}}
// {{{name}}} ({{{dataType}}}) maximum
if ({{#useGenericHost}}{{^required}}this.{{{name}}}Option.IsSet && {{/required}}{{/useGenericHost}}this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}} > ({{{dataType}}}){{maximum}})
if ({{#useGenericHost}}{{^required}}this.{{{name}}}Option.IsSet && {{/required}}{{/useGenericHost}}this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}} {{#exclusiveMaximum}}<={{/exclusiveMaximum}}{{^exclusiveMaximum}}>{{/exclusiveMaximum}} ({{{dataType}}}){{maximum}})
{
yield return new ValidationResult("Invalid value for {{{name}}}, must be a value less than or equal to {{maximum}}.", new [] { "{{{name}}}" });
yield return new ValidationResult("Invalid value for {{{name}}}, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.", new [] { "{{{name}}}" });
}

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

{{/minimum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,24 @@ components:
format: int32
maximum: 200
minimum: 20
int32Range:
type: integer
maximum: 2147483647
minimum: -2147483648
int64Positive:
type: integer
minimum: 2147483648 # int.MaxValue + 1
int64Negative:
type: integer
maximum: -2147483649 # int.MinValue - 1
int64PositiveExclusive:
type: integer
minimum: 2147483647
exclusiveMinimum: true
int64NegativeExclusive:
type: integer
maximum: -2147483648
exclusiveMaximum: true
unsigned_integer:
type: integer
format: int32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,24 @@ components:
maximum: 200
minimum: 20
type: integer
int32Range:
maximum: 2147483647
minimum: -2147483648
type: integer
int64Positive:
minimum: 2147483648
type: integer
int64Negative:
maximum: -2147483649
type: integer
int64PositiveExclusive:
exclusiveMinimum: true
minimum: 2147483647
type: integer
int64NegativeExclusive:
exclusiveMaximum: true
maximum: -2147483648
type: integer
unsigned_integer:
format: int32
maximum: 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ Name | Type | Description | Notes
**Double** | **double** | | [optional]
**Float** | **float** | | [optional]
**Int32** | **int** | | [optional]
**Int32Range** | **int** | | [optional]
**Int64** | **long** | | [optional]
**Int64Negative** | **long** | | [optional]
**Int64NegativeExclusive** | **long** | | [optional]
**Int64Positive** | **long** | | [optional]
**Int64PositiveExclusive** | **long** | | [optional]
**Integer** | **int** | | [optional]
**PatternWithBackslash** | **string** | None | [optional]
**PatternWithDigits** | **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ public void Int32Test()
// TODO unit test for the property 'Int32'
}

/// <summary>
/// Test the property 'Int32Range'
/// </summary>
[Fact]
public void Int32RangeTest()
{
// TODO unit test for the property 'Int32Range'
}

/// <summary>
/// Test the property 'Int64'
/// </summary>
Expand All @@ -152,6 +161,42 @@ public void Int64Test()
// TODO unit test for the property 'Int64'
}

/// <summary>
/// Test the property 'Int64Negative'
/// </summary>
[Fact]
public void Int64NegativeTest()
{
// TODO unit test for the property 'Int64Negative'
}

/// <summary>
/// Test the property 'Int64NegativeExclusive'
/// </summary>
[Fact]
public void Int64NegativeExclusiveTest()
{
// TODO unit test for the property 'Int64NegativeExclusive'
}

/// <summary>
/// Test the property 'Int64Positive'
/// </summary>
[Fact]
public void Int64PositiveTest()
{
// TODO unit test for the property 'Int64Positive'
}

/// <summary>
/// Test the property 'Int64PositiveExclusive'
/// </summary>
[Fact]
public void Int64PositiveExclusiveTest()
{
// TODO unit test for the property 'Int64PositiveExclusive'
}

/// <summary>
/// Test the property 'Integer'
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4281,19 +4281,15 @@ public async Task<ITestEnumParametersApiResponse> TestEnumParametersAsync(Option
System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty);

if (enumQueryStringArray.IsSet)
// here too
parseQueryStringLocalVar["enum_query_string_array"] = ClientUtils.ParameterToString(enumQueryStringArray.Value);

if (enumQueryString.IsSet)
// here too
parseQueryStringLocalVar["enum_query_string"] = ClientUtils.ParameterToString(enumQueryString.Value);

if (enumQueryDouble.IsSet)
// here too
parseQueryStringLocalVar["enum_query_double"] = ClientUtils.ParameterToString(enumQueryDouble.Value);

if (enumQueryInteger.IsSet)
// here too
parseQueryStringLocalVar["enum_query_integer"] = ClientUtils.ParameterToString(enumQueryInteger.Value);

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

if (stringGroup.IsSet)
// here too
parseQueryStringLocalVar["string_group"] = ClientUtils.ParameterToString(stringGroup.Value);

if (int64Group.IsSet)
// here too
parseQueryStringLocalVar["int64_group"] = ClientUtils.ParameterToString(int64Group.Value);

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

if (notRequiredNotNullable.IsSet)
// here too
parseQueryStringLocalVar["notRequiredNotNullable"] = ClientUtils.ParameterToString(notRequiredNotNullable.Value);

if (notRequiredNullable.IsSet)
// here too
parseQueryStringLocalVar["notRequiredNullable"] = ClientUtils.ParameterToString(notRequiredNullable.Value);

uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();
Expand Down
Loading
Loading