Skip to content

Commit

Permalink
Improve range attribute handling, closes #775
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Sep 13, 2018
1 parent 6dfa753 commit 7eacf5f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs
Expand Up @@ -188,5 +188,25 @@ public class AttributeTestClass
[ReadOnly(true)]
public bool ReadOnly { get; set; }
}

public class ClassWithTypedRange
{
[Range(typeof(decimal), "0", "1")]
public decimal Foo { get; set; }
}

[Fact]
public async Task When_range_has_type_and_strings_then_it_is_processed_correctly()
{
//// Arrange

//// Act
var schema = await JsonSchema4.FromTypeAsync<ClassWithTypedRange>();
var property = schema.Properties["Foo"];

//// Assert
Assert.Equal(0.0m, property.Minimum);
Assert.Equal(1.0m, property.Maximum);
}
}
}
56 changes: 48 additions & 8 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Expand Up @@ -949,14 +949,7 @@ public virtual void ApplyDataAnnotations(JsonSchema4 schema, JsonTypeDescription
if (typeDescription.Type == JsonObjectType.Number ||
typeDescription.Type == JsonObjectType.Integer)
{
dynamic rangeAttribute = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RangeAttribute");
if (rangeAttribute != null)
{
if (rangeAttribute.Minimum != null && rangeAttribute.Minimum > double.MinValue)
schema.Minimum = (decimal?)(double)rangeAttribute.Minimum;
if (rangeAttribute.Maximum != null && rangeAttribute.Maximum < double.MaxValue)
schema.Maximum = (decimal?)(double)rangeAttribute.Maximum;
}
ApplyRangeAttribute(schema, parentAttributes);

var multipleOfAttribute = parentAttributes.OfType<MultipleOfAttribute>().SingleOrDefault();
if (multipleOfAttribute != null)
Expand Down Expand Up @@ -1000,6 +993,53 @@ public virtual void ApplyDataAnnotations(JsonSchema4 schema, JsonTypeDescription
}
}

private void ApplyRangeAttribute(JsonSchema4 schema, IEnumerable<Attribute> parentAttributes)
{
dynamic rangeAttribute = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RangeAttribute");
if (rangeAttribute != null)
{
if (rangeAttribute.Minimum != null)
{
if (rangeAttribute.OperandType == typeof(double))
{
var minimum = (double) Convert.ChangeType(rangeAttribute.Minimum, typeof(double));
if (minimum > double.MinValue)
{
schema.Minimum = (decimal) minimum;
}
}
else
{
var minimum = (decimal) Convert.ChangeType(rangeAttribute.Minimum, typeof(decimal));
if (minimum > decimal.MinValue)
{
schema.Minimum = minimum;
}
}
}

if (rangeAttribute.Maximum != null)
{
if (rangeAttribute.OperandType == typeof(double))
{
var maximum = (double) Convert.ChangeType(rangeAttribute.Maximum, typeof(double));
if (maximum < double.MaxValue)
{
schema.Maximum = (decimal) maximum;
}
}
else
{
var maximum = (decimal) Convert.ChangeType(rangeAttribute.Maximum, typeof(decimal));
if (maximum < decimal.MaxValue)
{
schema.Maximum = maximum;
}
}
}
}
}

private object ConvertDefaultValue(Newtonsoft.Json.Serialization.JsonProperty property)
{
if (property.DefaultValue != null && property.DefaultValue.GetType().GetTypeInfo().IsEnum)
Expand Down

0 comments on commit 7eacf5f

Please sign in to comment.