Skip to content

Commit bf991e8

Browse files
Fix null examples not working (#3793)
Follow-up to #3788 to resolve #3784 correctly.
1 parent b9c3899 commit bf991e8

8 files changed

+27
-14
lines changed

src/Shared/JsonExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json;
22
using System.Text.Json.Nodes;
3+
using Microsoft.OpenApi;
34

45
namespace Swashbuckle.AspNetCore;
56

@@ -15,6 +16,10 @@ internal static class JsonExtensions
1516

1617
public static string ToJson(this JsonNode value)
1718
{
19+
if (value.IsJsonNullSentinel())
20+
{
21+
return "null";
22+
}
1823
var json = value.ToJsonString(Options);
1924

2025
#if !NET9_0_OR_GREATER

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 string.Equals(exampleString, "null") ? null : JsonValue.Create(exampleString);
22+
return string.Equals(exampleString, "null") ? JsonNullSentinel.JsonNull : 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.IntegrationTests/snapshots/10_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,10 @@
801801
"description": "3-letter ISO country code",
802802
"required": true,
803803
"schema": {
804-
"type": "string"
805-
}
804+
"type": "string",
805+
"example": null
806+
},
807+
"example": null
806808
},
807809
{
808810
"name": "city",

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/8_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,10 @@
801801
"description": "3-letter ISO country code",
802802
"required": true,
803803
"schema": {
804-
"type": "string"
805-
}
804+
"type": "string",
805+
"example": null
806+
},
807+
"example": null
806808
},
807809
{
808810
"name": "city",

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/9_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,10 @@
801801
"description": "3-letter ISO country code",
802802
"required": true,
803803
"schema": {
804-
"type": "string"
805-
}
804+
"type": "string",
805+
"example": null
806+
},
807+
"example": null
806808
},
807809
{
808810
"name": "city",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ 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.Null(example);
83+
Assert.NotNull(example);
84+
Assert.Equal("null", example.ToJson());
8485
}
8586

8687
[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]

test/WebSites/Basic/Controllers/FromQueryParamsController.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ public class FromQueryParamsController
1212
public IActionResult ValidateAddress([FromQuery] Address address)
1313
{
1414
Debug.Assert(address is not null);
15-
return new NoContentResult();
15+
return new NoContentResult();
1616
}
1717

1818
[HttpGet("zip-codes/validate")]
1919
public IActionResult ValidateZipCodes(
20-
[FromQuery]IEnumerable<string> zipCodes,
21-
[FromQuery(Name = "search")] [Required] Dictionary<string, string> parameters)
20+
[FromQuery] IEnumerable<string> zipCodes,
21+
[FromQuery(Name = "search")][Required] Dictionary<string, string> parameters)
2222
{
2323
Debug.Assert(zipCodes is not null);
2424
Debug.Assert(parameters is not null);
25-
return new NoContentResult();
25+
return new NoContentResult();
2626
}
2727
}
2828

@@ -31,6 +31,7 @@ public class Address
3131
/// <summary>
3232
/// 3-letter ISO country code
3333
/// </summary>
34+
/// <example>null</example>
3435
[Required]
3536
public string Country { get; set; }
3637

0 commit comments

Comments
 (0)