Skip to content

Commit 4dfabb4

Browse files
jgarciadelanocedadldl-cmdmartincostello
authored
Fix null examples (#3803)
Treat the word "null" as meaning `null`. --------- Co-authored-by: Javier García de la Noceda Argüelles <6615999+jgarciadelanoceda@users.noreply.github.com> Co-authored-by: dldl-cmd <76129819+dldl-cmd@users.noreply.github.com> Co-authored-by: Martin Costello <martin@martincostello.com>
1 parent 70b291f commit 4dfabb4

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ public static JsonNode Create(
1313
{
1414
var type = schema?.ResolveType(schemaRepository);
1515

16-
var isStringType = type is { } value &&
17-
value.HasFlag(JsonSchemaType.String) &&
18-
!value.HasFlag(JsonSchemaType.Null);
1916

20-
if (isStringType)
17+
if (string.Equals(exampleString, "null"))
2118
{
22-
return string.Equals(exampleString, "null") ? JsonNullSentinel.JsonNull : JsonValue.Create(exampleString);
19+
return JsonNullSentinel.JsonNull;
20+
}
21+
22+
if (type is { } value && value.HasFlag(JsonSchemaType.String))
23+
{
24+
return JsonValue.Create(exampleString);
2325
}
2426

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

test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedRecord.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
44

5+
#nullable enable
56
/// <summary>
67
/// Summary for XmlAnnotatedRecord
78
/// </summary>
@@ -13,7 +14,10 @@ namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
1314
/// <param name="DateTimeProperty" example="6/22/2022 12:00:00 AM">Summary for DateTimeProperty</param>
1415
/// <param name="EnumProperty" example="2">Summary for EnumProperty</param>
1516
/// <param name="GuidProperty" example="d3966535-2637-48fa-b911-e3c27405ee09">Summary for GuidProperty</param>
16-
/// <param name="StringPropertyWithNullExample" example="null">Summary for Nullable StringPropertyWithNullExample</param>
17+
/// <param name="NullableStringPropertyWithNullExample" example="null">Summary for NullableStringPropertyWithNullExample</param>
18+
/// <param name="StringPropertyWithNullExample" example="null">Summary for StringPropertyWithNullExample</param>
19+
/// <param name="NullableStringPropertyWithNotNullExample" example="example">Summary for NullableStringPropertyWithNotNullExample</param>
20+
/// <param name="NullableIntPropertyWithNotNullExample" example="3">Summary for NullableIntPropertyWithNotNullExample</param>
1721
/// <param name="StringProperty" example="Example for StringProperty">Summary for StringProperty</param>
1822
/// <param name="StringPropertyWithUri" example="https://test.com/a?b=1&amp;c=2">Summary for StringPropertyWithUri</param>
1923
/// <param name="ObjectProperty" example="{&quot;prop1&quot;: 1, &quot;prop2&quot;: &quot;foobar&quot;}">Summary for ObjectProperty</param>
@@ -26,7 +30,10 @@ public record XmlAnnotatedRecord(
2630
DateTime DateTimeProperty,
2731
IntEnum EnumProperty,
2832
Guid GuidProperty,
33+
string? NullableStringPropertyWithNullExample,
2934
string StringPropertyWithNullExample,
35+
string? NullableStringPropertyWithNotNullExample,
36+
int? NullableIntPropertyWithNotNullExample,
3037
string StringProperty,
3138
string StringPropertyWithUri,
3239
object ObjectProperty

test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Swashbuckle.AspNetCore.TestSupport;
22

33
namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
4-
4+
#nullable enable
55
/// <summary>
66
/// Summary for XmlAnnotatedType
77
/// </summary>
@@ -62,28 +62,40 @@ public class XmlAnnotatedType
6262
public Guid GuidProperty { get; set; }
6363

6464
/// <summary>
65-
/// Summary for Nullable StringPropertyWithNullExample
65+
/// Summary for NullableStringPropertyWithNullExample
6666
/// </summary>
6767
/// <example>null</example>
68-
public string StringPropertyWithNullExample { get; set; }
68+
public string? NullableStringPropertyWithNullExample { get; set; }
69+
70+
/// <summary>
71+
/// Summary for NullableStringPropertyWithNotNullExample
72+
/// </summary>
73+
/// <example>example</example>
74+
public string? NullableStringPropertyWithNotNullExample { get; set; }
75+
76+
/// <summary>
77+
/// Summary for StringPropertyWithNullExample
78+
/// </summary>
79+
/// <example>null</example>
80+
public required string StringPropertyWithNullExample { get; set; }
6981

7082
/// <summary>
7183
/// Summary for StringProperty
7284
/// </summary>
7385
/// <example>Example for StringProperty</example>
74-
public string StringProperty { get; set; }
86+
public required string StringProperty { get; set; }
7587

7688
/// <summary>
7789
/// Summary for StringPropertyWithUri
7890
/// </summary>
7991
/// <example><![CDATA[https://test.com/a?b=1&c=2]]></example>
80-
public string StringPropertyWithUri { get; set; }
92+
public required string StringPropertyWithUri { get; set; }
8193

8294
/// <summary>
8395
/// Summary for ObjectProperty
8496
/// </summary>
8597
/// <example>{"prop1": 1, "prop2": "foobar"}</example>
86-
public object ObjectProperty { get; set; }
98+
public required object ObjectProperty { get; set; }
8799

88100
/// <summary>
89101
/// Summary for AcceptsNothing
@@ -125,19 +137,25 @@ public void AcceptsArrayOfConstructedGenericType(int?[] param)
125137
{
126138
}
127139

140+
/// <summary>
141+
/// >Summary for NullableIntPropertyWithNotNullExample
142+
/// </summary>
143+
/// <example>3</example>
144+
public int? NullableIntPropertyWithNotNullExample { get; set; }
145+
128146
/// <summary>
129147
/// Summary for NestedType
130148
/// </summary>
131149
public class NestedType
132150
{
133-
public string Property { get; set; }
151+
public required string Property { get; set; }
134152

135153
public class InnerNestedType
136154
{
137155
/// <summary>
138156
/// Summary of DoubleNestedType.InnerType.Property
139157
/// </summary>
140-
public string InnerProperty { get; set; }
158+
public required string InnerProperty { get; set; }
141159
}
142160
}
143161
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public void Apply_SetsDescription_FromPropertySummaryTag(
7878
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithUri), JsonSchemaTypes.String, "\"https://test.com/a?b=1\\u0026c=2\"" },
7979
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), JsonSchemaTypes.String, "null" },
8080
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithNullExample), JsonSchemaTypes.String, "null" },
81+
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringPropertyWithNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "null" },
82+
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableStringPropertyWithNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "null" },
83+
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringPropertyWithNotNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "\"example\"" },
84+
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableStringPropertyWithNotNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "\"example\"" },
85+
{ typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableIntPropertyWithNotNullExample), JsonSchemaTypes.Integer | JsonSchemaType.Null, "3" },
86+
{ typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableIntPropertyWithNotNullExample), JsonSchemaTypes.Integer | JsonSchemaType.Null, "3" },
8187
};
8288

8389
[Theory]

0 commit comments

Comments
 (0)