Swashbuckle.aspnetcore: CancellationToken is being included as query parameters - v4.0.0

Created on 6 Nov 2018  路  9Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Version: v4.0.0

Sample method:

public async Task<IActionResult> SampleMethod(
    [FromBody] SampleRequest request, 
    CancellationToken cancellationToken);

Generated parameters:

        "parameters": [
          {
            "name": "request",
            "in": "body",
            "description": "SampleRequest ",
            "required": false,
            "schema": {
              "$ref": "#/definitions/SampleRequest"
            }
          },
          {
            "name": "IsCancellationRequested",
            "in": "query",
            "required": false,
            "type": "boolean"
          },
          {
            "name": "CanBeCanceled",
            "in": "query",
            "required": false,
            "type": "boolean"
          },
          {
            "name": "WaitHandle.Handle",
            "in": "query",
            "required": false,
            "type": "object"
          },
          {
            "name": "WaitHandle.SafeWaitHandle.IsInvalid",
            "in": "query",
            "required": false,
            "type": "boolean"
          },
          {
            "name": "WaitHandle.SafeWaitHandle.IsClosed",
            "in": "query",
            "required": false,
            "type": "boolean"
          }
        ]

Most helpful comment

I am also having this issue in Swashbuckle.AspNetCore 4.0.1

    public Task<IActionResult> Post([FromBody] Model model, CancellationToken cancellationToken)
    {
        return this.Command.Value.ExecuteAsync(model, cancellationToken);
    }

image

I am using Microsoft.AspNetCore.App version 2.1.5 with a targetframework of netcoreapp2.1

I was able to get rid of the parameters but adding an Operation filter based on a previous post i found in issue 627: https://github.com/domaindrivendev/Swashbuckle/issues/627

    public class SwaggerRemoveCancellationTokenParameterFilter : Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter
    {
        public void Apply(Operation operation, Swashbuckle.AspNetCore.SwaggerGen.OperationFilterContext context)
        {
            context.ApiDescription.ParameterDescriptions
                .Where(pd =>
                    pd.ModelMetadata.ContainerType == typeof(System.Threading.CancellationToken) ||
                    pd.ModelMetadata.ContainerType == typeof(System.Threading.WaitHandle) ||
                    pd.ModelMetadata.ContainerType == typeof(Microsoft.Win32.SafeHandles.SafeWaitHandle))
                .ToList()
                .ForEach(
                    pd =>
                    {
                        if (operation.Parameters != null)
                        {
                            var cancellationTokenParameter = operation.Parameters.FirstOrDefault(p => p.Name.Equals(pd.Name, System.StringComparison.OrdinalIgnoreCase));
                            if(cancellationTokenParameter != null)
                                operation.Parameters.Remove(cancellationTokenParameter);
                        }
                    });
        }
    }

Once I added the operation filter the CancelationToken properties were removed

All 9 comments

Which version of ASP.NET Core are you using?

These are some of the references:

    <PackageReference Include="Microsoft.AspNetCore" Version="2.1.4" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="2.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.1.1" />

The target framework is net47

I can't repro this - could you put together a minimal set of repro steps starting from an empty project?

Unless I'm mistake, ASP.NET Core 2.1 and above is NOT compatible with net47 so not sure how you can be targeting net47 with the references you've listed above?

I am also having this issue in Swashbuckle.AspNetCore 4.0.1

    public Task<IActionResult> Post([FromBody] Model model, CancellationToken cancellationToken)
    {
        return this.Command.Value.ExecuteAsync(model, cancellationToken);
    }

image

I am using Microsoft.AspNetCore.App version 2.1.5 with a targetframework of netcoreapp2.1

I was able to get rid of the parameters but adding an Operation filter based on a previous post i found in issue 627: https://github.com/domaindrivendev/Swashbuckle/issues/627

    public class SwaggerRemoveCancellationTokenParameterFilter : Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter
    {
        public void Apply(Operation operation, Swashbuckle.AspNetCore.SwaggerGen.OperationFilterContext context)
        {
            context.ApiDescription.ParameterDescriptions
                .Where(pd =>
                    pd.ModelMetadata.ContainerType == typeof(System.Threading.CancellationToken) ||
                    pd.ModelMetadata.ContainerType == typeof(System.Threading.WaitHandle) ||
                    pd.ModelMetadata.ContainerType == typeof(Microsoft.Win32.SafeHandles.SafeWaitHandle))
                .ToList()
                .ForEach(
                    pd =>
                    {
                        if (operation.Parameters != null)
                        {
                            var cancellationTokenParameter = operation.Parameters.FirstOrDefault(p => p.Name.Equals(pd.Name, System.StringComparison.OrdinalIgnoreCase));
                            if(cancellationTokenParameter != null)
                                operation.Parameters.Remove(cancellationTokenParameter);
                        }
                    });
        }
    }

Once I added the operation filter the CancelationToken properties were removed

As an alternative youcan use HttpContext.RequestAborted in the controller actions instead of the CancellationToken parameter. This has the same effect because HttpContext.RequestAborted is bound to the parameter by the model binder.

Same issue here. I'm on ASP.NET Core 2.1. Let me know if I can help you repro.

Note that this wasn't an issue on V3.0.0.

This is solved if you if you set the MVC compatibility version to 2.1:

.AddMvcCore()
.SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NinoFloris picture NinoFloris  路  3Comments

govin picture govin  路  3Comments

TimmyGilissen picture TimmyGilissen  路  3Comments

alasvant picture alasvant  路  3Comments

rgelb picture rgelb  路  3Comments