okapi icon indicating copy to clipboard operation
okapi copied to clipboard

Issue annotating HTTP status codes for responses

Open dlahmad opened this issue 3 years ago • 1 comments

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.

dlahmad avatar Aug 13 '22 14:08 dlahmad

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 avatar Aug 14 '22 10:08 ralpha

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

ralpha avatar Dec 03 '23 22:12 ralpha