diff --git a/src/DotSwashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs b/src/DotSwashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs index 850f55b..651eaee 100644 --- a/src/DotSwashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs +++ b/src/DotSwashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs @@ -16,6 +16,9 @@ public static void ApplyValidationAttributes(this OpenApiSchema schema, IEnumera if (attribute is DataTypeAttribute dataTypeAttribute) ApplyDataTypeAttribute(schema, dataTypeAttribute); + else if (attribute is LengthAttribute lengthAttribute) + ApplyLengthAttribute(schema, lengthAttribute); + else if (attribute is MinLengthAttribute minLengthAttribute) ApplyMinLengthAttribute(schema, minLengthAttribute); @@ -113,6 +116,20 @@ private static void ApplyDataTypeAttribute(OpenApiSchema schema, DataTypeAttribu } } + 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; + } + } + private static void ApplyMinLengthAttribute(OpenApiSchema schema, MinLengthAttribute minLengthAttribute) { if (schema.Type == "array") diff --git a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs index d9cac19..3228bce 100644 --- a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -355,6 +355,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); + + Assert.Equal(1, schema.Properties["ArrayWithLength"].MinItems); + Assert.Equal(3, schema.Properties["ArrayWithLength"].MaxItems); + Assert.Equal(1, schema.Properties["StringWithLength"].MinLength); + Assert.Equal(3, schema.Properties["StringWithLength"].MaxLength); + 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); diff --git a/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs b/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs index 50fe6f3..5424e1a 100644 --- a/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs +++ b/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs @@ -13,6 +13,12 @@ public class TypeWithValidationAttributes [MinLength(1), MaxLength(3)] public string[] ArrayWithMinMaxLength { get; set; } + [Length(1, 3)] + public string[] ArrayWithLength { get; set; } + + [Length(1, 3)] + public string StringWithLength { get; set; } + [Range(1, 10)] public int IntWithRange { get; set; } diff --git a/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs b/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs index 3c0feda..10b087b 100644 --- a/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs +++ b/test/DotSwashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs @@ -13,6 +13,10 @@ public class TypeWithValidationAttributesViaMetadataType public string[] ArrayWithMinMaxLength { get; set; } + public string[] ArrayWithLength { get; set; } + + public string StringWithLength { get; set; } + public int IntWithRange { get; set; } public string StringWithRegularExpression { get; set; } @@ -37,6 +41,12 @@ public class MetadataType [MinLength(1), MaxLength(3)] public string[] ArrayWithMinMaxLength { get; set; } + [Length(1, 3)] + public string[] ArrayWithLength { get; set; } + + [Length(1, 3)] + public string StringWithLength { get; set; } + [Range(1, 10)] public int IntWithRange { get; set; }