From 634d2cd65e6b54de76e05579062004c604eddc5d Mon Sep 17 00:00:00 2001 From: Havunen Date: Sun, 11 Feb 2024 14:13:48 +0200 Subject: [PATCH] Porting https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/2414 for https://github.com/Havunen/DotSwashbuckle/issues/1 --- CHANGELOG.md | 6 ++- .../SwaggerGenerator/SwaggerGenerator.cs | 29 ++++++++++- .../SwaggerGenerator/SwaggerGeneratorTests.cs | 48 +++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1452247..5d4ceed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,13 @@ # v3.0.0 -- Add support for using `` on schema XML comments. +- Adds support for using `` on schema XML comments. When using `` on a class, it will be used as the description for the schema. When there is also `` defined, the `` will be used as the title for the schema. When `` is defined alone it is used as description for the schema. -- Add support for defining examples using `SwaggerSchemaAttribute`. +- Adds support for defining examples using `SwaggerSchemaAttribute`. - Adds support for custom Required and Binding attributes - Fixes an issue where UniqueItems is not set true for `IReadOnlySet` +- Adds support for `WithSummary` and `WithDescription` metadata + # v2.0.0 diff --git a/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index 35928a4..8013e36 100644 --- a/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.OpenApi.Models; using DotSwashbuckle.AspNetCore.Swagger; +using Microsoft.AspNetCore.Http.Metadata; namespace DotSwashbuckle.AspNetCore.SwaggerGen { @@ -212,7 +213,9 @@ private OpenApiOperation GenerateOperation(ApiDescription apiDescription, Schema Parameters = GenerateParameters(apiDescription, schemaRepository), RequestBody = GenerateRequestBody(apiDescription, schemaRepository), Responses = GenerateResponses(apiDescription, schemaRepository), - Deprecated = apiDescription.CustomAttributes().OfType().Any() + Deprecated = apiDescription.CustomAttributes().OfType().Any(), + Summary = GenerateSummary(apiDescription), + Description = GenerateDescription(apiDescription), }; apiDescription.TryGetMethodInfo(out MethodInfo methodInfo); @@ -232,6 +235,30 @@ private OpenApiOperation GenerateOperation(ApiDescription apiDescription, Schema } } + private string GenerateSummary(ApiDescription apiDescription) + { + string operationSummary = apiDescription + .ActionDescriptor + ?.EndpointMetadata + ?.OfType() + .Select(s => s.Summary) + .LastOrDefault(); + + return operationSummary; + } + + private string GenerateDescription(ApiDescription apiDescription) + { + string operationDescription = apiDescription + .ActionDescriptor + ?.EndpointMetadata + ?.OfType() + .Select(s => s.Description) + .LastOrDefault(); + + return operationDescription; + } + private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription apiDescription, SchemaRepository schemaRepository) { var metadata = apiDescription.ActionDescriptor?.EndpointMetadata; diff --git a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs index e33f1cc..c127268 100644 --- a/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs +++ b/test/DotSwashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs @@ -581,6 +581,54 @@ public void GetSwagger_SetsParameterTypeToString_IfApiParameterHasNoCorrespondin Assert.Equal("string", operation.Parameters.First().Schema.Type); } + [Fact] + public void GetSwagger_CanReadEndpointSummaryFromMetadata() + { + var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameter)); + var actionDescriptor = new ActionDescriptor + { + EndpointMetadata = new List() { new EndpointSummaryAttribute("A Test Summary") }, + RouteValues = new Dictionary + { + ["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty) + } + }; + var subject = Subject( + apiDescriptions: new[] + { + ApiDescriptionFactory.Create(actionDescriptor, methodInfo, groupName: "v1", httpMethod: "POST", relativePath: "resource"), + } + ); + + var document = subject.GetSwagger("v1"); + + Assert.Equal("A Test Summary", document.Paths["/resource"].Operations[OperationType.Post].Summary); + } + + [Fact] + public void GetSwagger_CanReadEndpointDescriptionFromMetadata() + { + var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameter)); + var actionDescriptor = new ActionDescriptor + { + EndpointMetadata = new List() { new EndpointDescriptionAttribute("A Test Description") }, + RouteValues = new Dictionary + { + ["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty) + } + }; + var subject = Subject( + apiDescriptions: new[] + { + ApiDescriptionFactory.Create(actionDescriptor, methodInfo, groupName: "v1", httpMethod: "POST", relativePath: "resource"), + } + ); + + var document = subject.GetSwagger("v1"); + + Assert.Equal("A Test Description", document.Paths["/resource"].Operations[OperationType.Post].Description); + } + [Fact] public void GetSwagger_GeneratesRequestBody_ForFirstApiParameterThatIsBoundToBody() {