2017-01-12 14 views
3

Flasggerを使用してPython FlaskアプリケーションにSwagger UIを追加しています。インターネット上で最も一般的な例は、@app.routeを使用して、基本的なフラスコのスタイルのためのものである:働くBlueprintsを使用したFlaskアプリケーションでFlasggerを使用するには?

from flasgger.utils import swag_from 

@app.route('/api/<string:username>') 
@swag_from('path/to/external_file.yml') 
def get(username): 
    return jsonify({'username': username}) 

を。

しかし私のアプリケーションでは、@app.routeデコレータを使用してエンドポイントを定義していません。私はフラスコの青写真を使用しています。次のように:上記からわかるように

from flask import Flask, Blueprint 
from flask_restful import Api, Resource 
from flasgger.utils import swag_from 
... 

class TestResourceClass(Resource): 

     @swag_from('docs_test_get.yml', endpoint='test') 
     def get() : 
     print "This is the get method for GET /1.0/myapi/test endpoint" 

app = Flask(__name__) 
my_api_blueprint = Blueprint('my_api', __name__) 
my_api = Api(my_api_blueprint) 

app.register_blueprint(my_api_blueprint, url_prefix='/1.0/myapi/') 

my_api.add_resource(TestResourceClass, '/test/' 
         endpoint='test', 
         methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) 
.... 

は、私は、GETメソッドのエンドポイントにバインドされているTestResourceClass.get()方法に@swag_fromデコレータを使用しました。私はまた、エンドポイント= testが2つの場所で一致しています。

しかし、Swagger UIでは何も表示されません。すべて空白です。 docs_test_get.ymlファイルには、swagger仕様を定義するための有効なyamlマークアップが含まれています。

私には何が欠けていますか?どのようにしてFlasker Swagger UIをFlask Blueprintベースのセットアップで動作させることができますか?

+0

私はこれが実際にあなたの質問に役立つわけではないことを知っていますが、ここではちょうどいいです。私はflassgerを使用しません、私はフラスコスワッガーを使用します:https://github.com/gangverk/flask-swagger私はこの問題を具体的に扱うプルリクエストを作成しました:https://github.com/gangverk/flask- swagger/pull/34私はフラスコ・スワッガーが今青写真をサポートしていることだけを伝えることができます – AArias

+0

ありがとう私は見ていきます。私は代替案を見て、https://github.com/rantav/flask-restful-swaggerを見つけました。これは、BluePrintsとAPI定義のためのリソースを使用しているFlaskアプリケーションのサポートがはるかに優れているようです。 –

答えて

0

swag_from関数は、ファイルパスを解析するために何らかのエラーがあります。doc文字列を使用してget()でapiを定義することができます。 flasggerは、get、postのようなMethodView()メソッドを解析します。

+0

私は、ファイルパスがswag_fromの問題ではないことをテストしました。これは、非青写真スタイルのAPI定義で使用されています。 –

2

は今flasggerが動作しないようですまたは青の印刷スタイルのフラスコの定義(まだ)のための適切なサポートを持っていないhttps://github.com/rochacbruno/flasgger/blob/master/examples/example_blueprint.py

""" 
A test to ensure routes from Blueprints are swagged as expected. 
""" 
from flask import Blueprint, Flask, jsonify 

from flasgger import Swagger 
from flasgger.utils import swag_from 

app = Flask(__name__) 

example_blueprint = Blueprint("example_blueprint", __name__) 


@example_blueprint.route('/usernames/<username>', methods=['GET', 'POST']) 
@swag_from('username_specs.yml', methods=['GET']) 
@swag_from('username_specs.yml', methods=['POST']) 
def usernames(username): 
    return jsonify({'username': username}) 


@example_blueprint.route('/usernames2/<username>', methods=['GET', 'POST']) 
def usernames2(username): 
    """ 
    This is the summary defined in yaml file 
    First line is the summary 
    All following lines until the hyphens is added to description 
    the format of the first lines until 3 hyphens will be not yaml compliant 
    but everything below the 3 hyphens should be. 
    --- 
    tags: 
     - users 
    parameters: 
     - in: path 
     name: username 
     type: string 
     required: true 
    responses: 
     200: 
     description: A single user item 
     schema: 
      id: rec_username 
      properties: 
      username: 
       type: string 
       description: The name of the user 
       default: 'steve-harris' 
    """ 
    return jsonify({'username': username}) 


app.register_blueprint(example_blueprint) 

swag = Swagger(app) 

if __name__ == "__main__": 
    app.run(debug=True) 
+0

@BaummitAugen done! –

関連する問題