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

Async minimal route handlers don't serialize child members #39856

Closed
halter73 opened this issue Jan 29, 2022 · 0 comments · Fixed by #39858
Closed

Async minimal route handlers don't serialize child members #39856

halter73 opened this issue Jan 29, 2022 · 0 comments · Fixed by #39858
Assignees
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels bug This issue describes a behavior which is not expected - a bug.

Comments

@halter73
Copy link
Member

The issue is best demonstrated with code. The response bodies for each route handler are written as comments.

var app = WebApplication.Create(args);

app.MapGet("/", Parent () => new Child()); // { "c": "c", "p": "p" }

app.MapGet("/task", () => Task.FromResult<Parent>(new Child())); // { "p": "p" }
app.MapGet("/valuetask", ValueTask<Parent> () => new(new Child())); // { "p": "p" }

app.MapGet("/taskobj", () => Task.FromResult<object>(new Child())); // { "c": "c", "p": "p" }
app.MapGet("/valuetaskobj", ValueTask<object> () => new(new Child())); // { "c": "c", "p": "p" }

app.Run();

record Parent(string P = "p");
record Child(string C = "c") : Parent;

This shows an inconsistency in how returned objects are serialized in async methods that return a Task<Parent> or ValueTask<Parent> vs non-async methods that return Parent when the runtime type is a child type.

The non-async route handler has the right behavior because it results in a call to WriteAsJsonAsync<object>(...) instead of WriteAsJsonAsync<Parent>(...) which is what gets called in the async case. See https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism

@halter73 halter73 added bug This issue describes a behavior which is not expected - a bug. area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jan 29, 2022
@halter73 halter73 self-assigned this Jan 29, 2022
@halter73 halter73 changed the title Async minimal route handlers don't serialize the polymorphic members as JSON Async minimal route handlers don't serialize child members Jan 29, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant