Search code examples
odata

How to POST a derived entity to a base entity set


Suppose I have a base entity type named Employee and a derived entity type named Manager. And the set of all Employees is accessible at the /Employees URL. What is the correct way to POST an instance of Manager to the Employees entity set?

The OData v4 protocol spec says "To create an entity in a collection, the client sends a POST request to that collection's URL." But the spec does not say anything about specifying the type of the POSTed entity in a scenario where derived types exist.

The URI spec says derived types should be addressed using a cast segment on the target URL. For example,

POST /Employees/NS.Manager
{ "Name": "Bill Lumbergh" ... }

Meanwhile, the JSON Format spec suggests using the odata.type annotation as follows:

POST /Employees
{ "@odata.type": "#NS.Manager", "Name": "Bill Lumbergh" ... }

So which is correct? Or are both?

Please keep in mind that I am asking with respect to the OData specifications, not a specific OData library/framework (e.g. ASP.NET).


Solution

  • From an OData Protocol perspective, if you are specifying an instance in a JSON request that is derived from the target type you should include the @odata.type annotation. You do not need to specify the cast segment on the POST (though some implementations may support that, some may not).

    So I would expect your second example to be the most interoperable.

    Mike Pizzo Co-Editor, OASIS OData Specifications