getsling/flask-swagger

Are Blueprint supported ?

Opened this issue · 9 comments

Hi,

I have a Flask app using Blueprints and would like to document the API using swagger. I love the idea of having a single source of truth when it comes to the API documentation and hence would like to use flask-swagger.
Does it support Blueprint ?
I must admit I didn't even try, I was hoping to first get some tips before messing around with my docstrings everywhere....

thanks

Brieuc

Sorry, don't use Blueprints so I don't know... If Blueprints end up resolving to views then probably yes.

Yes. When calling swagger(), you can use the prefix arg and set it to the same you set url_prefix when defining your blueprint to limit the spec to said blueprint.

e.g:
In your __init__.py:

def create_app(config_name):
    app = Flask(__name__)
    (...)
    from .api_2_0 import api as api_2_0_blueprint
    app.register_blueprint(api_2_0_blueprint, url_prefix='/api/v2.0')

In some other file:

@api.route("/spec")
def spec():
    swag = swagger(current_app, prefix='/api/v2.0')
    (...)
    return jsonify(swag)

Awesome! Thanks. I'll leave the ticket open to remind me to update the docs

I don't see support for the 'prefix' keyword arg in the 'swagger' function. Has it been removed?

It was never there. Possibly @alejoar has a fork that added this?

Anyways, if all you want to do is prefix the paths I think it would be better to update basePath in the swagger response.
For example:

@app.route('/spec.json')
def spec():
    swaggered = swagger(app)
    swaggered['basePath'] = "/v1"  
    return jsonify(swaggered)

Great suggestion, these are my first Swagger experiments, so your comment is quite helpful, tx!

Any update on blueprint document update thing ? waiting for it.

@atlithorn @jjmurre
Sorry for the late reply. @atlithorn you are absolutely right, I'm using a fork I made myself and i forgot about it.

All I did is a very simple modification you can see here. Basically, if the prefix argument is present, it ignores all other url rules:

for rule in app.url_map.iter_rules():
    if prefix and rule.rule[:len(prefix)] != prefix:
        continue

I can create a pull request with this modification if there's interest. I already made another pull request (for a different feature) but it has been ignored for now.

Edit: Ended up submitting a new pull request.

Thanks @alejoar, merged. I'll try to get a release out asap