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

Results.Json(null) stopped producing a response body in .NET 8 (and 7) #53509

Closed
1 task done
WGroenestein opened this issue Jan 20, 2024 · 4 comments
Closed
1 task done
Labels
area-web-frameworks ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@WGroenestein
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In .NET 8 (and 7) when a null value is given to Results.Json, it no longer outputs "null" in the response body, instead the response body is empty

Expected Behavior

null

is put in the response body

Steps To Reproduce

https://github.com/WGroenestein/ResultJsonRepro

Exceptions (if any)

No response

.NET Version

8.0.100

Anything else?

Since .NET 8 (the new LTS) was released Nov 2023 and .NET 6 (current targeted LTS) is EOL Nov 2024 we are trying to switch our projects over to .NET 8 (LTS => LTS upgrade). However this LTS upgrade causes our API contracts to break.

We didn't find anything related to this breaking change in the msdn docs (https://learn.microsoft.com/en-us/dotnet/core/compatibility/7.0 and https://learn.microsoft.com/en-us/dotnet/core/compatibility/8.0)

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Jan 20, 2024
@martincostello martincostello added area-web-frameworks and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Jan 20, 2024
@teociaps
Copy link

The issue you're encountering seems to be relative to a change in JSON result handling between .NET versions 6 and 7/8, particularly in the context of minimal APIs. In .NET 6, the JsonResult class from MVC was used, whereas in newer versions 7 and 8, a new typed result class called JsonHttpResult<TValue> is utilized behind the scenes.

The key distinction lies in how null values are managed. With JsonHttpResult<TValue>, if the Value passed to it is null (your case), the JsonHttpResult<TValue>.ExecuteResultAsync method returns before writing the JSON result to the response body:

image

and then, if Value is not null:

image

JsonResult, on the other hand, behaves differently. It sets a default content type (application/json) and serializes the value to the response body regardless of whether it's null or not.

This is why Results.Json(null) behaves as you expected only in MVC/API Controller in .NET 7/8, while in minimal API it doesn't. In contrast, in .NET 6, it works consistently across both scenarios.

@captainsafia captainsafia added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label May 7, 2024
@captainsafia
Copy link
Member

@teociaps Thanks for taking the time to post this explainer.

It doesn't look like we documented this change in our announcements repo at https://github.com/aspnet/Announcements. I'll go ahead and file an issue to document this.

Copy link
Contributor

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.

@captainsafia
Copy link
Member

Still on my radar to add the breaking change notice here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web-frameworks ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

5 participants
@martincostello @captainsafia @WGroenestein @teociaps and others