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

Multiple Routes Swagger Documentation #658

Merged
merged 10 commits into from Jul 2, 2019
Merged

Conversation

SteadBytes
Copy link
Collaborator

Motivation

See #288 for initial issue.

Using the doc parameter of Api.route() decorator applies documentation to the Resource as a whole. This prevents documentation being added on a per-route basis (for example adding an additional description to a route, or marking one route as deprecated) as the doc is applied to all routes.

Furthermore, each route uses the same Swagger operationId, causing both routes to be expanded at the same time when selecting one in the Swagger UI (the elements are selected using an HTML id which is set to the operationId.

Changes

  • Using the doc parameter Api.route() will apply the documentation to that route only
  • Routes using the doc parameter are given unique operationIds (combining the original operationId with the URLs provided)
  • Each route will inherit documentation from the Resource (i.e. if applied using @api.doc, unless explicitly overridden with the doc parameter

Examples

Providing a description to a single route:

@api.route("/my-resource/<id>")
@api.route(
    "/also-my-resource/<id>",
    doc={"description": "Alias for /my-resource/<id>"},
)

class MyResource(Resource):
    def get(self, id):
        return {}

single_route_1

Expanding the first route only (note the description):
single_route_2

Expanding the second route only (note lack of description):
single_route_3

Inheriting/overriding from the Resource

@api.route('/my-resource/<id>', '/test/<id>')
@api.route('/also-my-resource/<id>', doc={"description": "Alias for /my-resource/<id>"})
@api.doc(params={'id': 'An ID'}, description="My resource")
class MyResource(Resource):
    def get(self, id):
        return {}

image

Expanding the first route only (note id parameter from @api.doc() but overridden description)
image

Expanding the second route only (note id parameter and description inherited from @api.doc() decorator):
image

Various combinations of the above can be used with of the available documentation parameters (description, id, params, deprecated etc).

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 96.571% when pulling f9c1663 on 288-swagger-multiple-routes into e911078 on master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 96.571% when pulling f9c1663 on 288-swagger-multiple-routes into e911078 on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 96.571% when pulling f9c1663 on 288-swagger-multiple-routes into e911078 on master.

@coveralls
Copy link

coveralls commented Jun 26, 2019

Coverage Status

Coverage increased (+0.09%) to 96.914% when pulling 7f64f0b on 288-swagger-multiple-routes into e911078 on master.

@SteadBytes SteadBytes merged commit fe085c8 into master Jul 2, 2019
@ziirish ziirish deleted the 288-swagger-multiple-routes branch July 9, 2019 08:33
@copdips
Copy link

copdips commented Jun 15, 2020

even it's late, but a big thank to you.

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

Successfully merging this pull request may close these issues.

None yet

4 participants