python - 为什么 apispec 验证在类似于 Python Flask API 后端的文档示例的格式上失败?
问题描述
我在 Python/Flask API 后端使用 apispec。我遵循了 apispec 文档示例中的格式。请参阅:https ://apispec.readthedocs.io/en/latest/ 谁能告诉我为什么我收到以下 json 架构和数据的验证错误?它说“响应”是必需的,但看起来它就在那里。结构不正确?任何帮助表示赞赏!
openapi_spec_validator.exceptions.OpenAPIValidationError: 'responses' is a required propertyFailed validating 'required' in schema['properties']['paths']['patternProperties']['^/']['properties']['get']:
{'additionalProperties': False,
'description': 'Describes a single API operation on a path.',
'patternProperties': {'^x-': {'$ref': '#/definitions/specificationExtension'}},
'properties': {'callbacks': {'$ref': '#/definitions/callbacksOrReferences'},
'deprecated': {'type': 'boolean'},
'description': {'type': 'string'},
'externalDocs': {'$ref': '#/definitions/externalDocs'},
'operationId': {'type': 'string'},
'parameters': {'items': {'$ref': '#/definitions/parameterOrReference'},
'type': 'array',
'uniqueItems': True},
'requestBody': {'$ref': '#/definitions/requestBodyOrReference'},
'responses': {'$ref': '#/definitions/responses'},
'security': {'items': {'$ref': '#/definitions/securityRequirement'},
'type': 'array',
'uniqueItems': True},
'servers': {'items': {'$ref': '#/definitions/server'},
'type': 'array',
'uniqueItems': True},
'summary': {'type': 'string'},
'tags': {'items': {'type': 'string'},
'type': 'array',
'uniqueItems': True}},
'required': ['responses'],
'type': 'object'}
On instance['paths']['/v1/activity']['get']:
{'get': {'description': 'Activity Get',
'responses': {'200': {'content': {'application/json': {'schema': 'ActivitySchema'}},
'description': 'success'}},
'security': [{'AccessTokenAuth': []}],
'tags': ['user', 'admin']}}
For reference, here is the original source comment that the data comes from:
"""
---
get:
description: Activity Get
responses:
200:
description: success
content:
application/json:
schema: ActivitySchema
security:
- AccessTokenAuth: []
tags:
- user
- admin
"""
解决方案
我在 apispec 文档中找到了答案: https ://apispec.readthedocs.io/en/latest/using_plugins.html#example-flask-and-marshmallow-plugins 其中说:“如果您的 API 使用基于方法的调度,过程类似。注意该方法不再需要包含在文档字符串中。这有点误导,因为它不是“不再需要包含”,而是“不能包含”。所以正确的文档字符串是:
"""
---
description: Activity Get
responses:
200:
description: success
content:
application/json:
schema: ActivitySchema
security:
- AccessTokenAuth: []
tags:
- user
- admin
"""
推荐阅读
- c# - 检查数据库中是否存在数字,如果不将数字分配给数据库对象?
- c# - C# GroupBy 两个键面临麻烦
- python - “。”之间的区别 和“./”和“./.” 对于路径
- python - ImportError:无法从“数据”(未知位置)导入名称“voc”
- javascript - 如何计算前导字符并用 替换它们?
- node.js - Runtime.ImportModuleError - 尝试导入本地实用程序文件时在 lambda 服务器上出现错误
- javascript - 下拉菜单:如果有下拉菜单=值,则显示 div
- sql-server - 将文件从文件表 unc 路径复制到文件服务器时访问被拒绝
- angular - 你如何在 TypeScript 中使用泛型的某些部分?
- c# - C# Lamba 表达式选择元素