$metadata specific calls return all of $metadata
Doing a $metadata specific call like odata/$metadata#Books returns all of $metadata.
Assemblies affected
OData 7.3.0. .NET Core Web API 2.2.
Reproduce steps
- Set up a web API and add OData. Like the example BookStore.
- Do a call to
odata/Booksand it will give a list of all Books in the db. At the very top, it will say:"@odata.context": "http://localhost:50000/odata/$metadata#Books",. So here I get this 'specific metadata link'. - Do a call to this link
odata/$metadata#Books.
Expected result
Maybe I am misinterpreting this. The #Books seems a simple anchor point but I'm not sure what its purpose is.
I'd expect to receive only $metadata regarding that specific call, for example only the metadata regarding Books.
<EntityType Name="Book">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="ISBN" Type="Edm.String" />
<Property Name="Title" Type="Edm.String" />
<Property Name="Author" Type="Edm.String" />
<Property Name="Price" Type="Edm.Decimal" Nullable="false" />
<Property Name="Location" Type="BookStore.Models.Address" />
<NavigationProperty Name="Press" Type="BookStore.Models.Press" />
</EntityType>
Actual result
Returns all of $metadata:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0"
xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="BookStore.Models"
xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="Book">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="ISBN" Type="Edm.String" />
<Property Name="Title" Type="Edm.String" />
<Property Name="Author" Type="Edm.String" />
<Property Name="Price" Type="Edm.Decimal" Nullable="false" />
<Property Name="Location" Type="BookStore.Models.Address" />
<NavigationProperty Name="Press" Type="BookStore.Models.Press" />
</EntityType>
<EntityType Name="Press">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="Name" Type="Edm.String" />
<Property Name="Email" Type="Edm.String" />
<Property Name="Category" Type="BookStore.Models.Category" Nullable="false" />
</EntityType>
<ComplexType Name="Address">
<Property Name="City" Type="Edm.String" />
<Property Name="Street" Type="Edm.String" />
</ComplexType>
<EnumType Name="Category">
<Member Name="Book" Value="0" />
<Member Name="Magazine" Value="1" />
<Member Name="EBook" Value="2" />
</EnumType>
</Schema>
<Schema Namespace="System.Linq"
xmlns="http://docs.oasis-open.org/odata/ns/edm">
<ComplexType Name="IQueryable_1OfBook" Abstract="true" />
</Schema>
<Schema Namespace="Default"
xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Function Name="MostExpensive" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(BookStore.Models.Book)" />
<ReturnType Type="Edm.Double" Nullable="false" />
</Function>
<Function Name="Search" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(BookStore.Models.Book)" />
<Parameter Name="searchQuery" Type="Edm.String" />
<ReturnType Type="System.Linq.IQueryable_1OfBook" />
</Function>
<EntityContainer Name="Container">
<EntitySet Name="Books" EntityType="BookStore.Models.Book">
<NavigationPropertyBinding Path="Press" Target="Presses" />
</EntitySet>
<EntitySet Name="Presses" EntityType="BookStore.Models.Press" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
@RobbeSchepens The top odata.context is the context uri, which describes the content of the payload. To make sure the payload is self-contained. It's not a link that you can query.
Beside, for your requirement, it's called "metadata as service". So far, neither OData lib nor web api odata doesn't support it.
For "Metadata as Service", you can refer to: http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part3-csdl/odata-v4.0-errata02-os-part3-csdl-complete.html#_Toc406398100
OData lib has the similar issue opened at: https://github.com/OData/odata.net/issues/181
By the way, OData V4.01 spec prunes the metadata service from the spec at: https://docs.oasis-open.org/odata/new-in-odata/v4.01/cn04/new-in-odata-v4.01-cn04.html#_Toc21700019 . Maybe @mikepizzo has some input for the removal.
The "#books" in the metadataUrl is an HTTP Fragment. In accordance with HTTP rules, fragments are used by the client in order to reference content within a response; they are ignored by the service. So the service is doing the right thing by returning the entire metadata document.
We have discussed a few different designs for requesting a subset of the metadata. One is "Metadata as a Service", as referenced by Sam. It was removed from the CSDL specification because it was orthogonal to how metadata was represented in a CSDL document and greatly increased the size of that document.
One of the challenges of Metadata as a Service was that it didn't return the metadata as CSDL, so it couldn't be used to retrieve the subset of metadata required to parse/understand the result.
We have also discussed passing in something in the preference header to specify that the client is only interested in a subset of the returned metadata. Services could ignore this and continue to return the entire metadata, or they could prune it to only the metadata that was interesting to the client. One question we debated was what to use for the value of this preference header to specify the interesting subset, and a leading candidate was to pass in the fragment from the contextUrl.
One of the reasons that we have not yet added this mechanism to the standard protocol was that we wanted to better understand the scenarios in which the client wanted to get a subset of the metadata. If you can describe your scenario, why you wanted a subset of the metadata and (importantly) how you were planning to use that metadata, that would help in evaluating different alternatives.
@mikepizzo The question came from our front-end team. They were hoping to get all the information specific to a type so that they can see all properties of it. They need it to compare our back-end Web API C# models to their ReactJS (TypeScript) models. This is necessary because we are in active development and changes happen frequently.
Getting the $metadata doesn't require authorization in our API so this makes it easier to implement a test that is executed in the build pipeline (Azure DevOps - CICD).
Possibly, when development is done, this feature would no longer be needed. So it would mainly be a development aid/tool.
@RobbeSchepens Thank you for raising this issue. Closing it as the current behaviour is according to the protocol. Feel free to open a separate to track a feature request for retrieving a subset of the metadata and link it to this issue.