OData / OData/AspNetCoreOData

@odata.bind not working

Open
#786 12 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Context

  • ASP.NET Core 6
  • Microsoft.AspNetCore.OData: 8.0.12

Description
Given the following datamodel:

public class Item
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public Group Group { get; set; }
}

public class Group
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
}

This controller:

// POST /Items
public async Task<IActionResult> Post([FromBody] Item item)
{
    if (!ModelState.IsValid) return BadRequest(ModelState);
    _context.Items.Add(item);
    await _context.SaveChangesAsync();
    return Created(item);
}

And assuming the following Group entity already exists in the DB:

{
    "Id": 1,
    "Name": "Groceries"
}

What would be the correct payload body to create a new Item entity with the relationship to the already existing category?
In the OData spec I've found mention of the following syntax:

{
    "Name": "Carrot",
    "Group@odata.bind": "Groups(1)"
}

Which should then automatically resolve the id to the actual object from the DB context during model binding of the 'item' input parameter of the POST action. However clearly this doesn't work. So what is the correct way of doing something like this?

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 shown POST /Items action and the Item and Group model definitions, then verify how Microsoft.AspNetCore.OData 8.0.12 handles @odata.bind during model binding. Reproduce the request using the provided Group record and determine the supported payload or binding behavior; done means a new Item is created with its existing Group relationship and valid model state.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.