From 757a8495f55d749d4e4d34f67768f399ea6cd2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Garc=C3=ADa=20de=20la=20Noceda=20Arg=C3=BCelles?= Date: Sat, 22 Jun 2024 00:11:51 +0200 Subject: [PATCH 1/2] Only set Exclusive Range when they are, do not set them when they are false --- .../SchemaGenerator/OpenApiSchemaExtensions.cs | 11 +++++++++-- .../SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs | 9 +++++++-- .../JsonSerializerSchemaGeneratorTests.cs | 9 +++++++-- .../Fixtures/TypeWithValidationAttributes.cs | 6 ++---- .../TypeWithValidationAttributesViaMetadataType.cs | 8 ++++---- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs index 0c4391fe52..92b2d51855 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs @@ -183,8 +183,15 @@ private static void ApplyRangeAttribute(OpenApiSchema schema, RangeAttribute ran { #if NET8_0_OR_GREATER - schema.ExclusiveMinimum = rangeAttribute.MinimumIsExclusive; - schema.ExclusiveMaximum = rangeAttribute.MaximumIsExclusive; + if(rangeAttribute.MinimumIsExclusive) + { + schema.ExclusiveMinimum = true; + } + + if(rangeAttribute.MaximumIsExclusive) + { + schema.ExclusiveMaximum = true; + } #endif diff --git a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs index 2a1529888d..571aca9898 100644 --- a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs @@ -322,10 +322,15 @@ 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); + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); + Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMinimum); + Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMaximum); +#else + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); #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 bfe407c63b..74145d814a 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -345,10 +345,15 @@ 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); + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); + Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMinimum); + Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMaximum); +#else + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); + Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); #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 9687d571ca..ff23792668 100644 --- a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs +++ b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributes.cs @@ -22,18 +22,16 @@ public class TypeWithValidationAttributes public string[] ArrayWithLength { get; set; } [Range(1, 10, MinimumIsExclusive = true, MaximumIsExclusive = true)] - public int IntWithRange { get; set; } + public int IntWithExclusiveRange { get; set; } [Base64String] public string StringWithBase64 { get; set; } -#else +#endif [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 18c96ca157..2cda5471da 100644 --- a/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs +++ b/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithValidationAttributesViaMetadataType.cs @@ -20,6 +20,8 @@ public class TypeWithValidationAttributesViaMetadataType public string StringWithBase64 { get; set; } + public double IntWithExclusiveRange { get; set; } + #endif public int IntWithRange { get; set; } @@ -53,18 +55,16 @@ public class MetadataType public string[] ArrayWithLength { get; set; } [Range(1, 10, MinimumIsExclusive = true, MaximumIsExclusive = true)] - public int IntWithRange { get; set; } + public int IntWithExclusiveRange { get; set; } [Base64String] public string StringWithBase64 { get; set; } -#else +#endif [Range(1, 10)] public int IntWithRange { get; set; } -#endif - [RegularExpression("^[3-6]?\\d{12,15}$")] public string StringWithRegularExpression { get; set; } From dc68588e2a30883ae274bb5b0e6a8137b0748216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Garc=C3=ADa=20de=20la=20Noceda=20Arg=C3=BCelles?= Date: Sat, 22 Jun 2024 00:26:03 +0200 Subject: [PATCH 2/2] No need of else because two conditions were the same --- .../SchemaGenerator/OpenApiSchemaExtensions.cs | 4 ++-- .../SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs | 9 +++------ .../JsonSerializerSchemaGeneratorTests.cs | 9 +++------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs index 92b2d51855..c6bf6bf059 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs @@ -183,12 +183,12 @@ private static void ApplyRangeAttribute(OpenApiSchema schema, RangeAttribute ran { #if NET8_0_OR_GREATER - if(rangeAttribute.MinimumIsExclusive) + if (rangeAttribute.MinimumIsExclusive) { schema.ExclusiveMinimum = true; } - if(rangeAttribute.MaximumIsExclusive) + if (rangeAttribute.MaximumIsExclusive) { schema.ExclusiveMaximum = true; } diff --git a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs index 571aca9898..b5d831c275 100644 --- a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs @@ -322,16 +322,13 @@ 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("byte", schema.Properties["StringWithBase64"].Format); - Assert.Equal("string", schema.Properties["StringWithBase64"].Type); - Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); - Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMinimum); Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMaximum); -#else + Assert.Equal("byte", schema.Properties["StringWithBase64"].Format); + Assert.Equal("string", schema.Properties["StringWithBase64"].Type); +#endif Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); -#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); diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs index 74145d814a..11d1be49a4 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -345,16 +345,13 @@ 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("byte", schema.Properties["StringWithBase64"].Format); - Assert.Equal("string", schema.Properties["StringWithBase64"].Type); - Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); - Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMinimum); Assert.Equal(true, schema.Properties["IntWithExclusiveRange"].ExclusiveMaximum); -#else + Assert.Equal("byte", schema.Properties["StringWithBase64"].Format); + Assert.Equal("string", schema.Properties["StringWithBase64"].Type); +#endif Assert.Null(schema.Properties["IntWithRange"].ExclusiveMinimum); Assert.Null(schema.Properties["IntWithRange"].ExclusiveMaximum); -#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);