Skip to content

fix(openapi): use the error type's bad_request_handler in Result responses - #1230

Merged
attila-lin merged 1 commit into
poem-web:masterfrom
b84330808:fix/result-error-bad-request-handler
Oct 22, 2026
Merged

attila-lin merged 1 commit into
poem-web:masterfrom
b84330808:fix/result-error-bad-request-handler

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Fixes #967

If an operation returns Result<T, E> and only E has #[oai(bad_request_handler = "...")], the handler is never called. A missing or invalid parameter gets the default 400 response instead.

The cause is impl ApiResponse for Result<T, E>: it sets BAD_REQUEST_HANDLER from T only, and from_parse_request_error always builds Ok(T::...).

This change reports a handler when either side has one. When T has none, the error goes to E's handler and comes back as Err. When both have one, T still wins, so existing code behaves the same. poem::Result<T> is unaffected because poem::Error has no handler.

I added a test in tests/api.rs for a missing parameter, a failed validator, and the case where both types have a handler. I also added a line to the poem-openapi changelog.

I used Claude Code to write this change.

…onses

`impl ApiResponse for Result<T, E>` only looked at `T` for a custom bad
request handler, so a handler declared on the error type was never
called and the default 400 response was returned instead.

Report a handler when either side has one. `T`'s handler still wins when
both define one, so existing APIs keep their current behaviour.

Fixes #967

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@attila-lin

Copy link
Copy Markdown
Collaborator

Thanks @b84330808! Small, focused fix for #967 with good tests covering both precedence cases. Merging 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bad_request_handler cannot be overriden

2 participants