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
[Feature Request - Community Feedback needed] Additional Responses #16
Comments
Did you check this link https://fastapi.tiangolo.com/tutorial/response-status-code/ ? |
@kurtrottmann I did, but that's only for changing the status code of the main (only) response. I'm wondering if it's possible to add documentations of the error responses. |
Thanks @kurtrottmann for helping here! That's highly appreciated 🎉 🌮 @pulitz There's still no defined way to declare them. I knew this would happen, but I didn't want to define the API/functionality for that yet, as I wanted to hear feedback for you guys (and other developers), before implementing it. Here's the problem/situation:When defining a single response model and status code, no matter what is actually returned by the path operation function, it will be converted to that model (and filtered by it) and that status code will be returned. If we have more than one possible status code, the path operation function has to return a Starlette This is because there is no way for FastAPI (actually for Python) to know what are all the possible values/data returned by the path operation function. What has to be done:So, this would mean, that the declared model and status code has to be declared twice (code repetition 😞 , that in this case, we can't avoid). Once in the path operation decorator (to declare it for the generated OpenAPI schema) and once inside the path operation function, to actually return it. The question, requested feedback:In summary, declaring additional response models/schemas and status codes will be "way more complex" than declaring a single one. Actually, it won't be that difficult, but will not be super-intuitive and simple, which is the ideal for FastAPI (and is what all the rest tries to achieve). I can think of two options: An additional parameter for the path operation decorator, something like This parameter could receive:
ReferencesOpenAPI operation object, that contains OpenAPI responses object that contains each OpenAPI response object that contains each field, and OpenAPI Media Type object (that contains the actual I know this gets hairy quickly, so I'll leave this issue open as a "feature request" to collect more feedback. I know each of those options above is doable, but what I'm concerned the most is the style of the developer-facing API, I want it to be as friendly and intuitive as the rest of FastAPI, or at least as close as possible. Actually, all the rest of FastAPI was designed similarly, first checking all the possible developer-facing functionality, in several editors, to ensure the best development experience for FastAPI users (developers), and then doing all the implementation to match that design. What do you guys think? |
Currently, I am using FastAPI to create an interface to call AWS services through boto3. And AWS raise ClientErrors which most (if not all) have the same structure and documented. |
Would you have simple examples for A,B,C ? I'm not sure I see concretly what that means if used in a simple example. So far for instance I got a post route that returns a 409 if user added is a duplicate, I wrote it this way and didn't feel the need for another black magic, but maybe that's because I was only concerned with the status_code
|
@euri10 Functionality-wise, yes, I am also doing something similar. However, what I want are for those different responses to be reflected in the auto-generated documentation. |
@pulitz I understand now, totally missed the perspective before. gonna read the openapi links now, what would be amazing to see in option A, B, C and D is a short example from a FastAPI user perspective |
I think Option A assuming I understand the options provided. I've no problem creating the response models. That's something I generally re-use anyway. |
I'm for Option A but with a small change - instead of mapping response codes directly to pydantic model, I'd map it to sub-dict that contains additional metadata useful for enriching generated OpenAPI. Similar to Path Operation Configuration. E.g.: {
409: {
"model": Item,
"description": "The item was updated on the server. Refresh your copy and try again",
...
}
} This way we can still keep it Pythonic while covering majority of use-cases (every abstraction has trade-offs and as someone who wrote thousands over thousands lines of OpenAPI yamls in the last years I'm very happy that I don't need to it anymore with FastAPI). So far FastAPI keeps OpenAPI behind the scenes (great!) and I believe it should continue this way. |
☝️This seems like option D to me. Maybe a hybrid where if the property of the status code is a:
Could get the best of both worlds? |
@troyswanson IMHO hybrid approach is too magical. But I understand it's subjective. |
Just chiming in to say I would really like to have this feature. Documenting multiple responses and errors is important for me. |
+1 |
This would be a great feature to have. Agree with @haizaar. |
Thanks for the feedback guys. This is up there in the priority list, along with some things that are missing (like docs about testing). |
Guys, we have it 🎉 🚀 It's in between the hybrid and explicit approach, helping minimize code as possible but keeping all the flexibility. Here are the new docs: https://fastapi.tiangolo.com/tutorial/additional-responses/ And the extended docs for bigger applications: https://fastapi.tiangolo.com/tutorial/bigger-applications/#add-some-custom-tags-and-responses It's avaliable in version There's a new parameter Each of these additional response This additional response If one of these response It means that, you can use the parts that you need, and/or combine them. For example, you can define a Pydantic model for a response, but declare that the same response can also return an image (or any other content type) apart from the JSON from the Pydantic model. |
Thanks @mostaphaRoudsari ! Yep, it should be fixed in #140 , released in in version |
As this was solved a while ago, I'll close this issue now. But feel free to add more comments or create new issues. |
I Would like to add the header in a response, using a model Pydantic, but the documentation mentioned is not availibled, how can i do it? I would like to add a Header, like this: Somebody knows, how i can do this? |
How do we add additional responses (e.g. 401, 403, 500, etc..) to the documentation other than the default 200 response and the 422 validation error response?
The text was updated successfully, but these errors were encountered: