Skip to content

Commit

Permalink
Add support for the [Length]attribute. (#2882)
Browse files Browse the repository at this point in the history
Implement support for the `[Length]` attribute.
  • Loading branch information
satma0745 committed May 15, 2024
1 parent 4220ffb commit 674eac8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public static void ApplyValidationAttributes(this OpenApiSchema schema, IEnumera
else if (attribute is MaxLengthAttribute maxLengthAttribute)
ApplyMaxLengthAttribute(schema, maxLengthAttribute);

#if NET8_0_OR_GREATER

else if (attribute is LengthAttribute lengthAttribute)
ApplyLengthAttribute(schema, lengthAttribute);

#endif

else if (attribute is RangeAttribute rangeAttribute)
ApplyRangeAttribute(schema, rangeAttribute);

Expand Down Expand Up @@ -146,6 +153,24 @@ private static void ApplyMaxLengthRouteConstraint(OpenApiSchema schema, MaxLengt
schema.MaxLength = maxLengthRouteConstraint.MaxLength;
}

#if NET8_0_OR_GREATER

private static void ApplyLengthAttribute(OpenApiSchema schema, LengthAttribute lengthAttribute)
{
if (schema.Type == "array")
{
schema.MinItems = lengthAttribute.MinimumLength;
schema.MaxItems = lengthAttribute.MaximumLength;
}
else
{
schema.MinLength = lengthAttribute.MinimumLength;
schema.MaxLength = lengthAttribute.MaximumLength;
}
}

#endif

private static void ApplyRangeAttribute(OpenApiSchema schema, RangeAttribute rangeAttribute)
{
schema.Maximum = decimal.TryParse(rangeAttribute.Maximum.ToString(), out decimal maximum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ public void GenerateSchema_SetsDeprecatedFlag_IfPropertyHasObsoleteAttribute()
[Theory]
[InlineData(typeof(TypeWithValidationAttributes))]
[InlineData(typeof(TypeWithValidationAttributesViaMetadataType))]

public void GenerateSchema_SetsValidationProperties_IfComplexTypeHasValidationAttributes(Type type)
{
var schemaRepository = new SchemaRepository();
Expand All @@ -315,6 +314,12 @@ public void GenerateSchema_SetsValidationProperties_IfComplexTypeHasValidationAt
Assert.Equal(3, schema.Properties["StringWithMinMaxLength"].MaxLength);
Assert.Equal(1, schema.Properties["ArrayWithMinMaxLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithMinMaxLength"].MaxItems);
#if NET8_0_OR_GREATER
Assert.Equal(1, schema.Properties["StringWithLength"].MinLength);
Assert.Equal(3, schema.Properties["StringWithLength"].MaxLength);
Assert.Equal(1, schema.Properties["ArrayWithLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithLength"].MaxItems);
#endif
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 @@ -325,7 +325,6 @@ public void GenerateSchema_SetsDeprecatedFlag_IfPropertyHasObsoleteAttribute()
[Theory]
[InlineData(typeof(TypeWithValidationAttributes))]
[InlineData(typeof(TypeWithValidationAttributesViaMetadataType))]

public void GenerateSchema_SetsValidationProperties_IfComplexTypeHasValidationAttributes(Type type)
{
var schemaRepository = new SchemaRepository();
Expand All @@ -338,6 +337,12 @@ public void GenerateSchema_SetsValidationProperties_IfComplexTypeHasValidationAt
Assert.Equal(3, schema.Properties["StringWithMinMaxLength"].MaxLength);
Assert.Equal(1, schema.Properties["ArrayWithMinMaxLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithMinMaxLength"].MaxItems);
#if NET8_0_OR_GREATER
Assert.Equal(1, schema.Properties["StringWithLength"].MinLength);
Assert.Equal(3, schema.Properties["StringWithLength"].MaxLength);
Assert.Equal(1, schema.Properties["ArrayWithLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithLength"].MaxItems);
#endif
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 @@ -13,6 +13,16 @@ public class TypeWithValidationAttributes
[MinLength(1), MaxLength(3)]
public string[] ArrayWithMinMaxLength { get; set; }

#if NET8_0_OR_GREATER

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

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

#endif

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

Expand All @@ -28,4 +38,4 @@ public class TypeWithValidationAttributes
[Required(AllowEmptyStrings = true)]
public string StringWithRequiredAllowEmptyTrue { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public class TypeWithValidationAttributesViaMetadataType

public string[] ArrayWithMinMaxLength { get; set; }

#if NET8_0_OR_GREATER

public string StringWithLength { get; set; }

public string[] ArrayWithLength { get; set; }

#endif

public int IntWithRange { get; set; }

public string StringWithRegularExpression { get; set; }
Expand All @@ -34,6 +42,16 @@ public class MetadataType
[MinLength(1), MaxLength(3)]
public string[] ArrayWithMinMaxLength { get; set; }

#if NET8_0_OR_GREATER

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

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

#endif

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

Expand All @@ -49,4 +67,4 @@ public class MetadataType
[Required(AllowEmptyStrings = true)]
public string StringWithRequiredAllowEmptyTrue { get; set; }
}
}
}

0 comments on commit 674eac8

Please sign in to comment.