首页 > 解决方案 > 为什么 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
        """

标签: pythonopenapiflask-apispec

解决方案


我在 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
        """

推荐阅读