Skip to content
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 @@ -242,12 +242,22 @@ private static void ApplyRangeAttribute(OpenApiSchema schema, RangeAttribute ran
// Use the appropriate culture as the user may have specified a culture-specific format for the numbers
// if they specified the value as a string. By default RangeAttribute uses the current culture, but it
// can be set to use the invariant culture.
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture || rangeAttribute.Minimum is double
? CultureInfo.InvariantCulture
: CultureInfo.CurrentCulture;

schema.Maximum = Convert.ToDecimal(rangeAttribute.Maximum, targetCulture);
schema.Minimum = Convert.ToDecimal(rangeAttribute.Minimum, targetCulture);
var maxString = Convert.ToString(rangeAttribute.Maximum, targetCulture);
var minString = Convert.ToString(rangeAttribute.Minimum, targetCulture);

if (decimal.TryParse(maxString, NumberStyles.Any, targetCulture, out var value))
{
schema.Maximum = value;
}

if (decimal.TryParse(minString, NumberStyles.Any, targetCulture, out value))
{
schema.Minimum = value;
}
}

#if NET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public static TheoryData<string, bool, RangeAttribute, string, string> TestCases
{
foreach (var exclusive in isExclusive)
{
testCases.Add(culture, exclusive, new(1, 1234) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1, 1234) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1d, 1234d) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1.23, 4.56) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1.23", "4.56");
Expand Down Expand Up @@ -111,6 +110,23 @@ public static void ApplyValidationAttributes_Handles_RangeAttribute_Correctly(
Assert.Equal(maximum, schema.Maximum);
}

[Fact]
public static void ApplyValidationAttributes_Handles_Invalid_RangeAttribute_Values()
{
// Arrange
var rangeAttribute = new RangeAttribute(typeof(int), "foo", "bar");
var schema = new OpenApiSchema();

// Act
schema.ApplyValidationAttributes([rangeAttribute]);

// Assert
Assert.Null(schema.ExclusiveMinimum);
Assert.Null(schema.ExclusiveMaximum);
Assert.Null(schema.Minimum);
Assert.Null(schema.Maximum);
}

private sealed class CultureSwitcher : IDisposable
{
private readonly CultureInfo _previous;
Expand Down