Flask API があり、各エンドポイントに OpenAPI3 docstring をコメントしました。Flasgger の自動検証を使用したいのですが、requestBody で定義したスキーマに対して要求を検証したいことを Flasgger に伝える方法がわかりません。これが私が試したことです:
api = Blueprint('api', __name__,
template_folder='templates')
@api.route("/apikey", methods=["POST"])
@swag.validate('apikeyrequest')
def generate_api_key():
"""Generate API key for user.
This endpoint generates an API key for a user if provided with a valid password and username
---
requestBody:
content:
'application/json':
schema:
id: apikeyrequest
type: object
required:
- username
- password
properties:
username:
type: string
example: "bob_andrews"
description: The username of the user for which an API key is created.
password:
type: string
example: "$k2%nvpdHS26!vEt"
description: The users password
twofa_token:
type: string
example: "425648"
description: A one time password if the user has 2 factor authentication enabled.
security: []
responses:
200:
description: An API key for this user. The API key is stored as a hash, so it is only shown once.
content:
'application/json':
schema:
type: object
properties:
api_key:
type: string
description: 'The API key for the specified user.'
401:
description: Unauthorized, either due to wrong credentials or a missing two factor token if the user has
two factor authentication enabled.
content:
'application/json':
schema:
type: object
properties:
error:
type: string
description: A string describing which part failed and (possibly) why.
500:
description: Server error, the error json will contain more details.
content:
'application/json':
schema:
type: object
properties:
error:
type: string
description: A string describing which part failed and (possibly) why.
"""
Swagger UI ページを見たときに表示されるエラー メッセージは次のとおりです。
Resolver error at paths./api/apikey.post.requestBody.content.application/json.schema.$ref
Could not resolve reference: Could not resolve pointer: /definitions/apikeyrequest does not exist in document
実際、flasgger によって生成された apispec json では、定義オブジェクトは空です。検証したいスキーマは、代わりに にありますpaths/api/apikey/post/requestBody
。