-
Notifications
You must be signed in to change notification settings - Fork 704
Swashbuckle Integration
Chris Martinez edited this page Jun 12, 2017
·
14 revisions
Although the API explorers for API versioning provide all of the necessary information, there is select information that Swagger and Swashbuckle will not wire up for you. Fortunately, bridging this gap is really easy to achieve using Swashbuckle's extensibility model. The following are simple IOperationFilter implementations that leverage the metadata provided by the corresponding API explorer to fill in these gaps. You can then add this filter to your configuration, according to the platform you're using, with:
swagger.OperationFilter<SwaggerDefaultValues>();
public class SwaggerDefaultValues : IOperationFilter
{
public void Apply(
Operation operation,
SchemaRegistry schemaRegistry,
ApiDescription apiDescription )
{
if ( operation.parameters == null )
{
return;
}
foreach ( var parameter in operation.parameters )
{
var description = apiDescription.ParameterDescriptions
.First( p => p.Name == parameter.name );
if ( parameter.description == null )
{
parameter.description = description.Documentation;
}
if ( parameter.@default == null )
{
parameter.@default = description.ParameterDescriptor.DefaultValue;
}
}
}
}
public class SwaggerDefaultValues : IOperationFilter
{
public void Apply( Operation operation, OperationFilterContext context )
{
foreach ( var parameter in operation.Parameters.OfType<NonBodyParameter>() )
{
var description = context.ApiDescription
.ParameterDescriptions
.First( p => p.Name == parameter.Name );
if ( parameter.Description == null )
{
parameter.Description = description.ModelMetadata.Description;
}
if ( parameter.Default == null )
{
parameter.Default = description.RouteInfo.DefaultValue;
}
parameter.Required |= !description.RouteInfo.IsOptional;
}
}
}
There are end-to-end examples using API versioning and Swashbuckle:
- Home
- Quick Starts
- Version Format
- Version Discovery
- Version Policies
- How to Version Your Service
- API Versioning with OData
- Configuring Your Application
- Error Responses
- API Documentation
- Extensions and Customizations
- Known Limitations
- FAQ
- Examples