Skip to content

Commit

Permalink
Add support for .NET 8 model state attributes (#2958)
Browse files Browse the repository at this point in the history
Adds support for `[Base64String]` and `MinimumIsExclusive` and `MaximumIsExclusive` properties for `[Range]`.
  • Loading branch information
jgarciadelanoceda committed Jun 21, 2024
1 parent 5303636 commit 2db53de
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public static void ApplyValidationAttributes(this OpenApiSchema schema, IEnumera
else if (attribute is LengthAttribute lengthAttribute)
ApplyLengthAttribute(schema, lengthAttribute);

else if (attribute is Base64StringAttribute base64Attribute)
ApplyBase64Attribute(schema);

#endif

else if (attribute is RangeAttribute rangeAttribute)
Expand Down Expand Up @@ -169,10 +172,22 @@ private static void ApplyLengthAttribute(OpenApiSchema schema, LengthAttribute l
}
}

private static void ApplyBase64Attribute(OpenApiSchema schema)
{
schema.Format = "byte";
}

#endif

private static void ApplyRangeAttribute(OpenApiSchema schema, RangeAttribute rangeAttribute)
{
#if NET8_0_OR_GREATER

schema.ExclusiveMinimum = rangeAttribute.MinimumIsExclusive;
schema.ExclusiveMaximum = rangeAttribute.MaximumIsExclusive;

#endif

schema.Maximum = decimal.TryParse(rangeAttribute.Maximum.ToString(), out decimal maximum)
? maximum
: schema.Maximum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ 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("byte", schema.Properties["StringWithBase64"].Format);
Assert.Equal("string", schema.Properties["StringWithBase64"].Type);
#endif
Assert.Equal(1, schema.Properties["IntWithRange"].Minimum);
Assert.Equal(10, schema.Properties["IntWithRange"].Maximum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ 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("byte", schema.Properties["StringWithBase64"].Format);
Assert.Equal("string", schema.Properties["StringWithBase64"].Type);
#endif
Assert.Equal(1, schema.Properties["IntWithRange"].Minimum);
Assert.Equal(10, schema.Properties["IntWithRange"].Maximum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ public class TypeWithValidationAttributes
[Length(1, 3)]
public string[] ArrayWithLength { get; set; }

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

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

#else

[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 @@ -18,6 +18,8 @@ public class TypeWithValidationAttributesViaMetadataType

public string[] ArrayWithLength { get; set; }

public string StringWithBase64 { get; set; }

#endif

public int IntWithRange { get; set; }
Expand Down Expand Up @@ -50,11 +52,19 @@ public class MetadataType
[Length(1, 3)]
public string[] ArrayWithLength { get; set; }

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

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

#else

[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 2db53de

Please sign in to comment.