diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsParameterFilter.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsParameterFilter.cs index 441b7e8e9d..5a63b5a8e6 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsParameterFilter.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsParameterFilter.cs @@ -73,10 +73,10 @@ private void ApplyParamTags(IOpenApiParameter parameter, ParameterFilterContext if (parameter is OpenApiParameter concrete) { - var example = paramNode.GetAttribute("example"); - if (!string.IsNullOrEmpty(example)) + var example = paramNode.SelectSingleNode("@example"); + if (example != null) { - concrete.Example = XmlCommentsExampleHelper.Create(context.SchemaRepository, parameter.Schema, example); + concrete.Example = XmlCommentsExampleHelper.Create(context.SchemaRepository, parameter.Schema, example.ToString()); } } } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeConstructedControllerWithXmlComments.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeConstructedControllerWithXmlComments.cs index ea1eb810cb..7ef2e63508 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeConstructedControllerWithXmlComments.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeConstructedControllerWithXmlComments.cs @@ -20,8 +20,10 @@ public void ActionWithSummaryAndResponseTags(T param) } /// Description for param1 - /// Description for param2 - public void ActionWithParamTags(T param1, T param2) + /// Description for param2 + /// Description for param3 with empty example + /// + public void ActionWithParamTags(T param1, T param2, T param3, T param4) { } } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeControllerWithXmlComments.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeControllerWithXmlComments.cs index f41d63ec07..1972f1f8e7 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeControllerWithXmlComments.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeControllerWithXmlComments.cs @@ -20,7 +20,9 @@ public void ActionWithSummaryAndRemarksTags() /// Description for param1 /// Description for param2 - public void ActionWithParamTags(string param1, string param2) + /// Description for param3 with empty example + /// + public void ActionWithParamTags(string param1, string param2, string param3, string param4) { } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsParameterFilterTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsParameterFilterTests.cs index 31f4f1ef71..42ec239d2e 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsParameterFilterTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsParameterFilterTests.cs @@ -9,58 +9,44 @@ namespace Swashbuckle.AspNetCore.SwaggerGen.Test; public class XmlCommentsParameterFilterTests { - [Fact] - public void Apply_SetsDescriptionAndExample_FromActionParamTag() - { - var parameter = new OpenApiParameter { Schema = new OpenApiSchema { Type = JsonSchemaTypes.String } }; - var parameterInfo = typeof(FakeControllerWithXmlComments) - .GetMethod(nameof(FakeControllerWithXmlComments.ActionWithParamTags)) - .GetParameters()[0]; - var apiParameterDescription = new ApiParameterDescription { }; - var filterContext = new ParameterFilterContext(apiParameterDescription, null, null, null, parameterInfo: parameterInfo); - - Subject().Apply(parameter, filterContext); - - Assert.Equal("Description for param1", parameter.Description); - Assert.NotNull(parameter.Example); - - Assert.Equal("\"Example for \\u0022param1\\u0022\"", parameter.Example.ToJson()); - } - - [Fact] - public void Apply_SetsDescriptionAndExample_FromUriTypeActionParamTag() + [Theory] + [InlineData(0, "Description for param1", "\"Example for \\u0022param1\\u0022\"")] + [InlineData(1, "Description for param2", "\"http://test.com/?param1=1\\u0026param2=2\"")] + [InlineData(2, "Description for param3 with empty example", "\"\"")] + [InlineData(3, "", null)] + public void Apply_SetsDescriptionAndExample_FromActionParamTag(int p, string expectedDescription, string expectedExample) { var parameter = new OpenApiParameter { Schema = new OpenApiSchema { Type = JsonSchemaTypes.String } }; var parameterInfo = typeof(FakeControllerWithXmlComments) .GetMethod(nameof(FakeControllerWithXmlComments.ActionWithParamTags)) - .GetParameters()[1]; + .GetParameters()[p]; var apiParameterDescription = new ApiParameterDescription { }; var filterContext = new ParameterFilterContext(apiParameterDescription, null, null, null, parameterInfo: parameterInfo); Subject().Apply(parameter, filterContext); - Assert.Equal("Description for param2", parameter.Description); - Assert.NotNull(parameter.Example); - - Assert.Equal("\"http://test.com/?param1=1\\u0026param2=2\"", parameter.Example.ToJson()); + Assert.Equal(expectedDescription, parameter.Description); + Assert.Equal(expectedExample, parameter.Example?.ToJson()); } - [Fact] - public void Apply_SetsDescriptionAndExample_FromUnderlyingGenericTypeActionParamTag() + [Theory] + [InlineData(0, "Description for param1", "\"Example for \\u0022param1\\u0022\"")] + [InlineData(1, "Description for param2", "\"http://test.com/?param1=1\\u0026param2=2\"")] + [InlineData(2, "Description for param3 with empty example", "\"\"")] + [InlineData(3, "", null)] + public void Apply_SetsDescriptionAndExample_FromUnderlyingGenericTypeActionParamTag(int p, string expectedDescription, string expectedExample) { var parameter = new OpenApiParameter { Schema = new OpenApiSchema { Type = JsonSchemaTypes.String } }; var parameterInfo = typeof(FakeConstructedControllerWithXmlComments) .GetMethod(nameof(FakeConstructedControllerWithXmlComments.ActionWithParamTags)) - .GetParameters()[0]; + .GetParameters()[p]; var apiParameterDescription = new ApiParameterDescription { }; var filterContext = new ParameterFilterContext(apiParameterDescription, null, null, null, parameterInfo: parameterInfo); Subject().Apply(parameter, filterContext); - Assert.Equal("Description for param1", parameter.Description); - Assert.NotNull(parameter.Example); - - Assert.Equal("\"Example for \\u0022param1\\u0022\"", parameter.Example.ToJson()); + Assert.Equal(expectedDescription, parameter.Description); + Assert.Equal(expectedExample, parameter.Example?.ToJson()); } [Fact]