Skip to content

fix: return HTTP 405 when request method mismatch #25

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

Merged
merged 5 commits into from
Apr 24, 2025

Conversation

nek023
Copy link
Contributor

@nek023 nek023 commented Oct 19, 2024

Overview

I fixed OapiRequestValidator to return http.StatusMethodNotAllowed instead of http.StatusNotFound when the request method does not match the routing definition.

Details

When the request method does not match the routing definition, the router returns ErrMethodNotAllowed.
However, in the current implementation of OapiRequestValidator, it returns http.StatusNotFound regardless of the error returned by the router.
This caused an unexpected response where the status would be status = 404, body = method not allowed when the request method didn’t match.

In this PR, I fixed validateRequest() to use errors.Is() to check the error and return the appropriate status code based on its value.

Additionally, I added test code for cases where the request method doesn't match.
This issue only occurs when combined with net/http, as both chi and gorilla probably handle this internally and return http.StatusMethodNotAllowed without needing fixes.

@nek023 nek023 requested a review from a team as a code owner October 19, 2024 15:23
@nek023 nek023 changed the title Return `http.StatusMethodNotAllowed for request method mismatch Return http.StatusMethodNotAllowed for request method mismatch Oct 19, 2024
@nek023 nek023 changed the title Return http.StatusMethodNotAllowed for request method mismatch Return http.StatusMethodNotAllowed for request method mismatch Oct 19, 2024
@jamietanna jamietanna added the bug Something isn't working label Apr 24, 2025
@jamietanna
Copy link
Member

jamietanna commented Apr 24, 2025

Thanks very much! Sorry for the delay 💜

@jamietanna jamietanna merged commit c74e132 into oapi-codegen:main Apr 24, 2025
14 checks passed
@jamietanna jamietanna changed the title Return http.StatusMethodNotAllowed for request method mismatch fix: return HTTP 405 when request method mismatch Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

Successfully merging this pull request may close these issues.

2 participants