getsling / flask-swagger

A swagger 2.0 spec extractor for flask
MIT License
457 stars 91 forks source link

Are Blueprint supported ? #33

Closed ghost closed 8 years ago

ghost commented 8 years ago

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

atlithorn commented 8 years ago

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

alejoar commented 8 years ago

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)
atlithorn commented 8 years ago

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

jjmurre commented 8 years ago

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

atlithorn commented 8 years ago

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)
jjmurre commented 8 years ago

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

PradeepJaiswar commented 8 years ago

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

alejoar commented 8 years ago

@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.

atlithorn commented 8 years ago

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