Skip to content

Commit 9fc324e

Browse files
icnocopcommonsensesoftware
authored andcommitted
Added Swagger support for OData void response type and default parameter values other than strings
Fixes #716 and #717
1 parent 8954793 commit 9fc324e

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

samples/aspnetcore/SwaggerODataSample/SwaggerDefaultValues.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Microsoft.Examples
22
{
33
using Microsoft.AspNetCore.Mvc.ApiExplorer;
4-
using Microsoft.OpenApi.Any;
54
using Microsoft.OpenApi.Models;
65
using Swashbuckle.AspNetCore.SwaggerGen;
76
using System.Linq;
@@ -24,6 +23,24 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context )
2423

2524
operation.Deprecated |= apiDescription.IsDeprecated();
2625

26+
// https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1752#issue-663991077
27+
foreach ( var responseType in context.ApiDescription.SupportedResponseTypes )
28+
{
29+
// based on internals of SwaggerGenerator
30+
// https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/b7cf75e7905050305b115dd96640ddd6e74c7ac9/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs#L383-L387
31+
var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString();
32+
var response = operation.Responses[responseKey];
33+
34+
// remove media types not supported by the API
35+
foreach ( var contentType in response.Content.Keys )
36+
{
37+
if ( !responseType.ApiResponseFormats.Any( x => x.MediaType == contentType ) )
38+
{
39+
response.Content.Remove( contentType );
40+
}
41+
}
42+
}
43+
2744
if ( operation.Parameters == null )
2845
{
2946
return;
@@ -42,7 +59,8 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context )
4259

4360
if ( parameter.Schema.Default == null && description.DefaultValue != null )
4461
{
45-
parameter.Schema.Default = new OpenApiString( description.DefaultValue.ToString() );
62+
// https://github.com/Microsoft/aspnet-api-versioning/issues/429#issuecomment-605402330
63+
parameter.Schema.Default = OpenApiAnyFactory.CreateFor( parameter.Schema, description.DefaultValue );
4664
}
4765

4866
parameter.Required |= description.IsRequired;

samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Microsoft.Examples
22
{
33
using Microsoft.AspNetCore.Mvc.ApiExplorer;
4-
using Microsoft.OpenApi.Any;
54
using Microsoft.OpenApi.Models;
65
using Swashbuckle.AspNetCore.SwaggerGen;
76
using System.Linq;
@@ -24,6 +23,24 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context )
2423

2524
operation.Deprecated |= apiDescription.IsDeprecated();
2625

26+
// https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1752#issue-663991077
27+
foreach ( var responseType in context.ApiDescription.SupportedResponseTypes )
28+
{
29+
// based on internals of SwaggerGenerator
30+
// https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/b7cf75e7905050305b115dd96640ddd6e74c7ac9/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs#L383-L387
31+
var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString();
32+
var response = operation.Responses[responseKey];
33+
34+
// remove media types not supported by the API
35+
foreach ( var contentType in response.Content.Keys )
36+
{
37+
if ( !responseType.ApiResponseFormats.Any( x => x.MediaType == contentType ) )
38+
{
39+
response.Content.Remove( contentType );
40+
}
41+
}
42+
}
43+
2744
if ( operation.Parameters == null )
2845
{
2946
return;
@@ -42,7 +59,8 @@ public void Apply( OpenApiOperation operation, OperationFilterContext context )
4259

4360
if ( parameter.Schema.Default == null && description.DefaultValue != null )
4461
{
45-
parameter.Schema.Default = new OpenApiString( description.DefaultValue.ToString() );
62+
// https://github.com/Microsoft/aspnet-api-versioning/issues/429#issuecomment-605402330
63+
parameter.Schema.Default = OpenApiAnyFactory.CreateFor( parameter.Schema, description.DefaultValue );
4664
}
4765

4866
parameter.Required |= description.IsRequired;

0 commit comments

Comments
 (0)