-
Notifications
You must be signed in to change notification settings - Fork 10.3k
OkObjectResult with generic type parameter #48324
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
@lus thanks for contacting us. I think what @mitchdenny mentions is right. The only thing I can suggest is authoring an https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.metadata.ijsontypeinforesolver?view=net-7.0 to take over the proxy serialization yourself. But this is a case where ASP.NET does not know about EF and EF does not know about ASP.NET, and is not a boundary we want to break, so there is some additional integration work. MVC does a lot more internally than just calling If I had to solve this, I would try the type info resolver to try and detect the proxy types and return the contract for the base instance instead. As for how to do that exactly, I will defer to the docs on config for system.text.json and mvc. |
Another option would be to write your own generic We could consider new API that does essentially this, but we'd have to think about consistency with |
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. |
@halter73 thank you for your detailed reply. I do understand now that making this change to the API is not that straightforward as I may have thought, so thanks for clearing this up. |
In case anyone else runs into this here's a JsonConverter that helps solve it: /// <summary>
/// Used to serialize EF Proxy objects using the non proxy class. This solves a number of issues
/// including not serializing added proxy properties and respecting Json Attributes on the non
/// proxy class (like JsonIgnore or JsonPropertyName)
/// </summary>
public class UnwrapEFProxyJsonConverter : JsonConverter<object>
{
public override bool CanConvert(Type typeToConvert)
{
var entityTypeFromProxy = ObjectContext.GetObjectType(typeToConvert);
// types won't match if typeToConvert is an EF proxy
return entityTypeFromProxy != typeToConvert;
}
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("UnwrapEFProxyJsonConverter converter is not for deserialization");
}
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
var entityTypeFromProxy = ObjectContext.GetObjectType(value.GetType());
JsonSerializer.Serialize(writer, value, entityTypeFromProxy, options);
}
} |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
I am a bit perplexed that issue #31396 was closed with the simple response "there is nothing we can do about it" as it seems like it is an issue that is very messy to work around. Please correct me if I am missing something.
The root of the problem described there is that ASP.NET uses
JsonSerializer.Serialize<object>
for serializingOkObjectResult
s.EF Core returns proxies of some types with some additional properties that are unwanted in the resulting JSON.
Because
Serialize<object>
is used instead ofSerialize<wanted_type>
, these properties are not ignored as they lack the[JsonIgnore]
attribute (which seemingly cannot be added either as an EF Core maintainer acknowledged this issue without bringing this up as a possible solution).Describe the solution you'd like
As long as I am not missing something: would it technically be possible to add an additional
OkObjectResult<T>
class that usesT
rather thanobject
(and subsequentlyJsonSerializer.Serialize<T>
rather thanJsonSerializer.Serialize<object>
)?We could then use
in the controller.
Additional context
I know that there were other issues suggesting this, but they were closed because multiple people think #8535 would be a better solution.
As I am not sure about whether this would solve this issue as well, I am going to bring this up again.
The text was updated successfully, but these errors were encountered: