Skip to content

How can I make the spec for OpenAPI show that my endpoint can return 400 as well? #676

Answered by NexRX
danechitoaie asked this question in Q&A
Discussion options

You must be logged in to vote

Here are two options for handling the bad request errors

Option 1:

The easiest one is probably doing this on you're enum:

#[derive(ApiResponse)]
#[oai(bad_request_handler = "bad_request_handler")]
enum CreateUserResponse {
    /// Returns when the user is successfully created.
    #[oai(status = 200)]
    Ok,
    /// Returns when the user already exists.
    #[oai(status = 409)]
    UserAlreadyExists,
    /// Returns when the request parameters is incorrect.
    #[oai(status = 400)]
    BadRequest(PlainText<String>),
}

// Convert error to `CreateUserResponse::BadRequest`.
fn bad_request_handler(err: Error) -> CreateUserResponse {
    CreateUserResponse::BadRequest(PlainText(format!("erro…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@danechitoaie
Comment options

@NexRX
Comment options

Answer selected by danechitoaie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants