Skip to content

Commit ea2cbee

Browse files
jgarciadelanocedaJavier García de la Noceda Argüelles
andauthored
Fix null examples being represented as the string null (#3788)
Fix null examples being represented as the string null. Co-authored-by: Javier García de la Noceda Argüelles <jgarcian@riamoneytransfer.com>
1 parent 008d676 commit ea2cbee

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static JsonNode Create(
1919

2020
if (isStringType)
2121
{
22-
return JsonValue.Create(exampleString);
22+
return string.Equals(exampleString, "null") ? null : JsonValue.Create(exampleString);
2323
}
2424

2525
// HACK If the value is a string, but we can't detect it as one, then

test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsExampleHelperTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ public void Create_Returns_Null_When_Type_String_And_Value_Null_String_Literal()
8080
var example = XmlCommentsExampleHelper.Create(
8181
schemaRepository, schema, "null");
8282

83-
Assert.NotNull(example);
84-
Assert.Equal("null", example.GetValue<string>());
83+
Assert.Null(example);
8584
}
8685

8786
[Fact]

test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public void Apply_SetsDescription_FromPropertySummaryTag(
7676
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.ObjectProperty), JsonSchemaTypes.Object, "{\n \"prop1\": 1,\n \"prop2\": \"foobar\"\n}" },
7777
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithUri), JsonSchemaTypes.String, "\"https://test.com/a?b=1\\u0026c=2\"" },
7878
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithUri), JsonSchemaTypes.String, "\"https://test.com/a?b=1\\u0026c=2\"" },
79-
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), JsonSchemaTypes.String, "\"null\"" },
80-
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithNullExample), JsonSchemaTypes.String, "\"null\"" },
79+
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), JsonSchemaTypes.String, null },
80+
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithNullExample), JsonSchemaTypes.String, null },
8181
};
8282

8383
[Theory]

0 commit comments

Comments
 (0)