Cannot use immutable objects in OData functions (actions)
@xuzhg is already working on this.
Since Oct 11, 2022.
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Description
Assemblies affected
ASP.NET Core OData 8.x
Describe the bug
ODataActionParameters raises an exception when the model was configured with a class with no default constructor (ie immutable class).
Reproduce steps
Configure model like this :
public class MyCompositeKey
{
public MyCompositeKey(string key1, string key2)
{
Key1 = key1;
Key2 = key2;
}
public string Key1 { get; }
public string Key2 { get; }
}
builder.CreateEntity<MyEntity>("MyEntity").Action("MyAction").Parameter<MyCompositeKey>();
In the controller, exception will be raised when action is called :
public class MyEntity : ODataController
{
[HttpPost]
public IActionResult MyAction(ODataActionParameters parameters) => Ok();
}
Expected behavior
As it is supported in ASP.NET Controllers thought JSON serialisation, we could except it to work for OData functions.
It prevents using any immutable class in the OData Model.
Additional context
Microsoft.AspNetCore.OData.Formatter.Deserialization.ODataResourceDeserializer.CreateResourceInstance uses Activator.CreateInstance and thus expects a default constructor.
So maybe the method could call the same code as it is used for JSON deserialisation, which I think, can pickup the right constructor based on parameter names ?
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.