OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
854 stars 475 forks source link

groupby not working without NewtonsoftJson #2030

Open keatkeat87 opened 4 years ago

keatkeat87 commented 4 years ago

error message : Unable to cast object of type 'System.String' to type 'Microsoft.AspNet.OData.Query.Expressions.GroupByWrapper'. image

when i add NewtonsoftJson to my project, this error gone. dada

Assemblies affected

asp.net core 3.1, odata 7.3 beta

Reproduce steps

1.git clone https://github.com/keatkeat87/odata-groupby-without-NewtonsoftJson.git 2.dotnet ef migrations add init 3.dotnet ef database update 4.F5 5.visit /odata/Products?$apply=groupby((category))

Expected result

it should work without NewtonsoftJson

Actual result

when not using NewtonsoftJson, error come out.

thesheps commented 3 years ago

Any love for this issue folks? This is still a problem a year later. I've reproduced this in a much simplified example based on the excellent research by @keatkeat87

https://github.com/thesheps/groupby-issue

This example has no dependency on Entity Framework at all, and has a reduced number of moving parts. If you spin this example up and hit:

http://localhost:5000/odata/products?$apply=groupby((Category))

You'll see the following error: Unable to cast object of type 'System.String' to type 'Microsoft.AspNet.OData.Query.Expressions.GroupByWrapper'.

If you uncomment line 16 of Startup.cs which enables Newtonsoft.Json, the example runs successfully BUT the response has any odata.context variables omitted.

This makes me think there's something wrong in the underpinnings of the default OData serializer. Happy to get involved in devving a fix for this, but would love some guidance from the community!

Leonhard1987 commented 1 year ago

Funnily I have an error when enabling Newtonsoft and it works fine with system.text My Error is: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'Microsoft.AspNetCore.OData.Query.Wrapper.GroupByWrapper'.'

in class: NewtonsoftJsonOutputFormatter.cs

CallStack:

Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(object target) Unknown Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.Serialization.JsonContainerContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonProperty property, out Newtonsoft.Json.Serialization.JsonContract memberContract, out object memberValue) Unknown Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) Unknown Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) Unknown Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(Newtonsoft.Json.JsonWriter writer, System.Collections.IEnumerable values, Newtonsoft.Json.Serialization.JsonArrayContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) Unknown Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(Newtonsoft.Json.JsonWriter jsonWriter, object value, System.Type objectType) Unknown Newtonsoft.Json.dll!Newtonsoft.Json.JsonSerializer.SerializeInternal(Newtonsoft.Json.JsonWriter jsonWriter, object value, System.Type objectType) Unknown

lionengine123 commented 1 year ago

This helped me with Newtonsoft: https://github.com/OData/AspNetCoreOData/issues/807

package in csproj:
<PackageReference Include="Microsoft.AspNetCore.OData.NewtonsoftJson" Version="8.2.0" /> and in startup.cs:

            var mvc = services.AddMvc(options => options.EnableEndpointRouting = false)
                .AddODataNewtonsoftJson()