-
Notifications
You must be signed in to change notification settings - Fork 74
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
How to Use Schema with Path Parameter? #219
Comments
Can you try this? @bp.route('/<int:clientId>')
@blp.arguments(ClientSchema, location='path')
class ClientResource(MethodView):
def get(self, *args, **kwargs):
pass |
That gives me the description in the OpenAPI interface, but also results in Stack Trace
If I move the arguments decorator on the
I get similar type errors complaining about umbers of arguments. |
Yes, sorry, it should be on the function, not the class. The issue with this method is that both Flask and marshmallow/webargs send the parameter to the function. I don't know how to inhibit this in Flask to let the schema do it alone. I never use a schema to validate and document path parameters. I rely on werkzeug path parameter converters for the validation, and if I define a custom path parameter converter, I register it in flask-smorest to get automatic documentation as explained in https://flask-smorest.readthedocs.io/en/latest/openapi.html#register-custom-path-parameter-converters. To add extra documentation, one can then pass In your case, I'd make clientId a read-only field so that it is not used on PUT and I'd rely on flask/werkzeug to pass the id on GET/PUT/DELETE. You lose the Note that werkzeug's int converter has a TL;DR This should get you there. Or close. @bp.route('/<int(min=1):clientId>', parameters=["clientId": {'description': 'The ID of the client'})
class ClientResource(MethodView):
def get(self, *args, **kwargs):
pass |
This is actually my main motivation. I'm trying to keep things as centralized as possible by not having documentation scattered around and not having to check arguments at the head of every method (the example I gave above is very simplified). But as long as using @bp.route('/<int:clientId>')
class ClientResource(MethodView):
@bp.arguments(ClientSchema, location='path', as_kwargs=True)
def get(self, **kwargs):
pass isn't the wrong way to do things, and will not cause me any issues, then this seems relatively neat.. |
I did think that the parameter converters could be employed to solve this issue, but I'm not familiar with werkzeug so really don't know how to do this. |
My example above should do what you want. I understand the will to centralize. I'd choose my way other yours because sending clientId twice to the view func kinda sucks while my method only leaves a bit of documentation in the call to route which is not that bad. Yours should work too. |
In the example below, I am trying to figure out how to use the
ClientSchema
to correctly document and validateClientResource.get
. Is this possible?The text was updated successfully, but these errors were encountered: