Skip to content

Commit 3a73377

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Add support to automatically explore API version parameters
1 parent 4892d78 commit 3a73377

File tree

48 files changed

+2262
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2262
-269
lines changed

samples/aspnetcore/SwaggerSample/ImplicitApiVersionParameter.cs

-73
This file was deleted.

samples/aspnetcore/SwaggerSample/Startup.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public void ConfigureServices( IServiceCollection services )
6363
options.SwaggerDoc( description.GroupName, CreateInfoForApiVersion( description ) );
6464
}
6565

66-
// add a custom operation filter which documents the implicit API version parameter
67-
options.OperationFilter<ImplicitApiVersionParameter>();
66+
// add a custom operation filter which sets default values
67+
options.OperationFilter<SwaggerDefaultValues>();
6868

6969
// integrate xml comments
7070
options.IncludeXmlComments( XmlCommentsFilePath );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Microsoft.Examples
2+
{
3+
using Swashbuckle.AspNetCore.Swagger;
4+
using Swashbuckle.AspNetCore.SwaggerGen;
5+
using System.Linq;
6+
7+
/// <summary>
8+
/// Represents the Swagger/Swashbuckle operation filter used to document the implicit API version parameter.
9+
/// </summary>
10+
/// <remarks>This <see cref="IOperationFilter"/> is only required due to bugs in the <see cref="SwaggerGenerator"/>.
11+
/// Once they are fixed and published, this class can be removed.</remarks>
12+
public class SwaggerDefaultValues : IOperationFilter
13+
{
14+
/// <summary>
15+
/// Applies the filter to the specified operation using the given context.
16+
/// </summary>
17+
/// <param name="operation">The operation to apply the filter to.</param>
18+
/// <param name="context">The current operation filter context.</param>
19+
public void Apply( Operation operation, OperationFilterContext context )
20+
{
21+
// REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/412
22+
// REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/413
23+
foreach ( var parameter in operation.Parameters.OfType<NonBodyParameter>() )
24+
{
25+
var description = context.ApiDescription.ParameterDescriptions.First( p => p.Name == parameter.Name );
26+
27+
if ( parameter.Description == null )
28+
{
29+
parameter.Description = description.ModelMetadata.Description;
30+
}
31+
32+
if ( parameter.Default == null )
33+
{
34+
parameter.Default = description.RouteInfo.DefaultValue;
35+
}
36+
37+
parameter.Required |= !description.RouteInfo.IsOptional;
38+
}
39+
}
40+
}
41+
}

samples/webapi/SwaggerODataWebApiSample/ImplicitApiVersionParameter.cs

-71
This file was deleted.

samples/webapi/SwaggerODataWebApiSample/Startup.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void Configuration( IAppBuilder builder )
7777
} );
7878

7979
// add a custom operation filter which documents the implicit API version parameter
80-
swagger.OperationFilter<ImplicitApiVersionParameter>();
80+
swagger.OperationFilter<SwaggerDefaultValues>();
8181

8282
// integrate xml comments
8383
swagger.IncludeXmlComments( XmlCommentsFilePath );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Microsoft.Examples
2+
{
3+
using Swashbuckle.Swagger;
4+
using System.Linq;
5+
using System.Web.Http.Description;
6+
7+
/// <summary>
8+
/// Represents the Swagger/Swashbuckle operation filter used to document the implicit API version parameter.
9+
/// </summary>
10+
/// <remarks>This <see cref="IOperationFilter"/> is only required due to bugs in the <see cref="SwaggerGenerator"/>.
11+
/// Once they are fixed and published, this class can be removed.</remarks>
12+
public class SwaggerDefaultValues : IOperationFilter
13+
{
14+
/// <summary>
15+
/// Applies the filter to the specified operation using the given context.
16+
/// </summary>
17+
/// <param name="operation">The operation to apply the filter to.</param>
18+
/// <param name="schemaRegistry">The API schema registry.</param>
19+
/// <param name="apiDescription">The API description being filtered.</param>
20+
public void Apply( Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription )
21+
{
22+
if ( operation.parameters == null )
23+
{
24+
return;
25+
}
26+
27+
foreach ( var parameter in operation.parameters )
28+
{
29+
var description = apiDescription.ParameterDescriptions.First( p => p.Name == parameter.name );
30+
31+
// REF: https://github.com/domaindrivendev/Swashbuckle/issues/1101
32+
if ( parameter.description == null )
33+
{
34+
parameter.description = description.Documentation;
35+
}
36+
37+
// REF: https://github.com/domaindrivendev/Swashbuckle/issues/1089
38+
// REF: https://github.com/domaindrivendev/Swashbuckle/pull/1090
39+
if ( parameter.@default == null )
40+
{
41+
parameter.@default = description.ParameterDescriptor.DefaultValue;
42+
}
43+
}
44+
}
45+
}
46+
}

samples/webapi/SwaggerODataWebApiSample/SwaggerODataWebApiSample.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
</ItemGroup>
128128
<ItemGroup>
129129
<Compile Include="CaseInsensitiveODataUriResolver.cs" />
130-
<Compile Include="ImplicitApiVersionParameter.cs" />
130+
<Compile Include="SwaggerDefaultValues.cs" />
131131
<Compile Include="Configuration\ApiVersions.cs" />
132132
<Compile Include="Configuration\OrderModelConfiguration.cs" />
133133
<Compile Include="Configuration\PersonModelConfiguration.cs" />

samples/webapi/SwaggerWebApiSample/ImplicitApiVersionParameter.cs

-70
This file was deleted.

samples/webapi/SwaggerWebApiSample/Startup.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ public void Configuration( IAppBuilder builder )
6060
.TermsOfService( "Shareware" );
6161
}
6262
} );
63-
64-
65-
// add a custom operation filter which documents the implicit API version parameter
66-
swagger.OperationFilter<ImplicitApiVersionParameter>();
63+
64+
// add a custom operation filter which sets default values
65+
swagger.OperationFilter<SwaggerDefaultValues>();
6766

6867
// integrate xml comments
6968
swagger.IncludeXmlComments( XmlCommentsFilePath );

0 commit comments

Comments
 (0)