Exception when using filter associated with a Dictionary<string,object>
@xuzhg is already working on this.
Since Apr 20, 2023.
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Description
Assemblies affected
Which assemblies and versions are known to be affected e.g. ASP.NET Core OData 8.x
Microsoft.AspNetCore.OData (8.0.12)
Describe the bug
In a model class we use a Dictionary<string,object> type property to benefit from dynamic properties. Unfortunately, when we apply an odata filter (e.g. contains(DynamicProperty1,"Test")) that relates to the dictionary on this class, an exception is thrown :
ArgumentException: Method 'System.Object get_Item(System.String)' declared on type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' cannot be called with instance of type 'System.Object'
System.Linq.Expressions.Expression.ValidateCallInstanceType(Type instanceType, MethodInfo method).
This exception is called because the library generates an Expression Where SQL containing the fields of the dynamic property whereas they do not exist in base. Is this a normal operation of odata filters with dynamic properties or have we not set up dynamic properties correctly?
Reproduce steps
- Add a Dictionary<string,object> property in an Entity model class.
- It's not mandatory but fill the dictionary with AutoMapper (or anything else) for example {"DynProp1","Hello"}
- call the controller with a filter using a dynamic property for example :
https://localhost:5000/v1/Customers?$filter=contains(DynProp1,'Hell') - ArgumentException!
[HttpGet]
[EnableQuery]
public IActionResult Get()
{
// SQL Query
var query = context.Customers.Where(c => c.Age > 21)
.OrderBy(l => l.OrderNumber)
// AutoMapper populate the Dictionary<string,object> with {"DynProp1","Hello"}
.ProjectTo<CustomerModel>(_mapper.ConfigurationProvider);
return Ok(query);
// Exception will be thrown during the execution of this query
}
Expected behavior
No more exception or a better documentation to explain how to setup dynamics properties.
Additional context
Thank you very much for this great library.:)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.