dotnet / dotnet/aspnetcore

ProducesResponseTypeAttribute ContentType Unexpected Behaviour

Open
#58,140 0 comments 2 reactions 0 assignees View on GitHub
area-mvc
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

### Describe the bug

I am encountering an issue where the Content-Type of the response returned by an action, which utilizes ProducesResponseTypeAttribute to specify the content type, does not match my expectations. Specifically, when I set the action to return a string with the Content-Type set to "application/json", the actual response Content-Type I receive is "text/plain". On the other hand, when I utilize the ProducesAttribute to define the content type explicitly, I receive the expected Content-Type of "application/json".

### Expected Behavior

The response’s Content-Type should be "application/json" when the ProducesResponseTypeAttribute specifies application/json, rather than returning "text/plain".

### Steps To Reproduce

Access the minimal project that demonstrates this issue: [ProducesResponseTypeIssue GitHub Repository](https://github.com/pedrosanguineti/ProducesResponseTypeIssue).
Run the solution.
Open the ProducesResponseTypeIssue.http file.
Send both requests:
The first request (uses ProducesAttribute) will return a response with the Content-Type of "text/plain".
The second request (uses ProducesResponseTypeAttribute) will return a response with the Content-Type of "application/json" as expected.

### Exceptions (if any)

No exceptions were thrown during the process.

### .NET Version

8.0.401

### Anything else?

dotnet --info
```
.NET SDK:
Version: 8.0.401
Commit: 811edcc344
Workload version: 8.0.400-manifests.a15686fb
MSBuild version: 17.11.4+37eb419ad

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.401\

Host:
Version: 8.0.8
Architecture: x64
Commit: 08338fcaa5

.NET SDKs installed:
6.0.300 [C:\Program Files\dotnet\sdk]
7.0.315 [C:\Program Files\dotnet\sdk]
7.0.408 [C:\Program Files\dotnet\sdk]
8.0.204 [C:\Program Files\dotnet\sdk]
8.0.300 [C:\Program Files\dotnet\sdk]
8.0.401 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
```
Update 1: improves readability.
Update 2: specifies the attributes used in the request of the sample code.

Contributor guide

Open the contributing guide

Research direction

Run the linked ProducesResponseTypeIssue minimal project and send both requests from ProducesResponseTypeIssue.http, comparing the ProducesAttribute and ProducesResponseTypeAttribute response headers. Read the two attribute usages described in the issue first; done means the response using application/json reports the expected Content-Type consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.