Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger show wrong OData QueryOptions #1110

Open
1 task done
fissssssh opened this issue Sep 23, 2024 · 2 comments
Open
1 task done

Swagger show wrong OData QueryOptions #1110

fissssssh opened this issue Sep 23, 2024 · 2 comments

Comments

@fissssssh
Copy link

fissssssh commented Sep 23, 2024

Is there an existing issue for this?

  • 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

@commonsensesoftware
Copy link
Collaborator

This is almost certainly an external issue with OData itself. Let's start by reviewing #944, which also has a deep link over to the OData repo where I outline the issue further.

Ultimately, the API Explorer extensions only document OData query options. It doesn't change anything about how they work. The Litmus test is trying to get it working without API Versioning. I suspect in this case, it will not work. I've tried to provide comprehensive examples to show working OData combinations, which aren't always straight forward.

If you find that $top does work as expected, but then fails when you add API Versioning and/or it's API Explorer extensions, then it's worth taking a deeper look. I have a strong feeling, this is a simple OData misconfiguration.

As I recall, you must do one of the following for [EnableQuery] to take effect:

  • Enable global query options
  • Use Model Bound query settings via:
    • Attributes declared on your model (ex: [Select], [Page], etc)
    • Conventions defined via the ODataModelBuilder (ex: .Select(...), .Page(...), etc)

@fissssssh
Copy link
Author

This is almost certainly an external issue with OData itself. Let's start by reviewing #944, which also has a deep link over to the OData repo where I outline the issue further.

Ultimately, the API Explorer extensions only document OData query options. It doesn't change anything about how they work. The Litmus test is trying to get it working without API Versioning. I suspect in this case, it will not work. I've tried to provide comprehensive examples to show working OData combinations, which aren't always straight forward.

If you find that $top does work as expected, but then fails when you add API Versioning and/or it's API Explorer extensions, then it's worth taking a deeper look. I have a strong feeling, this is a simple OData misconfiguration.

As I recall, you must do one of the following for [EnableQuery] to take effect:

  • Enable global query options

  • Use Model Bound query settings via:

    • Attributes declared on your model (ex: [Select], [Page], etc)
    • Conventions defined via the ODataModelBuilder (ex: .Select(...), .Page(...), etc)

I update OData configuration and $top and $skip working, but still not appear in Api Explorer

builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
}).AddOData(opts =>
{
+   options.Count().Select().Expand().OrderBy().SetMaxTop(null);
    opts.EnableNoDollarQueryOptions = false;
});

I created a minimalistic repo project to reproduce this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants