James Baldwinson
James Baldwinson

Reputation: 11

WebApi Odata returning complex type

I have a simple data model consisting of two entities

public class product 
{
     public int ID {get;set;}
     public string Name {get;set;}
}

public class supplier
{
    public int ID {get;set;}
    public string Name {get;set;}
    public IEnumerable<product> products {get;set;}
}

Now from my WebApi odata controller I want to return the supplier with all their products. But I cannot seem to get this working, with it just returning the suppler and effectively stripping the product information. The controller method is a simple Get and GetEntityByKey. My configuration is as follows.

 ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.EntitySet<supplier>("supplier");
            modelBuilder.EntitySet<product>("product");

Is there a configuration options I'm missing to get this to work?

Upvotes: 1

Views: 4083

Answers (3)

Andrey Tagaew
Andrey Tagaew

Reputation: 1603

Try to use $expand clause In my situation i had to add a complexType first:

builder.ComplexType();

Additionally I found that different query should be used to get the complex type on UI

Instead of calling http://url/api/AccountDetails?$select=name,accountNumber,balance another url should be used: http://url/api/AccountDetails?$select=name,accountNumber,balance&$expand=balance

you can only see complex properties like balance via $expand

Also, important to have $expand feature turned on. To do that add it before you add the edm model: endpoints.Select().Expand().Filter().OrderBy().Count().MaxTop(10); endpoints.MapODataRoute("odata", "odata", this.GetEdmModel());

See details here: https://stackoverflow.com/a/55476645/2050539

Upvotes: 0

Roman Bats
Roman Bats

Reputation: 1795

Maybe try to use QueryableAttribute. Look here:

Expand support

2) Supporting $select and $expand on single entities through QueryableAttribute.

Upvotes: 0

Jen S
Jen S

Reputation: 4555

You didn't mention the URI you're using to get the supplier entity, but I'm going to assume it looks something like: http://.../ServiceRoot.svc/supplier(1). By default in OData, navigation properties are not expanded; that is, by default, requesting a supplier will not include the IDs and Names of the linked products unless you ask for them explicitly via the $expand query option. For example: http://.../ServiceRoot.svc/supplier(1)?$expand=products.

If you don't expand the navigation property, then the products property of the supplier will show up as just a collection of links to the product entities. If you aren't seeing links to the products in the response payload, it could be because you're using the new v3 JSON format of OData, which may omit navigation links that follow the general OData URI conventions (since the client can generate those links itself).

If you include the request URI and payload you're getting back, I can be a bit clearer on what's happening in your situation.

A quick note on terminology: "complex type" in OData usually refers to a structural type that has no identity. A typical example of this could be an Address type, which is a value type that has multiple components (city, country, street, etc.) but doesn't need to have its own key. What you're talking about here are navigations between entities.

Upvotes: 1

Related Questions