-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Make ()
and None
return a HTTP 204 response instead of 200
#2393
Conversation
Because there are a lot of test routes ending with a semicolon.
There are also tests in the examples folder. This one fails (line 75): axum/examples/handle-head-request/src/main.rs Lines 66 to 81 in 801a78a
Hm. The return value from the route is
Which now produces a HTTP 204 response. I'm not sure yet why that is. I'll keep looking. edit: I understand now why this response is generated and this is actually intented behaviour. The implementation of the |
The trait implementation is here: https://github.com/tokio-rs/axum/blob/801a78a4cf387694636a98df4367443258e815a2/axum-core/src/response/into_response.rs#L352-L362 The trait implementation does not create a body and this test example is a HEAD request, which can use either 200 or 204: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#successful_responses https://www.rfc-editor.org/rfc/rfc9110#status.204
I think |
That's a good point. I can easily remove the |
Yes I agree we should remove the impl for About |
I removed the implentation for
I think it is fair to say that it is a breaking change. But it is also a pre-1.0 breaking change and this gives you the opportunity to discuss how axum should behave at version 1.0. The way I see it: Using a HTTP 204 header will make it easier for users to write tests that check that there is no response body for a given route. Current tests might break, but they will become more reliable after this breaking change. If you think that the current behaviour is fine then feel free to close this PR. |
To be fair, the tests that this PR updates are testing the core behaviour of axum. And the test behaviour is typically: I make a request and I expect a valid response. And most tests only care about the HTTP headers and not about the body. And it's very convenient to write a test that just returns But projects that use axum as a library will have a lot more tests that expect an actual body in the response. The routes that return a I've taken my time to update all of the tests in this PR. It was a lot of tests and I looked at each one individually to make sure I'm not overlooking something. But most tests in this project are fairly easy to read through and understand. It was really not that much work to fix them. It will probably be less of an issue for downstream projects. Or do you have a particular project in mind that would be heavily affected by this change? |
I'm not worried that updating the tests will be hard. I think you're right that it'll be easy. My question is more about whether it's worth the churn and whether this change really matters. I'm still not sure about that and not sure how to decide 🤔 |
I'm still not sure I think this is a great idea so I think I'll be a bit conservative and close this for now. Thanks for the suggestion though! |
I also encountered this.
If we do not want to change the |
I like that idea! Want to send a PR? |
We cannot change the behavior of `<() as IntoResponse>::into_response`, so add a newtype struct to explicitly use 204 No Content status. Refs: tokio-rs#2363, tokio-rs#2393
We cannot change the behavior of `<() as IntoResponse>::into_response`, so add a newtype struct to explicitly use 204 No Content status. Refs: tokio-rs#2363, tokio-rs#2393
Motivation
I'm proposing an implementation for #2363: The unit type
()
andNone
should generate a HTTP 204 response.Solution
This PR does the following things:
IntoResponse
for()
to always return a HTTP 204 resultAdd an implementation of.IntoResponse
forOption<T: IntoResponse>
so that HTTP 204 is returned when theOption
isNone
axum/src/response/mod.rs
. (Please let me know if they should be somewhere else)