Skip to content
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

Support multiple schemas in @response? #64

Open
anilravuri opened this issue Oct 6, 2022 · 7 comments
Open

Support multiple schemas in @response? #64

anilravuri opened this issue Oct 6, 2022 · 7 comments
Labels
question Further information is requested

Comments

@anilravuri
Copy link

This is a requirement we had in our team where we have a different set of responses (thus multiple schemas) for a given endpoint. I tried marshmallow-oneofschema but that does not seem to work. Any thoughts on this?

@miguelgrinberg
Copy link
Owner

I don't consider the idea of multiple schemas a good API design practice, so I really have no direct experience (or interest) in implementing this. But marshmallow supports nesting schemas, which may give you something close to what you need. Or else you can hand modify the OpenAPI data structure in a process_apispec callback and make it be exactly what you need.

@miguelgrinberg miguelgrinberg added the question Further information is requested label Oct 6, 2022
@sgavil
Copy link

sgavil commented Feb 27, 2023

@miguelgrinberg What would be the approach if I wanted to return a schema if everything goes well but some kind of failed schema if something goes wrong?

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Feb 27, 2023

@sgavil Your question makes me think that you think APIFairy somehow prevents you from returning an error response based on a schema. This is actually incorrect, you can return anything you want in an error handler, including a serialized Marshmallow schema.

In terms documenting your error schemas, see the @other_responses decorator.

@sersorrel
Copy link

The thing that trips me up with this is that, say you have a flask-httpauth error handler which might return a 401, 403, whatever, and you apply this flask-httpauth object to a route with @apifairy.authenticate: now you must also remember to apply @apifairy.other_responses to the route to show what the error handler could return, and you have to do this for every route you use the error handler on. That makes it very easy to forget the @apifairy.other_responses and accidentally give the impression that a route will never fail, when really it can return the same authentication failures as any of your other authenticated routes, and makes it very difficult to ever change what your error handler produces, because you would have to update the @other_response decorator on every route it is used on.

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Feb 27, 2023

@sersorrel You can create your own custom authentication decorator that passes all its arguments through to apifairy.authenticate and also includes apifairy.other_responses with the set of canned error responses you'd like all your authenticated endpoints to have.

@sgavil
Copy link

sgavil commented Feb 27, 2023

@miguelgrinberg But if I add the @response with a schema and then I return a different failure schema, the failure schema gets converted into the schema I've specified in the response decorator. Am I doing something wrong?

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Feb 27, 2023

@sgavil Yes, you should return your error responses in an error handler, not in the route function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants