Skip to content

Commit

Permalink
Only set exclusive ranges when set (#2960)
Browse files Browse the repository at this point in the history
Only set Exclusive Range when they are, do not set them when they are false.
  • Loading branch information
jgarciadelanoceda committed Jun 21, 2024
1 parent 2db53de commit f6c1e31
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ private static void ApplyRangeAttribute(OpenApiSchema schema, RangeAttribute ran
{
#if NET8_0_OR_GREATER

schema.ExclusiveMinimum = rangeAttribute.MinimumIsExclusive;
schema.ExclusiveMaximum = rangeAttribute.MaximumIsExclusive;
if (rangeAttribute.MinimumIsExclusive)
{
schema.ExclusiveMinimum = true;
}

if (rangeAttribute.MaximumIsExclusive)
{
schema.ExclusiveMaximum = true;
}

#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,13 @@ public void GenerateSchema_SetsValidationProperties_IfComplexTypeHasValidationAt
Assert.Equal(3, schema.Properties["StringWithLength"].MaxLength);
Assert.Equal(1, schema.Properties["ArrayWithLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithLength"].MaxItems);
Assert.Equal(true, schema.Properties["IntWithRange"].ExclusiveMinimum);
Assert.Equal(true, schema.Properties["IntWithRange"].ExclusiveMaximum);
Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMinimum);
Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMaximum);
Assert.Equal("byte", schema.Properties["StringWithBase64"].Format);
Assert.Equal("string", schema.Properties["StringWithBase64"].Type);
#endif
Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum);
Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum);
Assert.Equal(1, schema.Properties["IntWithRange"].Minimum);
Assert.Equal(10, schema.Properties["IntWithRange"].Maximum);
Assert.Equal("^[3-6]?\\d{12,15}$", schema.Properties["StringWithRegularExpression"].Pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,13 @@ public void GenerateSchema_SetsValidationProperties_IfComplexTypeHasValidationAt
Assert.Equal(3, schema.Properties["StringWithLength"].MaxLength);
Assert.Equal(1, schema.Properties["ArrayWithLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithLength"].MaxItems);
Assert.Equal(true, schema.Properties["IntWithRange"].ExclusiveMinimum);
Assert.Equal(true, schema.Properties["IntWithRange"].ExclusiveMaximum);
Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMinimum);
Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMaximum);
Assert.Equal("byte", schema.Properties["StringWithBase64"].Format);
Assert.Equal("string", schema.Properties["StringWithBase64"].Type);
#endif
Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum);
Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum);
Assert.Equal(1, schema.Properties["IntWithRange"].Minimum);
Assert.Equal(10, schema.Properties["IntWithRange"].Maximum);
Assert.Equal("^[3-6]?\\d{12,15}$", schema.Properties["StringWithRegularExpression"].Pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ public class TypeWithValidationAttributes
public string[] ArrayWithLength { get; set; }

[Range(1, 10, MinimumIsExclusive = true, MaximumIsExclusive = true)]
public int IntWithRange { get; set; }
public int IntWithExclusiveRange { get; set; }

[Base64String]
public string StringWithBase64 { get; set; }

#else
#endif

[Range(1, 10)]
public int IntWithRange { get; set; }

#endif

[RegularExpression("^[3-6]?\\d{12,15}$")]
public string StringWithRegularExpression { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class TypeWithValidationAttributesViaMetadataType

public string StringWithBase64 { get; set; }

public double IntWithExclusiveRange { get; set; }

#endif

public int IntWithRange { get; set; }
Expand Down Expand Up @@ -53,18 +55,16 @@ public class MetadataType
public string[] ArrayWithLength { get; set; }

[Range(1, 10, MinimumIsExclusive = true, MaximumIsExclusive = true)]
public int IntWithRange { get; set; }
public int IntWithExclusiveRange { get; set; }

[Base64String]
public string StringWithBase64 { get; set; }

#else
#endif

[Range(1, 10)]
public int IntWithRange { get; set; }

#endif

[RegularExpression("^[3-6]?\\d{12,15}$")]
public string StringWithRegularExpression { get; set; }

Expand Down

0 comments on commit f6c1e31

Please sign in to comment.