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.
Which assemblies and versions are known to be affected e.g. OData .Net lib 7.x
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";
}
The property 'name' cannot be used in the $filter query option.It does work when I add "name" to the Filter attribute on ModelA, but that doesn't seem like it should be necessary.
Closing as duplicate of https://github.com/OData/WebApi/issues/1696
@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