Skip to content

ODataQueryOptions parameter on method generates over 1600 parameters in Swagger UI #599

Closed
@DoctorGamester

Description

@DoctorGamester

If I have a method like this:

        /// <summary>
        /// get/search people
        /// </summary>
        /// <response code="200">Successful get</response>
        /// <response code="401">API key is missing or invalid</response>
        /// <response code="403">You are logged in but do not have the authorization to view this resource.</response>
        /// <response code="500">The request was formed correctly. An internal error is stopping the request from going through.</response>
        /// <response code="0">Error</response>
        [HttpGet]
        [ODataRoute]
        [Authorize(AuthenticationSchemes = BasicAuthenticationHandler.SchemeName)]
        [Produces("application/json")]
        [ProducesResponseType(typeof(ODataValue<IEnumerable<Person>>), 200)]
        [SwaggerResponse(statusCode: 401, type: typeof(Error), description: "API key is missing or invalid")]
        [SwaggerResponse(statusCode: 403, type: typeof(Error), description: "You are logged in but do not have the authroization to view this resource.")]
        [SwaggerResponse(statusCode: 0, type: typeof(OdataError[]), description: "Error")]
        [EnableQuery]
        public IActionResult Get()
        {
            //snip
        }

The moment I change it to have ODataQueryOptions, like this:

public IActionResult Get(ODataQueryOptions<Person> options)

Suddenly over 1600 parameters are generated by the Swagger UI, and a RequestBody that is normally null becomes full of information as well. It also takes an extremely long time to show these parameters when I click on the method in the UI.

I currently get around this by using the following code for a DocumentFilter:

        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var paths = swaggerDoc.Paths.ToList();
            swaggerDoc.Paths.Clear();
            foreach(var item in paths)
            {
                foreach(var operation in item.Value.Operations)
                {
                    var parameters = operation.Value.Parameters
                        .Where(p => 
                            !p.Name.StartsWith("IfMatch") &&
                            !p.Name.StartsWith("IfNoneMatch") &&
                            !p.Name.StartsWith("Request") &&
                            !p.Name.StartsWith("Context") &&
                            !p.Name.StartsWith("RawValue") &&
                            !p.Name.StartsWith("Select") &&
                            !p.Name.StartsWith("Apply") &&
                            !p.Name.StartsWith("Filter") &&
                            !p.Name.StartsWith("OrderBy") &&
                            !p.Name.StartsWith("Skip") &&
                            !p.Name.StartsWith("Top") &&
                            !p.Name.StartsWith("Count") &&
                            !p.Name.StartsWith("Validator")
                            ).ToList();
                    operation.Value.Parameters.Clear();
                    foreach (var pr in parameters)
                        operation.Value.Parameters.Add(pr);
                    operation.Value.RequestBody = null;
                }
                swaggerDoc.Paths.Add(item.Key, item.Value);
            }

I am using the following packages, and targeting .NET Core 3.1:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.OData" Version="7.3.0" />
    <PackageReference Include="Microsoft.AspNetCore.OData.Versioning" Version="4.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.OData.Versioning.ApiExplorer" Version="4.1.1" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="1.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0" />
  </ItemGroup>

Am I doing something wrong? Have I run into a support issue, or more an issue where I am not quite using the library combination properly?

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions