OData / odata.net

ODataLib: Open Data Protocol - .NET Libraries and Frameworks
https://docs.microsoft.com/odata
Other
686 stars 349 forks source link

Serialization only supports 2 deep expand #996

Open SETravis opened 6 years ago

SETravis commented 6 years ago

When attempting to nest more than 2 expands nothing shows up after the first 2. No error is given. I have added [EnableQuery(MaxExpansionDepth = 0)] so not getting an error about exceeding limit.

Assemblies affected

Reproduce steps

A simple example is a User object that has a self referencing "CreatedBy" navigation property.

OData/Users?$expand=CreatedBy($expand=CreatedBy) works as expected returning: User { ... CreatedBy { ... Createdby { ... } } }

Adding another level: OData/Users?$expand=CreatedBy($expand=CreatedBy($expand=CreatedBy)) returns the same result as above without error.

Expected result

The second request should return a third 'CreatedBy" nested under the second.

Actual result

The 3rd expansion level was not serialized.

Additional detail

I saw previous post about making sure any navigation properties were EntitySets. I have used this simple example so there is only one Entity involved which does expand correctly at the first two levels. The actual issue I'm having is more complex and does not involve self references.

SETravis commented 6 years ago

There appears to be an issue in creating the nested write context (ODataSerializerContext) inside of ODataResourceSerializer.WriteComplexAndExpandedNavigationProperty. The write context contains a NavigationSource which is used to determine the NavigationSource of the next nested write context. The first time a nested write context is created the context used to create it points to "user" and contains an 'unknownNavigationPropertyCache' which includes the CreatedBy navigation property. This property is placed into the newly created nested write context. It does not however contain any navigationProperties in the Cache. When the next nested write context is created the NavigationSource of the write context is null which prohibits the 3rd level of "CreatedBy" from be serialized out.

robward-ms commented 6 years ago

@SETravis - You are seeing the issue when using WebApi? I think you are referencing this code: https://github.com/OData/WebApi/blob/88c5f01f2ee2d685d873b2ab075909b7da6651f1/src/System.Web.OData/OData/Formatter/Serialization/ODataResourceSerializer.cs

Do you have a sample repo?

SETravis commented 6 years ago

I enabled debugging according to the directions. I did not pay attention that the source was coming from OData/WebAPI. Do I need to add the issue to OData/WebAPI or should I create simple source to reproduce and add here?

SchlenkR commented 5 years ago

This issue is pretty old, but I stumbled across it and it seems it's still occurring. I use Microsoft.AspNetCore.OData, v7.1.0