Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for .Net8 Model State Attributes #1

Closed
wants to merge 1 commit into from
Closed
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 @@ -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,23 @@ private static void ApplyLengthAttribute(OpenApiSchema schema, LengthAttribute l
}
}

private static void ApplyBase64Attribute(OpenApiSchema schema)
{
if (schema.Type == "string")
{
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,9 @@ 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);
#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,9 @@ 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);
#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 @@ -20,11 +20,16 @@ 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 @@ -17,6 +17,7 @@ public class TypeWithValidationAttributesViaMetadataType
public string StringWithLength { get; set; }

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

#endif

Expand Down Expand Up @@ -46,14 +47,18 @@ public class MetadataType

[Length(1, 3)]
public string StringWithLength { get; set; }

[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