GREsau / okapi

OpenAPI (AKA Swagger) document generation for Rust projects
MIT License
606 stars 110 forks source link

Issue annotating HTTP status codes for responses #111

Closed dlahmad closed 10 months ago

dlahmad commented 2 years ago

Is there a way to annotate a rocket handler method with the HTTP status codes it (possibly) returns a response?

I tried to find the functionality, but it seems not to be there.

ralpha commented 2 years ago

I'm not sure what you mean. But I think you mean something like this: https://github.com/GREsau/okapi/blob/5cb59b3fddcf24294b4c6110882d89a68eaf01c1/examples/special-types/src/main.rs#L26

This way you can directly change the HTTP status.

If you instead want to use a Result and get a list of all the possible HTTP code in you openApi docs use:

fn create_message(message: crate::DataResult<'_, Message>) -> Result<Json<Message>, error::Error>>

Where the Error is your own custom error.

You can then add the openApi errors to it like this: https://github.com/GREsau/okapi/blob/master/examples/custom_schema/src/error.rs

But to know what HTTP status codes an endpoint can return, you have to figure that out yourself. This is because this is depended on the logic of the code and could even be runtime determined so, no way to know at compile time.

ralpha commented 10 months ago

Added a new FAQ question to add some more info for others that might have similar questions. https://github.com/GREsau/okapi#q-how-do-i-document-my-endpoints