为什么apispec验证在类似于Python Flask API后端文档示例的格式上失败?

2024-06-01 10:10:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Python/FlaskAPI后端中使用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
        """

Tags: refjsontruegetstringschematyperequired
1条回答
网友
1楼 · 发布于 2024-06-01 10:10:50

我在apispec文档中找到了答案: https://apispec.readthedocs.io/en/latest/using_plugins.html#example-flask-and-marshmallow-plugins 上面写着: 如果API使用基于方法的分派,则过程类似。请注意,该方法不再需要包含在docstring中 这有点误导,因为它不是“不再需要包含”,而是“不能包含”。 因此,正确的文档字符串是:

        """
         -
        description: Activity Get
        responses:
          200:
            description: success
            content:
              application/json:
                schema: ActivitySchema
        security:
          - AccessTokenAuth: []
        tags:
          - user
          - admin
        """

相关问题 更多 >