diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs index 0d8fc813a8..0c4391fe52 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs @@ -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) @@ -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; diff --git a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs index 11d284ab6d..2a1529888d 100644 --- a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs @@ -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); diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs index 3771bfb46c..bfe407c63b 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -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); diff --git a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs index 9ee38bd305..9687d571ca 100644 --- a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs +++ b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs @@ -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; } diff --git a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs index 603605e3b5..18c96ca157 100644 --- a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs +++ b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs @@ -18,6 +18,8 @@ public class TypeWithValidationAttributesViaMetadataType public string[] ArrayWithLength { get; set; } + public string StringWithBase64 { get; set; } + #endif public int IntWithRange { get; set; } @@ -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; }