Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groupby not working without NewtonsoftJson #2030

Open
keatkeat87 opened this issue Jan 27, 2020 · 3 comments
Open

groupby not working without NewtonsoftJson #2030

keatkeat87 opened this issue Jan 27, 2020 · 3 comments
Assignees

Comments

@keatkeat87
Copy link

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
Copy link

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
Copy link

Leonhard1987 commented Jul 10, 2023

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
Copy link

This helped me with Newtonsoft:
OData/AspNetCoreOData#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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants