AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

Use expand with star *

Open manoack opened this issue 3 years ago • 5 comments

Hello,

I can use expand in complex situations. But customers ask for $expand=* syntax. But this expand seems be ignored. Is this a known problem or a missinterpretation of OData specification? http://docs.oasis-open.org/odata/odata/v4.0/os/part2-url-conventions/odata-v4.0-os-part2-url-conventions.html#_System_Query_Option_1

manoack avatar Nov 29 '22 14:11 manoack

@manoack can you provide a concrete example and more details on which version of OData you are using?

I'm asking because I just tested $expand=* and it is working on one of my OData endpoints here. I had never tried that and double checked out of curiosity.

From my perspective, this is already working as expected.

julealgon avatar Nov 29 '22 14:11 julealgon

Hmmm... I think i run in the same problem like you wrote on https://github.com/OData/AspNetCoreOData/discussions/603. Now I try to understand your approach and apply them correctly. Then I will come back.

manoack avatar Nov 29 '22 15:11 manoack

OData8.zip

I create an example, here is a part of my model:


  [DataContract]
  public class Parent1
  {
    [DataMember]
    public Child Child { get; set; }
    [DataMember]
    public int Id { get; set; }
    public Parent1() { }
  }

  [DataContract]
  public class Parent2
  {
    [DataMember]
    public IEnumerable<Child> Childs { get; set; }
    [DataMember]
    public int Id { get; set; }
    public Parent2() { }
  }

  [DataContract]
  public class Child
  {
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public IEnumerable<ChildA1> ChildsA1 { get; set; }

    [DataMember]
    public IEnumerable<ChildA2> ChildsA2 { get; set; }

    public Child() { }
  }


  [DataContract]
  public class ChildA1
  {
    [DataMember]
    public int Id { get; set; }
    public ChildA1() {
      
    }
  }

  [DataContract]
  public class ChildA2
  {
    [DataMember]
    public int Id { get; set; }
    public ChildA2() {
      
    }
  }

I add only two needed entitysets (commented code has no effect):

      var builder = new ODataConventionModelBuilder();
      builder.EntitySet<Parent1>("Parent1");
      builder.EntitySet<Parent2>("Parent2");;
      //builder.EntitySet<Child>("Childs");
      //builder.EntitySet<ChildA1>("ChildsA1");
      //builder.EntitySet<ChildA2>("ChildsA2");

Calls like

$expand=Childs($expand=ChildsA1,ChildsA2)

works like expected.

Calls like

$expand=*

ignores the ChildsA1 and ChildsA2 properties.

I play in my OData 8.0.11 enviroment mit Contained attributes too, without any effect.

manoack avatar Nov 30 '22 06:11 manoack

@manoack The response you are getting with $expand=* is the expected response. It only expands to one level. We are though working on supporting this $expand=*($levels=max) from ODL level.

ElizabethOkerio avatar Dec 06 '22 19:12 ElizabethOkerio

@ElizabethOkerio if i try to use $expand=*($levels=max) on my endpoints i'm getting ODataException: The $level option on navigation property 'property' is not allowed, because the related entity type 'propertyType' could not be cast to source entity type 'entityType'.

it's because of this

https://github.com/OData/odata.net/blob/18489649ab0f736421919602b6d1a32cc74ce2bc/src/Microsoft.OData.Core/UriParser/Binders/SelectExpandBinder.cs#L883-L897

is this normal ? (the fact that AspNetCoreOdata does not respect the odata convention, i mean) : https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_SystemQueryOptionexpand

Example 124: expand all related entities and their related entities
http://host/service/Categories?$expand=*($levels=2)

thanks for your answer !

Angelinsky7 avatar Nov 06 '24 15:11 Angelinsky7