aspnet-api-versioning icon indicating copy to clipboard operation
aspnet-api-versioning copied to clipboard

Swagger show wrong OData QueryOptions

Open fissssssh opened this issue 1 year ago • 2 comments
trafficstars

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

I used Asp.Versioning.OData.ApiExplorer 8.1.0 AddODataApiExplorer with the following configuration:

  • Program.cs

    builder.Services.AddControllers().AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
        options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
    }).AddOData(opts =>
    {
        opts.EnableNoDollarQueryOptions = false;
    });
    
    builder.Services.AddApiVersioning().AddOData(opts =>
    {
        opts.ModelBuilder.DefaultModelConfiguration = (builder, apiVersion, routePrefix) =>
        {
            builder.EntitySet<Experiment>("Experiments");
        };
        opts.AddRouteComponents("api/v{version:apiVersion}");
    }).AddODataApiExplorer(opts =>
    {
        opts.GroupNameFormat = "'v'VVV";
        opts.FormatGroupName = (group, version) => $"{group} - {version}";
        opts.SubstituteApiVersionInUrl = true;
    });
    
  • ExperimentsController

    
    [ApiVersion(1.0)]
    public class ExperimentsController : ODataController
    {
        // dependencies...
    
        [EnableQuery(MaxTop = 1000, AllowedQueryOptions = Select | Skip | Top | Count)]
        public IActionResult Get()
        {
            return Ok(_db.Experiments);
        }
    }
    

I did not configure QueryOption globally, nor did I use a specific convention model configuration. I only used EnableQuery, but the $skip and $top parameters are not displayed in Swagger, and $count cannot be used.

I tried add $skip=1&$top=1 to query string by curl in terminal, and response say The limit of '0' for Top query has been exceeded

Expected Behavior

I hope to be able to use $count as defined in EnableQuery and have the $select, $top, and $skip parameters correctly worked.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.401

Anything else?

No response

fissssssh avatar Sep 23 '24 15:09 fissssssh