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
22 changes: 22 additions & 0 deletions src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ public async Task When_Range_attribute_is_set_on_double_then_minimum_and_maximum
Assert.Equal(5.5m, property.Minimum);
Assert.Equal(10.5m, property.Maximum);
}

[Theory]
[InlineData("en-US")]
[InlineData("de-DE")]
[InlineData("nb-NO")]
public async Task When_Range_attribute_is_set_on_decimal_then_minimum_and_maximum_are_set_regardless_of_culture(string culture)
{
// Arrange
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);

// Act
var schema = NewtonsoftJsonSchemaGenerator.FromType<AttributeTestClass>();
var property = schema.Properties["Decimal"];

// Assert
Assert.Equal(5.5m, property.Minimum);
Assert.Equal(10.5m, property.Maximum);
}

[Fact]
public async Task When_Range_attribute_has_double_max_then_max_is_not_set()
Expand Down Expand Up @@ -178,6 +197,9 @@ public class AttributeTestClass
[Range(5.5, 10.5)]
public double Double { get; set; }

[Range(typeof(decimal), "5.5", "10.5")]
public double Decimal { get; set; }

[Range(5.5, double.MaxValue)]
public double DoubleOnlyMin { get; set; }

Expand Down
8 changes: 4 additions & 4 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,15 +1300,15 @@ private static void ApplyRangeAttribute(JsonSchema schema, IEnumerable<Attribute
{
if (rangeAttribute.OperandType == typeof(double))
{
var minimum = (double)Convert.ChangeType(rangeAttribute.Minimum, typeof(double));
var minimum = (double)Convert.ChangeType(rangeAttribute.Minimum, typeof(double), CultureInfo.InvariantCulture);
if (minimum > double.MinValue)
{
schema.Minimum = (decimal)minimum;
}
}
else
{
var minimum = (decimal)Convert.ChangeType(rangeAttribute.Minimum, typeof(decimal));
var minimum = (decimal)Convert.ChangeType(rangeAttribute.Minimum, typeof(decimal), CultureInfo.InvariantCulture);
if (minimum > decimal.MinValue)
{
schema.Minimum = minimum;
Expand All @@ -1320,15 +1320,15 @@ private static void ApplyRangeAttribute(JsonSchema schema, IEnumerable<Attribute
{
if (rangeAttribute.OperandType == typeof(double))
{
var maximum = (double)Convert.ChangeType(rangeAttribute.Maximum, typeof(double));
var maximum = (double)Convert.ChangeType(rangeAttribute.Maximum, typeof(double), CultureInfo.InvariantCulture);
if (maximum < double.MaxValue)
{
schema.Maximum = (decimal)maximum;
}
}
else
{
var maximum = (decimal)Convert.ChangeType(rangeAttribute.Maximum, typeof(decimal));
var maximum = (decimal)Convert.ChangeType(rangeAttribute.Maximum, typeof(decimal), CultureInfo.InvariantCulture);
if (maximum < decimal.MaxValue)
{
schema.Maximum = maximum;
Expand Down