Getting the @odata.id of a navigation reference to an external entity
@Sreejithpin is already working on this.
Since Dec 21, 2021.
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Description
I have a navigation property for which I need to get the OData ID in a POST request of the containing entity.
The following example is modified one of the samples. My need is a tad different from what is shown in the original samples. Assume that the Department entity below is defined in an external service.
Here's an example ():
public class Organization
{
public int OrganizationId { get; set; }
public string Name { get; set; }
public IList<Department> Departs { get; set; }
}
// Note: Assume that Department is an entity defined in an external service.
public class Department
{
[JsonProperty("@odata.id")]
public Uri ODataId { get; set; }
}
In the EDM model builder:
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Organization>("Organizations");
builder.EntitySet<Department>("Departments");
Here's the metadata for this:
<EntityType Name="Organization">
<Key>
<PropertyRef Name="OrganizationId"/>
</Key>
<Property Name="OrganizationId" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
<NavigationProperty Name="Departs" Type="Collection(ODataRoutingSample.Models.Department)"/>
</EntityType>
<EntityType Name="Department">
<Property Name="ODataId" Type="System.Uri"/>
</EntityType>
When I make the following POST request, I see that the ODataId member for Department is null:
POST http://localhost:5000/v1/organizations HTTP/1.1
Content-Type: application/json
Accept: application/json
{
"Name": "John Doe",
"Departs":[
{"@odata.id": "https://foo.com/v1.0/departments/4"},
{"@odata.id": "https://foo.com/v1.0/departments/5"}
]
}
Here's how the Post method is implemented:
[EnableQuery]
public IActionResult Post([FromBody] Organization org)
{
// Every entry of org.Departs contains a null value for ODataId
return Ok(org);
}
However, when I make the POST request to the $ref endpoint, I am able to get the value of @odata.id in the CreateRef() method's Uri parameter:
POST http://localhost:5000/v1/organizations/1/departs/$ref HTTP/1.1
Content-Type: application/json
Accept: application/json
{
"@odata.id": "https://foo.com/v1.0/departments/4"
}
Here's how the CreateRef is implemented:
[HttpPost]
public IActionResult CreateRef(int key, string navigationProperty, [FromBody] Uri reference)
{
// reference contains the value of @odata.id
return Ok($"CreateRef - {key}: {navigationProperty}. ref={reference}");
}
How do I go about getting the @odata.id in the first POST request for Department?
Note: I tried debugging the ODataResourceDeserializer and I saw that while it does recognize the Id of the resource with the specified URI, it just ignores it at the time any properties are being applied to the CLR object. I also tried making Department an open type (by adding a dynamic properties dictionary but that didn't work either).
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.