I can't find a way to flag a parameter on my method to exclude the CancellationToken parameter from generating in the documentation.
I propose a convention of ignoring this type of parameter during generation.
I agree that CancellationToken parameters should be ignored by default. In the meantime, here's a simple IOperationFilter that does just that:
public class SwaggerRemoveCancellationTokenParameterFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
apiDescription.ParameterDescriptions
.Where(pd => pd.ParameterDescriptor.ParameterType == typeof(CancellationToken))
.ToList()
.ForEach(pd =>
{
if (operation.parameters != null)
{
var cancellationTokenParameter = operation.parameters.Single(p => p.name == pd.Name);
operation.parameters.Remove(cancellationTokenParameter);
}
});
}
}
Finally found out why my controller wasn't working and this was it - just spent all weekend trying to get this going.
If you are using swashbuckle v6 and ASP.NET 5 then the code you need differs slightly from the above sample.
The code below was what I had to use to get things going...
public class SwaggerRemoveCancellationTokenParameterFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
context.ApiDescription.ParameterDescriptions
.Where(pd =>
pd.ModelMetadata.ContainerType == typeof(CancellationToken) ||
pd.ModelMetadata.ContainerType == typeof(WaitHandle) ||
pd.ModelMetadata.ContainerType == typeof(SafeWaitHandle))
.ToList()
.ForEach(
pd =>
{
if (operation.Parameters != null)
{
var cancellationTokenParameter = operation.Parameters.Single(p => p.Name == pd.Name);
operation.Parameters.Remove(cancellationTokenParameter);
}
});
}
}
Definitely needs a fix for next version. @dementeddevil filter works perfectly though, but it took me hours to get it working
Even with removing the parameters, the models will end up in the schema. By generating a client via swagger-codegen you will end up with class definitions for CancellationToken , WaitHandle, SafeWaitHandle etc. Has anybody found out so far how to remove those schema entries again? ISchemaFilter only allows modifying of schemas, not excluding them.
These implementation is not working you should use the document filter interface and remove those definitions from the schema registry
Fixed by 7ab9b842c5ad068dd7115a102bed45559133e0a6. Will automatically ignore parameters of type CancellationToken without the need for custom filters.
Most helpful comment
Finally found out why my controller wasn't working and this was it - just spent all weekend trying to get this going.
If you are using swashbuckle v6 and ASP.NET 5 then the code you need differs slightly from the above sample.
The code below was what I had to use to get things going...