Skip to content

Commit 2eed13e

Browse files
Avoid string parsing
Do not round trip the values from `[Range]` if they were set using `RangeAttribute(int, int)`.
1 parent 7493c7f commit 2eed13e

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
@@ -91,22 +91,42 @@ internal static void ApplyValidationAttributes(this JsonNode schema, IEnumerable
9191
}
9292
else if (attribute is RangeAttribute rangeAttribute)
9393
{
94-
// Use InvariantCulture if explicitly requested or if the range has been set via the
95-
// RangeAttribute(double, double) or RangeAttribute(int, int) constructors.
96-
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture || rangeAttribute.Minimum is double || rangeAttribute.Maximum is int
97-
? CultureInfo.InvariantCulture
98-
: CultureInfo.CurrentCulture;
94+
decimal? minDecimal = null;
95+
decimal? maxDecimal = null;
9996

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

103-
if (decimal.TryParse(minString, NumberStyles.Any, targetCulture, out var minDecimal))
123+
if (minDecimal is { } minValue)
104124
{
105-
schema[rangeAttribute.MinimumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMinimum : OpenApiSchemaKeywords.MinimumKeyword] = minDecimal;
125+
schema[rangeAttribute.MinimumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMinimum : OpenApiSchemaKeywords.MinimumKeyword] = minValue;
106126
}
107-
if (decimal.TryParse(maxString, NumberStyles.Any, targetCulture, out var maxDecimal))
127+
if (maxDecimal is { } maxValue)
108128
{
109-
schema[rangeAttribute.MaximumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMaximum : OpenApiSchemaKeywords.MaximumKeyword] = maxDecimal;
129+
schema[rangeAttribute.MaximumIsExclusive ? OpenApiSchemaKeywords.ExclusiveMaximum : OpenApiSchemaKeywords.MaximumKeyword] = maxValue;
110130
}
111131
}
112132
else if (attribute is RegularExpressionAttribute regularExpressionAttribute)

0 commit comments

Comments
 (0)