Skip to content

Instantly share code, notes, and snippets.

@zeakd
Created December 19, 2018 08:00
Show Gist options
  • Save zeakd/04b5c3af79e492779985fd883d48f123 to your computer and use it in GitHub Desktop.
Save zeakd/04b5c3af79e492779985fd883d48f123 to your computer and use it in GitHub Desktop.
simple apispec, flask, marshmallow integrations
from apispec import APISpec
from apispec.ext.flask import FlaskPlugin
from apispec.ext.marshmallow import MarshmallowPlugin
from flask import jsonify
import marshmallow
spec = APISpec(
title='ttang me',
version='1.0.0',
openapi_version='2.0',
plugins=[
FlaskPlugin(),
MarshmallowPlugin(),
],
)
def init_app(app):
try:
# register paths
with app.test_request_context():
for name, func in app.view_functions.items():
spec.add_path(view=func)
# register only schemas with __swagger_name__ parameter
for name, schemas in marshmallow.class_registry._registry.items():
for schema in schemas:
swagger_name = getattr(schema, '__swagger_name__', None)
if swagger_name:
spec.definition(swagger_name, schema=schema)
# routes swagger json
@app.route('/swagger.json')
def get_swagger_json():
return jsonify(spec.to_dict())
except Exception as e:
# error is not visible on development
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment