diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs index b4b856c729..ef07f20503 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs @@ -13,13 +13,15 @@ public static JsonNode Create( { var type = schema?.ResolveType(schemaRepository); - var isStringType = type is { } value && - value.HasFlag(JsonSchemaType.String) && - !value.HasFlag(JsonSchemaType.Null); - if (isStringType) + if (string.Equals(exampleString, "null")) { - return string.Equals(exampleString, "null") ? JsonNullSentinel.JsonNull : JsonValue.Create(exampleString); + return JsonNullSentinel.JsonNull; + } + + if (type is { } value && value.HasFlag(JsonSchemaType.String)) + { + return JsonValue.Create(exampleString); } // HACK If the value is a string, but we can't detect it as one, then diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedRecord.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedRecord.cs index fc3e3b77cd..b77ae281b0 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedRecord.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedRecord.cs @@ -2,6 +2,7 @@ namespace Swashbuckle.AspNetCore.SwaggerGen.Test; +#nullable enable /// /// Summary for XmlAnnotatedRecord /// @@ -13,7 +14,10 @@ namespace Swashbuckle.AspNetCore.SwaggerGen.Test; /// Summary for DateTimeProperty /// Summary for EnumProperty /// Summary for GuidProperty -/// Summary for Nullable StringPropertyWithNullExample +/// Summary for NullableStringPropertyWithNullExample +/// Summary for StringPropertyWithNullExample +/// Summary for NullableStringPropertyWithNotNullExample +/// Summary for NullableIntPropertyWithNotNullExample /// Summary for StringProperty /// Summary for StringPropertyWithUri /// Summary for ObjectProperty @@ -26,7 +30,10 @@ public record XmlAnnotatedRecord( DateTime DateTimeProperty, IntEnum EnumProperty, Guid GuidProperty, + string? NullableStringPropertyWithNullExample, string StringPropertyWithNullExample, + string? NullableStringPropertyWithNotNullExample, + int? NullableIntPropertyWithNotNullExample, string StringProperty, string StringPropertyWithUri, object ObjectProperty diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs index 36f52a885b..d56e0f2b76 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs @@ -1,7 +1,7 @@ using Swashbuckle.AspNetCore.TestSupport; namespace Swashbuckle.AspNetCore.SwaggerGen.Test; - +#nullable enable /// /// Summary for XmlAnnotatedType /// @@ -62,28 +62,40 @@ public class XmlAnnotatedType public Guid GuidProperty { get; set; } /// - /// Summary for Nullable StringPropertyWithNullExample + /// Summary for NullableStringPropertyWithNullExample /// /// null - public string StringPropertyWithNullExample { get; set; } + public string? NullableStringPropertyWithNullExample { get; set; } + + /// + /// Summary for NullableStringPropertyWithNotNullExample + /// + /// example + public string? NullableStringPropertyWithNotNullExample { get; set; } + + /// + /// Summary for StringPropertyWithNullExample + /// + /// null + public required string StringPropertyWithNullExample { get; set; } /// /// Summary for StringProperty /// /// Example for StringProperty - public string StringProperty { get; set; } + public required string StringProperty { get; set; } /// /// Summary for StringPropertyWithUri /// /// - public string StringPropertyWithUri { get; set; } + public required string StringPropertyWithUri { get; set; } /// /// Summary for ObjectProperty /// /// {"prop1": 1, "prop2": "foobar"} - public object ObjectProperty { get; set; } + public required object ObjectProperty { get; set; } /// /// Summary for AcceptsNothing @@ -125,19 +137,25 @@ public void AcceptsArrayOfConstructedGenericType(int?[] param) { } + /// + /// >Summary for NullableIntPropertyWithNotNullExample + /// + /// 3 + public int? NullableIntPropertyWithNotNullExample { get; set; } + /// /// Summary for NestedType /// public class NestedType { - public string Property { get; set; } + public required string Property { get; set; } public class InnerNestedType { /// /// Summary of DoubleNestedType.InnerType.Property /// - public string InnerProperty { get; set; } + public required string InnerProperty { get; set; } } } } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs index 7cc3f32e0c..e6ba7eaf3f 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs @@ -78,6 +78,12 @@ public void Apply_SetsDescription_FromPropertySummaryTag( { typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithUri), JsonSchemaTypes.String, "\"https://test.com/a?b=1\\u0026c=2\"" }, { typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), JsonSchemaTypes.String, "null" }, { typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.StringPropertyWithNullExample), JsonSchemaTypes.String, "null" }, + { typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringPropertyWithNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "null" }, + { typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableStringPropertyWithNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "null" }, + { typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableStringPropertyWithNotNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "\"example\"" }, + { typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableStringPropertyWithNotNullExample), JsonSchemaTypes.String | JsonSchemaType.Null, "\"example\"" }, + { typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.NullableIntPropertyWithNotNullExample), JsonSchemaTypes.Integer | JsonSchemaType.Null, "3" }, + { typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.NullableIntPropertyWithNotNullExample), JsonSchemaTypes.Integer | JsonSchemaType.Null, "3" }, }; [Theory]