From b4ecaaab4fa5b180c27a4a69691e955a65954745 Mon Sep 17 00:00:00 2001 From: Hugo Woodiwiss Date: Sun, 14 Apr 2024 17:37:35 +0100 Subject: [PATCH] Changes per review --- .../SwaggerGenerator/SwaggerGenerator.cs | 24 +++++-------------- .../SwaggerGenerator/SwaggerGeneratorTests.cs | 8 ++++--- ...shbuckle.AspNetCore.SwaggerGen.Test.csproj | 2 +- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index b3929dede0..9dd009b048 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -216,8 +216,10 @@ private OpenApiOperation GenerateOperation(ApiDescription apiDescription, Schema RequestBody = GenerateRequestBody(apiDescription, schemaRepository), Responses = GenerateResponses(apiDescription, schemaRepository), Deprecated = apiDescription.CustomAttributes().OfType().Any(), +#if NET7_0_OR_GREATER Summary = GenerateSummary(apiDescription), Description = GenerateDescription(apiDescription), +#endif }; apiDescription.TryGetMethodInfo(out MethodInfo methodInfo); @@ -656,32 +658,18 @@ private OpenApiMediaType CreateResponseMediaType(ModelMetadata modelMetadata, Sc new KeyValuePair("default", "Error") }; - private string GenerateSummary(ApiDescription apiDescription) - { - string operationSummary = null; #if NET7_0_OR_GREATER - operationSummary = apiDescription - .ActionDescriptor - ?.EndpointMetadata + private string GenerateSummary(ApiDescription apiDescription) => + apiDescription.ActionDescriptor?.EndpointMetadata ?.OfType() .Select(s => s.Summary) .LastOrDefault(); -#endif - return operationSummary; - } - private string GenerateDescription(ApiDescription apiDescription) - { - string operationDescription = null; -#if NET7_0_OR_GREATER - operationDescription = apiDescription - .ActionDescriptor - ?.EndpointMetadata + private string GenerateDescription(ApiDescription apiDescription) => + apiDescription.ActionDescriptor?.EndpointMetadata ?.OfType() .Select(s => s.Description) .LastOrDefault(); #endif - return operationDescription; - } } } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs index 53fa61b598..f384f09502 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs @@ -151,8 +151,8 @@ public void GetSwagger_UseProvidedOpenApiOperation_IfExistsInMetadata() var actionDescriptor = new ActionDescriptor { EndpointMetadata = new List() - { - new OpenApiOperation + { + new OpenApiOperation { OperationId = "OperationIdSetInMetadata", Parameters = new List() @@ -199,7 +199,7 @@ public void GetSwagger_GenerateProducesSchemas_ForProvidedOpenApiOperation() { Content = new Dictionary() { - ["application/someMediaType"] = new() + ["application/someMediaType"] = new() } } } @@ -965,6 +965,7 @@ public void GetSwagger_CanReadTagsFromMetadata() Assert.Equal(new[] { "Some", "Tags", "Here" }, document.Paths["/resource"].Operations[OperationType.Post].Tags.Select(t => t.Name)); } +#if NET7_0_OR_GREATER [Fact] public void GetSwagger_CanReadEndpointSummaryFromMetadata() { @@ -1012,6 +1013,7 @@ public void GetSwagger_CanReadEndpointDescriptionFromMetadata() Assert.Equal("A Test Description", document.Paths["/resource"].Operations[OperationType.Post].Description); } +#endif [Fact] public void GetSwagger_SupportsOption_ConflictingActionsResolver() diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.csproj b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.csproj index c66f3f9983..0cb9c42369 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.csproj +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.csproj @@ -3,7 +3,7 @@ true 1701;1702;1591 - net7.0 + net6.0;net7.0