From 7ab6515f7710c533eb75c0ed71eadc82f1f9e7c8 Mon Sep 17 00:00:00 2001 From: Rami Abughazaleh Date: Sun, 17 Jan 2021 22:10:05 -0800 Subject: [PATCH] Added Swagger support for OData void response type and default parameter values other than strings Fixes #716 and #717 --- .../SwaggerDefaultValues.cs | 22 +++++++++++++++++-- .../SwaggerSample/SwaggerDefaultValues.cs | 22 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/samples/aspnetcore/SwaggerODataSample/SwaggerDefaultValues.cs b/samples/aspnetcore/SwaggerODataSample/SwaggerDefaultValues.cs index 735d4f47..b9aec0b8 100644 --- a/samples/aspnetcore/SwaggerODataSample/SwaggerDefaultValues.cs +++ b/samples/aspnetcore/SwaggerODataSample/SwaggerDefaultValues.cs @@ -1,7 +1,6 @@ namespace Microsoft.Examples { using Microsoft.AspNetCore.Mvc.ApiExplorer; - using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using System.Linq; @@ -24,6 +23,24 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context ) operation.Deprecated |= apiDescription.IsDeprecated(); + // https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1752#issue-663991077 + foreach ( var responseType in context.ApiDescription.SupportedResponseTypes ) + { + // based on internals of SwaggerGenerator + // https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/b7cf75e7905050305b115dd96640ddd6e74c7ac9/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs#L383-L387 + var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString(); + var response = operation.Responses[responseKey]; + + // remove media types not supported by the API + foreach ( var contentType in response.Content.Keys ) + { + if ( !responseType.ApiResponseFormats.Any( x => x.MediaType == contentType ) ) + { + response.Content.Remove( contentType ); + } + } + } + if ( operation.Parameters == null ) { return; @@ -42,7 +59,8 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context ) if ( parameter.Schema.Default == null && description.DefaultValue != null ) { - parameter.Schema.Default = new OpenApiString( description.DefaultValue.ToString() ); + // https://github.com/Microsoft/aspnet-api-versioning/issues/429#issuecomment-605402330 + parameter.Schema.Default = OpenApiAnyFactory.CreateFor( parameter.Schema, description.DefaultValue ); } parameter.Required |= description.IsRequired; diff --git a/samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs b/samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs index 735d4f47..b9aec0b8 100644 --- a/samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs +++ b/samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs @@ -1,7 +1,6 @@ namespace Microsoft.Examples { using Microsoft.AspNetCore.Mvc.ApiExplorer; - using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using System.Linq; @@ -24,6 +23,24 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context ) operation.Deprecated |= apiDescription.IsDeprecated(); + // https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1752#issue-663991077 + foreach ( var responseType in context.ApiDescription.SupportedResponseTypes ) + { + // based on internals of SwaggerGenerator + // https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/b7cf75e7905050305b115dd96640ddd6e74c7ac9/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs#L383-L387 + var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString(); + var response = operation.Responses[responseKey]; + + // remove media types not supported by the API + foreach ( var contentType in response.Content.Keys ) + { + if ( !responseType.ApiResponseFormats.Any( x => x.MediaType == contentType ) ) + { + response.Content.Remove( contentType ); + } + } + } + if ( operation.Parameters == null ) { return; @@ -42,7 +59,8 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context ) if ( parameter.Schema.Default == null && description.DefaultValue != null ) { - parameter.Schema.Default = new OpenApiString( description.DefaultValue.ToString() ); + // https://github.com/Microsoft/aspnet-api-versioning/issues/429#issuecomment-605402330 + parameter.Schema.Default = OpenApiAnyFactory.CreateFor( parameter.Schema, description.DefaultValue ); } parameter.Required |= description.IsRequired;