diff --git a/src/DotSwashbuckle.AspNetCore.SwaggerGen/XmlComments/ExampleParser.cs b/src/DotSwashbuckle.AspNetCore.SwaggerGen/XmlComments/ExampleParser.cs index fdb03cbf4a..26dc344c32 100644 --- a/src/DotSwashbuckle.AspNetCore.SwaggerGen/XmlComments/ExampleParser.cs +++ b/src/DotSwashbuckle.AspNetCore.SwaggerGen/XmlComments/ExampleParser.cs @@ -14,13 +14,26 @@ SchemaRepository schemaRepository ) { var resolvedType = schema?.ResolveType(schemaRepository); - var shouldBeQuoted = ( + var isStringSchema = ( string.Equals(resolvedType, "string", StringComparison.Ordinal) || string.Equals(resolvedType, "array", StringComparison.Ordinal) ) && !string.Equals(example, "null", StringComparison.Ordinal); var exampleValue = WhiteSpaceCleaner.Condense(example); - return OpenApiAnyFactory.CreateFromJson(shouldBeQuoted ? $"\"{exampleValue}\"" : exampleValue); + + var serializedSchema = OpenApiAnyFactory.CreateFromJson(exampleValue); + + if (serializedSchema != null) + { + return serializedSchema; + } + + if (isStringSchema) + { + return new OpenApiString(exampleValue); + } + + return null; } } } diff --git a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/DotSwashbuckle.AspNetCore.SwaggerGen.Test.xml b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/DotSwashbuckle.AspNetCore.SwaggerGen.Test.xml index 1b730f6a6e..b0213726e3 100644 --- a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/DotSwashbuckle.AspNetCore.SwaggerGen.Test.xml +++ b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/DotSwashbuckle.AspNetCore.SwaggerGen.Test.xml @@ -269,6 +269,20 @@ Then Publication = 333284-2023 + + + For test only + + For test only + "Test-Test-Test" + + + + For test only + + For test only + {"someKey": "someValue"} + Summary for StringPropertyWithUri diff --git a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs index 314cb21ce7..2abce1d9bc 100644 --- a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs +++ b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/XmlAnnotatedType.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.Serialization; using DotSwashbuckle.AspNetCore.TestSupport; namespace DotSwashbuckle.AspNetCore.SwaggerGen.Test @@ -80,6 +81,22 @@ public class XmlAnnotatedType /// public string MultiLineSummaryExample { get; set; } + /// + /// For test only + /// + /// For test only + /// "Test-Test-Test" + [DataMember(Name = "testProperty", EmitDefaultValue = false)] + public string QuoteProperty { get; set; } + + /// + /// For test only + /// + /// For test only + /// {"someKey": "someValue"} + [DataMember(Name = "testProperty", EmitDefaultValue = false)] + public string KvpExampleProperty { get; set; } + /// /// Summary for StringPropertyWithUri /// diff --git a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs index f239f7c6e9..4af1deb06e 100644 --- a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs +++ b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsSchemaFilterTests.cs @@ -74,6 +74,8 @@ public void Apply_SetsDescription_FromPropertySummaryTag( [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), "string", "null")] [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithUri), "string", "\"https://test.com/a?b=1&c=2\"")] [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.MultiLineSummaryExample), "string", "\"When Number equals 2023/S 106-333284 Then Publication = 333284-2023\"")] + [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.QuoteProperty), "string", "\"Test-Test-Test\"")] + [InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.KvpExampleProperty), "string", "{\n \"someKey\": \"someValue\"\n}")] [InlineData(typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.BoolProperty), "boolean", "true")] [InlineData(typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.IntProperty), "integer", "10")] [InlineData(typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.LongProperty), "integer", "4294967295")]