Skip to content

Commit 4ebfa21

Browse files
Avoid string parsing
Do not round trip the values from `[Range]` if they were set using `RangeAttribute(int, int)`.
1 parent 7e732bf commit 4ebfa21

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,42 @@ internal static void ApplyValidationAttributes(this JsonNode schema, IEnumerable
9090
}
9191
else if (attribute is RangeAttribute rangeAttribute)
9292
{
93-
// Use InvariantCulture if explicitly requested or if the range has been set via the
94-
// RangeAttribute(double, double) or RangeAttribute(int, int) constructors.
95-
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture || rangeAttribute.Minimum is double || rangeAttribute.Maximum is int
96-
? CultureInfo.InvariantCulture
97-
: CultureInfo.CurrentCulture;
93+
decimal? minDecimal = null;
94+
decimal? maxDecimal = null;
9895

99-
var minString = Convert.ToString(rangeAttribute.Minimum, targetCulture);
100-
var maxString = Convert.ToString(rangeAttribute.Maximum, targetCulture);
96+
if (rangeAttribute.Minimum is int minimumInteger)
97+
{
98+
// The range was set with the RangeAttribute(int, int) constructor.
99+
minDecimal = minimumInteger;
100+
maxDecimal = (int)rangeAttribute.Maximum;
101+
}
102+
else
103+
{
104+
// Use InvariantCulture if explicitly requested or if the range has been set via the RangeAttribute(double, double) constructor.
105+
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture || rangeAttribute.Minimum is double x
106+
? CultureInfo.InvariantCulture
107+
: CultureInfo.CurrentCulture;
108+
109+
var minString = Convert.ToString(rangeAttribute.Minimum, targetCulture);
110+
var maxString = Convert.ToString(rangeAttribute.Maximum, targetCulture);
111+
112+
if (decimal.TryParse(minString, NumberStyles.Any, targetCulture, out var value))
113+
{
114+
minDecimal = value;
115+
}
116+
if (decimal.TryParse(maxString, NumberStyles.Any, targetCulture, out value))
117+
{
118+
maxDecimal = value;
119+
}
120+
}
101121

102-
if (decimal.TryParse(minString, NumberStyles.Any, targetCulture, out var minDecimal))
122+
if (minDecimal is { } minValue)
103123
{
104-
schema[rangeAttribute.MinimumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMinimum : OpenApiSchemaKeywords.MinimumKeyword] = minDecimal;
124+
schema[rangeAttribute.MinimumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMinimum : OpenApiSchemaKeywords.MinimumKeyword] = minValue;
105125
}
106-
if (decimal.TryParse(maxString, NumberStyles.Any, targetCulture, out var maxDecimal))
126+
if (maxDecimal is { } maxValue)
107127
{
108-
schema[rangeAttribute.MaximumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMaximum : OpenApiSchemaKeywords.MaximumKeyword] = maxDecimal;
128+
schema[rangeAttribute.MaximumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMaximum : OpenApiSchemaKeywords.MaximumKeyword] = maxValue;
109129
}
110130
}
111131
else if (attribute is RegularExpressionAttribute regularExpressionAttribute)

0 commit comments

Comments
 (0)