0b01001001 / spectree

API spec validator and OpenAPI document generator for Python web frameworks.
https://0b01001001.github.io/spectree/
Apache License 2.0
317 stars 74 forks source link

Openapi description for path variable #131

Closed parisni closed 2 years ago

parisni commented 3 years ago

So far, I can't find a way to add a description for path variables. On the flask side, It would be cool to have a parameter such doc as flask-restfull provides :

@api.route('/my-resource/<id>', endpoint='my-resource')
@api.doc(params={'id': 'An ID'})
kemingy commented 3 years ago

Check this example: https://github.com/0b01001001/spectree/blob/36c4d18834eb2bbe19098d97e24036baca3943e2/examples/flask_demo.py#L55-L76

For the path variables, this library doesn't provide a strong validation with pydantic. It will use the default path converter.

But of course you can still get the correct OpenAPI document for path variables.

parisni commented 3 years ago

But of course you can still get the correct OpenAPI document for path variables

Sorry I cannot find out in your example how to add a description to source and target path variable. I can see how to add constraints on them such as length but no description field that would appear in the openapi specs

kemingy commented 3 years ago

Sorry I misunderstood your question before. Currently, this library doesn't support descriptions for the path variables. You can use function description for the path variables.

One possible way is to add another path model. But I'm not sure if we should validate the input again.

class PathVariables(pydantic.BaseModel):
    source: str = pydantic.Field(..., description='source language')
    target: str

@api.route('/api/<string:source>/<string:target>')
@spec.validate(path=PathVariables)
def index(source, target):
    pass

Feel free to give more feedbacks about this.