dotnet / dotnet/aspnetcore

OkObjectResult with generic type parameter

Open
#48,324 5 comments 1 reaction 0 assignees View on GitHub
area-mvc enhancement
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### 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` for serializing `OkObjectResult`s.
EF Core returns proxies of some types with some additional properties that are unwanted in the resulting JSON.
Because `Serialize` is used instead of `Serialize`, 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` class that uses `T` rather than `object` (and subsequently `JsonSerializer.Serialize` rather than `JsonSerializer.Serialize`)?

We could then use

```cs
MyType proxiedValue = dbSet.Find(...);

return Ok(proxiedValue);
```

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.