From 0e0c69068cf29ce054af235e7fbbcf57c62e306a Mon Sep 17 00:00:00 2001 From: Dunnymeister Date: Tue, 29 Jun 2021 10:02:47 +0800 Subject: [PATCH 1/6] Unit test to show parsed example value that parses to null does not work correctly --- .../SwaggerGenerator/OpenApiAnyFactoryTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs index e43911380..e40e51be8 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs @@ -26,6 +26,18 @@ public void CreateFromJson_SimpleType(string json, Type expectedType, object exp Assert.Equal(expectedValue, actualValue); } + [Theory] + [InlineData("null", typeof(OpenApiNull), null)] + public void CreateFromJson_NullType(string json, Type expectedType, object expectedValue) + { + var openApiAnyObject = OpenApiAnyFactory.CreateFromJson(json); + Assert.NotNull(openApiAnyObject); + Assert.Equal(expectedType, openApiAnyObject.GetType()); + Assert.Equal(AnyType.Null, openApiAnyObject.AnyType); + var valueProperty = expectedType.GetProperty("Value"); + Assert.Equal(expectedValue, valueProperty); + } + [Theory] [InlineData("[1,2]", typeof(OpenApiInteger), 1, 2)] [InlineData("[4294877294,4294877295]", typeof(OpenApiLong), 4294877294L, 4294877295L)] From babad77c8cd6b8005292ea71749d19bcc5965b21 Mon Sep 17 00:00:00 2001 From: Dunnymeister Date: Tue, 29 Jun 2021 09:58:27 +0800 Subject: [PATCH 2/6] Returning OpenApiNull when the parsed json is of type Null --- .../SwaggerGenerator/OpenApiAnyFactory.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/OpenApiAnyFactory.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/OpenApiAnyFactory.cs index c1cc5f987..b28aa2195 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/OpenApiAnyFactory.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/OpenApiAnyFactory.cs @@ -44,6 +44,9 @@ private static IOpenApiAny CreateOpenApiObject(JsonElement jsonElement) private static IOpenApiAny CreateFromJsonElement(JsonElement jsonElement) { + if (jsonElement.ValueKind == JsonValueKind.Null) + return new OpenApiNull(); + if (jsonElement.ValueKind == JsonValueKind.True || jsonElement.ValueKind == JsonValueKind.False) return new OpenApiBoolean(jsonElement.GetBoolean()); From b8dcad37b76b9c472d3aab5254f2b3c2b609ab14 Mon Sep 17 00:00:00 2001 From: Dunnymeister Date: Tue, 29 Jun 2021 14:57:05 +0800 Subject: [PATCH 3/6] Unit test to show XmlCommentsSchemaFilter does not treat nullable string properties with null values correctly --- .../Fixtures/XmlAnnotatedType.cs | 6 ++++++ .../Swashbuckle.AspNetCore.SwaggerGen.Test.xml | 6 ++++++ .../XmlComments/XmlCommentsSchemaFilterTests.cs | 1 + 3 files changed, 13 insertions(+) diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs index 417a68759..b8d9c88eb 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs @@ -57,6 +57,12 @@ public class XmlAnnotatedType /// d3966535-2637-48fa-b911-e3c27405ee09 public Guid GuidProperty { get; set; } + /// + /// Summary for Nullable StringProperty + /// + /// null + public string NullableStringProperty { get; set; } + /// /// Summary for StringProperty /// diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml index 73191b95c..4e7d86ab8 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml @@ -127,6 +127,12 @@ d3966535-2637-48fa-b911-e3c27405ee09 + + + Summary for Nullable StringProperty + + null + Summary for StringProperty diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs index 5a37272fb..284811b37 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs @@ -66,6 +66,7 @@ public void Apply_SetsDescription_FromFieldSummaryTag() [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.GuidProperty), "string", "\"d3966535-2637-48fa-b911-e3c27405ee09\"")] [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringProperty), "string", "\"Example for StringProperty\"")] [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.ObjectProperty), "object", "{\n \"prop1\": 1,\n \"prop2\": \"foobar\"\n}")] + [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringProperty), "string", "null")] [UseInvariantCulture] public void Apply_SetsExample_FromPropertyExampleTag( Type declaringType, From 30f9bf9aa2b8cf01e5f2ae96729f654b73a682ae Mon Sep 17 00:00:00 2001 From: Dunnymeister Date: Tue, 29 Jun 2021 15:38:26 +0800 Subject: [PATCH 4/6] XmlComment example tags for string properties where the example is null will now resolve to a null value instead of the string "null" --- .../XmlComments/XmlCommentsSchemaFilter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsSchemaFilter.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsSchemaFilter.cs index 8acb85550..7683e5174 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsSchemaFilter.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsSchemaFilter.cs @@ -48,7 +48,7 @@ private void ApplyMemberTags(OpenApiSchema schema, SchemaFilterContext context) var exampleNode = fieldOrPropertyNode.SelectSingleNode("example"); if (exampleNode != null) { - var exampleAsJson = (schema.ResolveType(context.SchemaRepository) == "string") + var exampleAsJson = (schema.ResolveType(context.SchemaRepository) == "string") && !exampleNode.Value.Equals("null") ? $"\"{exampleNode.InnerXml}\"" : exampleNode.InnerXml; From cee9d0f16cd57b623a541b973fe931d02bf03bf5 Mon Sep 17 00:00:00 2001 From: Dunnymeister Date: Tue, 29 Jun 2021 23:24:50 +0800 Subject: [PATCH 5/6] Renaming NullableStringProperty to StringPropertyWithNullExample for clarity --- .../Fixtures/XmlAnnotatedType.cs | 4 ++-- .../Swashbuckle.AspNetCore.SwaggerGen.Test.xml | 4 ++-- .../XmlComments/XmlCommentsSchemaFilterTests.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs index b8d9c88eb..661ee115f 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs @@ -58,10 +58,10 @@ public class XmlAnnotatedType public Guid GuidProperty { get; set; } /// - /// Summary for Nullable StringProperty + /// Summary for Nullable StringPropertyWithNullExample /// /// null - public string NullableStringProperty { get; set; } + public string StringPropertyWithNullExample { get; set; } /// /// Summary for StringProperty diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml index 4e7d86ab8..fa859460e 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml @@ -127,9 +127,9 @@ d3966535-2637-48fa-b911-e3c27405ee09 - + - Summary for Nullable StringProperty + Summary for Nullable StringPropertyWithNullExample null diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs index 284811b37..6c2c8dcd6 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs @@ -66,7 +66,7 @@ public void Apply_SetsDescription_FromFieldSummaryTag() [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.GuidProperty), "string", "\"d3966535-2637-48fa-b911-e3c27405ee09\"")] [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringProperty), "string", "\"Example for StringProperty\"")] [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.ObjectProperty), "object", "{\n \"prop1\": 1,\n \"prop2\": \"foobar\"\n}")] - [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringProperty), "string", "null")] + [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), "string", "null")] [UseInvariantCulture] public void Apply_SetsExample_FromPropertyExampleTag( Type declaringType, From 97357ebfb321c39be8ba3dcca5cb204f3285a656 Mon Sep 17 00:00:00 2001 From: Dunnymeister Date: Tue, 29 Jun 2021 23:31:00 +0800 Subject: [PATCH 6/6] Changing unit test that was marked as Theory with a single test case to a Fact --- .../SwaggerGenerator/OpenApiAnyFactoryTests.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs index e40e51be8..260dc4b20 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiAnyFactoryTests.cs @@ -26,16 +26,17 @@ public void CreateFromJson_SimpleType(string json, Type expectedType, object exp Assert.Equal(expectedValue, actualValue); } - [Theory] - [InlineData("null", typeof(OpenApiNull), null)] - public void CreateFromJson_NullType(string json, Type expectedType, object expectedValue) + [Fact] + public void CreateFromJson_NullType() { - var openApiAnyObject = OpenApiAnyFactory.CreateFromJson(json); + var expectedType = typeof(OpenApiNull); + + var openApiAnyObject = OpenApiAnyFactory.CreateFromJson("null"); Assert.NotNull(openApiAnyObject); Assert.Equal(expectedType, openApiAnyObject.GetType()); Assert.Equal(AnyType.Null, openApiAnyObject.AnyType); var valueProperty = expectedType.GetProperty("Value"); - Assert.Equal(expectedValue, valueProperty); + Assert.Null(valueProperty); } [Theory]