OData / OData/AspNetCoreOData

Cannot use immutable objects in OData functions (actions)

Open
#716 0 comments 2 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Oct 11, 2022.

bug feature
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.