我试图通过引用同一个 YAML 文件来记录多个端点,但它不起作用。这不支持吗?还是我做错了什么?
应用程序.py
from flask import Flask, jsonify
from flasgger import Swagger
app = Flask(__name__)
Swagger(app)
@app.route('/colors1/<palette>/')
def colors1(palette):
"""
file: ../colors.yaml
"""
all_colors = {
'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
'rgb': ['red', 'green', 'blue']
}
if palette == 'all':
result = all_colors
else:
result = {palette: all_colors.get(palette)}
return jsonify(result)
@app.route('/colors2/<palette>/')
def colors2(palette):
"""
file: ../colors.yaml
"""
all_colors = {
'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
'rgb': ['red', 'green', 'blue']
}
if palette == 'all':
result = all_colors
else:
result = {palette: all_colors.get(palette)}
return jsonify(result)
颜色.yaml
Multiple endpoint specs
---
paths:
/colors1/:
get:
parameters:
- name: palette
in: path
type: string
enum: ['all', 'rgb', 'cmyk']
required: true
default: all
responses:
200:
description: A list of colors (may be filtered by palette)
schema:
$ref: '#/components/schema/Palette'
examples:
rgb: ['red', 'green', 'blue']
/colors2/:
get:
parameters:
- name: palette
in: path
type: string
enum: ['all', 'rgb', 'cmyk']
required: true
default: all
responses:
200:
description: A list of colors (may be filtered by palette)
schema:
$ref: '#/components/schema/Palette'
examples:
rgb: ['red', 'green', 'blue']
components:
schemas:
Palette:
type: object
properties:
palette_name:
type: array
items:
$ref: '#/components/schema/Color'
Color:
type: string
预期结果
具有完整规范(示例、响应、参数等)的两个端点,就像 README 中的屏幕截图中的规范一样。
实际结果
规格不完整,缺少很多细节: