Skip to content

Commit b0381a5

Browse files
Fix RangeAttribute behaviour
- Use invariant culture when values are `double`. - Use `decimal.TryParse()` not `Convert.ToDecimal()` so that invalid values are just ignored like they were before. - Remove duplicate test case.
1 parent cc663df commit b0381a5

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,22 @@ private static void ApplyRangeAttribute(OpenApiSchema schema, RangeAttribute ran
242242
// Use the appropriate culture as the user may have specified a culture-specific format for the numbers
243243
// if they specified the value as a string. By default RangeAttribute uses the current culture, but it
244244
// can be set to use the invariant culture.
245-
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture
245+
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture || rangeAttribute.Minimum is double
246246
? CultureInfo.InvariantCulture
247247
: CultureInfo.CurrentCulture;
248248

249-
schema.Maximum = Convert.ToDecimal(rangeAttribute.Maximum, targetCulture);
250-
schema.Minimum = Convert.ToDecimal(rangeAttribute.Minimum, targetCulture);
249+
var maxString = Convert.ToString(rangeAttribute.Maximum, targetCulture);
250+
var minString = Convert.ToString(rangeAttribute.Minimum, targetCulture);
251+
252+
if (decimal.TryParse(maxString, NumberStyles.Any, targetCulture, out var value))
253+
{
254+
schema.Maximum = value;
255+
}
256+
257+
if (decimal.TryParse(minString, NumberStyles.Any, targetCulture, out value))
258+
{
259+
schema.Minimum = value;
260+
}
251261
}
252262

253263
#if NET

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiSchemaExtensionsTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public static TheoryData<string, bool, RangeAttribute, string, string> TestCases
5555
{
5656
foreach (var exclusive in isExclusive)
5757
{
58-
testCases.Add(culture, exclusive, new(1, 1234) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
5958
testCases.Add(culture, exclusive, new(1, 1234) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
6059
testCases.Add(culture, exclusive, new(1d, 1234d) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
6160
testCases.Add(culture, exclusive, new(1.23, 4.56) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1.23", "4.56");
@@ -111,6 +110,23 @@ public static void ApplyValidationAttributes_Handles_RangeAttribute_Correctly(
111110
Assert.Equal(maximum, schema.Maximum);
112111
}
113112

113+
[Fact]
114+
public static void ApplyValidationAttributes_Handles_Invalid_RangeAttribute_Values()
115+
{
116+
// Arrange
117+
var rangeAttribute = new RangeAttribute(typeof(int), "foo", "bar");
118+
var schema = new OpenApiSchema();
119+
120+
// Act
121+
schema.ApplyValidationAttributes([rangeAttribute]);
122+
123+
// Assert
124+
Assert.Null(schema.ExclusiveMinimum);
125+
Assert.Null(schema.ExclusiveMaximum);
126+
Assert.Null(schema.Minimum);
127+
Assert.Null(schema.Maximum);
128+
}
129+
114130
private sealed class CultureSwitcher : IDisposable
115131
{
116132
private readonly CultureInfo _previous;

0 commit comments

Comments
 (0)