我似乎无法使 swag_from 按预期工作。
这是我的API
from flask import Flask, request, jsonify
from flasgger import swag_from, Swagger
app = Flask(__name__)
swagger = Swagger(app)
@app.post('/calculate')
@swag_from('calculate.yml', validation=True)
def calculate() -> Flask.response_class:
request_data = request.get_json()
flights = request_data['flights']
if not isinstance(flights, collections.Iterable):
return jsonify({ 'message': 'Invalid input, should be a valid list' }), 400
return jsonify({ 'result': 'something here' }), 200
if __name__ == '__main__':
app.run()
这是我的.yml
...etc etc
paths:
/calculate:
post:
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: request
description: request
required: true
schema:
$ref: '#/definitions/GetFlightPath'
responses:
'200':
description: OK
schema:
type: array
items:
$ref: '#/definitions/GetFlightResponse'
'400':
description: Bad Request
'401':
description: Unauthorized
deprecated: false
definitions:
GetFlightResponse:
type: object
properties:
description:
type:
string
cost:
$ref: '#/definitions/Cost'
title: GetFlight response
GetFlightPath:
type: object
properties:
flights:
type: array
items:
type: array
items:
type: string
title: GetFlight Request
当我发布到例如“http://127.0.0.1:8080/计算”
{
"flights": [["SFO", 1]]
}
我预计验证会失败。我认为这是预期的行为,“in: body”参数中的模式用于验证请求。
我做错了什么?
好吧,发布此内容 25 秒后我就明白了...我使用的是 yaml 模式而不是 yml。我的错
tags:
- Calculate Flights
parameters:
- in: body
name: request
description: request
required: true
schema:
$ref: '#/definitions/GetFlightPath'
responses:
'200':
description: OK
schema:
type: array
items:
$ref: '#/definitions/GetFlightResponse'
'400':
description: Bad Request
'401':
description: Unauthorized
definitions:
GetFlightResponse:
type: object
properties:
description:
type:
string
cost:
$ref: '#/definitions/Cost'
title: GetFlight response
GetFlightPath:
type: object
properties:
flights:
type: array
items:
type: array
items:
type: string
title: GetFlight Request