AspNetCoreOData
AspNetCoreOData copied to clipboard
Cannot use immutable objects in OData functions (actions)
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 ?