Skip to content

Support Ad Hoc Model Bound Settings #933

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

Merged
merged 8 commits into from
Dec 7, 2022

Conversation

commonsensesoftware
Copy link
Collaborator

Support Ad Hoc Model Bound Settings

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

Adds support for OData Model Bound settings using an ad hoc EDM. Developers have the following options:

  1. Apply any Model Bound settings via attributes (ex: [Filter("title")])
    a. This is achieved via the ImplicitModelBoundSettingsConvention
    b. In ASP.NET Core, this is wire up automatically via DI
    c. In ASP.NET Web API, it must be added explicitly
  2. ODataExplorerOptions now has a public VersionedODataModelBuilder AdHocModelBuilder { get; } property that can be used to:
    a. Configure ad hoc settings
    b. Add conventions specific to ad hoc models

Each convention must be IModelConfiguration. If an implementation needs/wants to participate in the discovery process, it must also implement IODataQueryOptionsConvention, which will be visited for each ApiDescription.

Examples

Assume the following model:

[Filter("title", "published")]
public class Book
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public int Published { get; set; }
}

The following configurations will explore the $filter query option and indicated that only title and published are allowed.

ASP.NET Core

The basic scenario works without any additional configuration:

builder.Services.AddApiVersioning().AddODataApiExplorer();

The basic scenario works without any additional configuration:

builder.Services
       .AddApiVersioning()
       .AddODataApiExplorer(
        options =>
        {
            // equivalent to the default, but demonstrates a non-DI method to add conventions
            options.AdHocModelBuilder.ModelConfigurations.Clear();
            options.AdHocModelBuilder.ModelConfigurations.Add( new ImplicitModelBoundSettingsConvention() );
        } );

ASP.NET Web API

configuration.EnableDependencyInjection();
configuration.AddApiVersioning();
configuration.MapHttpAttributeRoutes();
configuration.AddODataApiExplorer(
       options => options.AdHocModelBuilder.ModelConfigurations.Add( new ImplicitModelBoundSettingsConvention() ) );

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

Successfully merging this pull request may close these issues.

1 participant