Webapi: Cannot filter on child model property without enabling property in parent [Filter]

Created on 25 Aug 2020  路  6Comments  路  Source: OData/WebApi

I'm trying to filter on a child collection of my OData model by adding [Filter] attributes. I have added the [Filter] attribute to my top-level model, and that works. However I am getting an error message saying that the property on my child model cannot be used as a filter unless I add it to the attribute on my main model.

Assemblies affected

Which assemblies and versions are known to be affected e.g. OData .Net lib 7.x

  • System.Web.OData v6.0.0.0 _(I think)_

Reproduce steps

Filter string: children/any(x: x/name eq 'bar')

Controller and models:

[ODataRoutePrefix("repro")]
public class ReproController : ODataController
{
    public static List<ModelA> Data { get; set; } = new List<ModelA>
    {
        new ModelA(),
    };

    [HttpGet]
    [ODataRoute]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Filter)]
    public Task Repro(ODataQueryOptions<ModelA> options)
    {
        options.Validate(new ODataValidationSettings
        {
            AllowedQueryOptions = AllowedQueryOptions.Filter,
        });

        return Task.CompletedTask;
    }
}

[Filter("children")]
public class ModelA
{
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    public IEnumerable<ModelB> Children { get; set; } = new List<ModelB>
    {
        new ModelB(),
    };
}

[Filter("name")]
public class ModelB
{
    public string Name { get; set; } = "foo";
}

Expected result

  • Filter validation succeeds.

Actual result

  • Validation fails: The property 'name' cannot be used in the $filter query option.

Additional detail

It does work when I add "name" to the Filter attribute on ModelA, but that doesn't seem like it should be necessary.

bug

All 6 comments

@robinkanters
Can you try to add the [Filter] on the property?

[Filter("Name")]
public IEnumerable<ModelB> Children...

Hi @xuzhg, unfortunately that doesnt help, already tried adding that attribute all over the place

@mikepizzo appreciate the reopen but Im pretty sure the issue is in the FilterClauseValidator, right? Thats in that other repo iirc

@robinkanters - right you are. transferring the issue. I note that there is already an issue in that repo, so we may eventual (re-)close this as a dupe, but wanted to keep this one open for now for the active discussion.

Related issue #1696 has a detailed investigation of this issue. Perhaps we can get it fixed for 7.5

Was this page helpful?
0 / 5 - 0 ratings