OData / OData/AspNetCoreOData

Dictionary parameter in ODataActionParameters

Open
#210 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug followup
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

ODataActionParameters does not seem to support dictionaries in a complex parameter. Here's a minimal example to reproduce it:

public class Entity
{
    public string Id { get; set; }
}

public class ComplexParam
{
    public string StringValue { get; set; }

    public Dictionary<string, string> EnvVars { get; set; }
}

private IEdmModel GetModel()
{
    var builder = new ODataConventionModelBuilder();
    var entity = builder.EntitySet<Entity>("Entities");
    entity.EntityType.HasKey(x => x.Id);

    var action = entity.EntityType.Collection.Action("TestAction");
    action.Parameter<ComplexParam>("ComplexParam").Required();

    return builder.GetEdmModel();
}

The controller is as following:

[ODataRoutePrefix("Entities")]
public class EntityController : ODataController
{
    [ODataRoute("TestAction")]
    [HttpPost]
    public void TestAction(ODataActionParameters parameters)
    {
        var obj = parameters?["ComplexParam"];
        if(obj is ComplexParam cp)
        {
            // do something
        }
    }

The request I send:

{
    "ComplexParam":  {
        "StringValue": "1",
        "EnvVars": {
            "a": "b",
            "c": "d"
        }
    }
}

parameters is always null in the controller. If I remove the dictionary property and the EnvVars json field from the request, it works as expected. I tried with both Dictionary<string, string> and Dictionary<string, object> and neither works, is there any workaround?

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.

Research direction

Start with the ODataConventionModelBuilder setup and EntityController.TestAction shown in the issue, then reproduce the request with the dictionary property present and absent. Trace how the ODataActionParameters value is bound for the complex parameter; done means the request populates parameters and preserves the EnvVars dictionary, with the workaround or limitation documented if support is not possible.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.