-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[JsonIgnore] not working with EF Core types when requesting single resource when Lazy Loading is enabled (.NET 5) #31396
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
Comments
The workaround: use manual serialization and
Don't forget to provide the same serializer options you configured via |
The output from the app you provided seems fine:
What am I missing? |
I am surprised to see that the first "Ignored" field is not showing up. I didn't notice that before. However the other property
The workaround produces the intended output:
This leads me to a new hypothesis, which I tested as follows:
This produces the incorrect output
My interpretation is that
None of these behaviors, on its own, looks like a bug, but in combination, the unwanted behavior occurs. I'm pretty sure that manual serialization in the controller will prevent the OData service from working. Is there another workaround that would not have this drawback? |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Are there any updates on this? I have the exact same issue ( In my concrete case, the issue was that I created a temporary private ContentResult FixedOk<T>(T obj)
{
return Content(JsonSerializer.Serialize(obj, _jsonSerializerOptions), "application/json");
} where |
Can confirm this bug is now 2 years old, and alive and thriving as ever in .NET 6 / Azure Functions @msftbot That's a nice 1 year old dusty link to triage there. Could you maybe provide a link to wherever you stoved this bug away? |
@ajcvickers interested in your thoughts here? |
Now there's life in this thread, I'd like to add to the scope, that the workaround, in my case, is adding BOTH System.Text.Json.Serialization.JsonIgnoreAttribute and System.Runtime.Serialization.IgnoreDataMemberAttribute. The reason being that System.Text.Json.JsonSerializer is immune to System.Runtime.Serialization.IgnoreDataMemberAttribute, but I use JsonSerializer in tests. Another issue is Refit that does not see System.Runtime.Serialization.IgnoreDataMemberAttribute either, so the choice of implementation is between inconsistent code (using a different attribute according to context where possible) or use both attributes. Both solutions are far from optimal. |
To be clear, this isn't an ASP.NET specific issue. The interaction of EF generated proxies and the JSON serializer exist outside the context of ASP.NET. The only flavor that ASP.NET adds to this discussion is some indirection that can change what serializer options come into play. How Refit intersects with this is even further removed. But let's continue here for a little while and avoid conways law for a little while longer :) If @ajcvickers chimes in we might be able to make a case to move this over to the efcore repo for further conversation. The fundamental reason we see this behaviour is that when EF core has lazy loading enabled it makes use of a proxy. That proxy overrides the implementation of the navigation properties ( I'm not sure if the EF team has some clever ideas on how to work around this, but I think that we are unlikely to see a solution on the other side where STJ somehow becomes aware of Castle-based proxies and looks for the base type to infer how to ignore properties. You can make the case by filing an issue in the dotnet/runtime repo. So the reality is that lazy loading and JSON serialization don't mix well together today - not a bug, just merely the absence of a feature. |
@mitchdenny Thanks for the info! Assuming you mean Entity Framework when you mention EF, I can inform that I don't use EF. I see this issue when returning |
There is a comment above which sums up this issue nicely:
I don't think there is much can be done about this. If the code uses a proxy, but the serializer should treat it as a non-proxy for serialization purposes, then that is done by using the type with the generic method. @noontz I don't think you are describing the same issue as is being discussed here. |
@ajcvickers "I don't think you are describing the same issue as is being discussed here".. You are absolutely correct. Sorry for the stir.. Google sent me here as System.Text.Json.Serialization.JsonIgnoreAttribute was being ignored, and I pulled the trigger way to fast in the wrong context.. This seems to be a rather conceptual issue across technologies? |
I'm going to resolve this as by design for the moment then. |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Describe the bug
When an object is serialized by ASP.NET controller methods, the [System.Text.Json.Serialization.JsonIgnore] attribute is (ironically) ignored under the following circumstances:
The object is returned directly, not wrapped in IEnumerable or IQueryable.
The object is an EF Core object with Lazy Loading enabled. Since the derived type is generated in a dynamic assembly in memory, I can't decompile it to investigate why this is relevant, but the mere fact that a derived class is being serialized (rather than
EntityDBO
itself) is insufficient to trigger the bug.Default JSON serialization options are used, or
IMvcBuilder.AddJsonOptions
is used to configure JSON (i.e. Newtonsoft is disabled)This bug was described on StackOverflow over a year ago, but I can't find it in the issue tracker.
To Reproduce
I prepared a repo that demonstrates the problem with EF Core 5.0.4:
To reproduce, simply run it and browse to https://localhost:44369/api/controller/1
Further technical details
ASP.NET Core version: the project file does not refer to any ASP.NET packages, but I am having this bug in a .NET 5.0 project as well as the .NET Core 3.1 test project
IDE: Visual Studio 2019, 16.9.0
The text was updated successfully, but these errors were encountered: