使用oneOf打开API 3和Flask应用程序响应验证问题

时间:2018-12-03 11:12:08

标签: python-3.x validation flask swagger openapi

我有一个带有Open API 3的简单Flask应用程序。我已经通过这种方式定义了yml文件,使用oneOf进行了200种不同的响应

paths:
  /document:
    post:
      operationId: document.classify
      tags:
        - document
      summary: Classify a document and return the belonging cluster
      description: Assign a class to the input document
      requestBody:
        content:
          application/json:
            schema:
              ...
        description: Document to classify
        required: true
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Output'
                  - $ref: '#/components/schemas/Fault'
components:
  schemas:
    Output:
      type: object
      properties:
        dID:
          type: string
        ClassificationConcepts:
          type: array
          items:
            properties:
              Concept:
                type: array
                description: Comma separated classification tags
                items:
                  type: string
    Fault:
      type: object
      properties:
        faultCode:
          type: string
          description: Fault code number
        faultMessage:
          type: string
          description: Fault code message

但是验证步骤未通过输出,错误为“验证模式中的'oneOf'失败”在实例上:{'ClassificationConcepts':[{'Concept':['Test']}]],'dID' :'test'}。

'oneOf': [{
    'properties': {
      'ClassificationConcepts': {
        'items': {
          'properties': {
            'Concept': {
              'description': 'Comma '
              'separated '
              'classification '
              'tags',
              'items': {
                'type': 'string'
              },
              'type': 'array'
            }
          }
        },
        'type': 'array'
      },
      'dID': {
        'type': 'string'
      }
    },
    'type': 'object',
    'x-scope': ['']
  }
  ...
]
}

这是我的Python代码:

dID = body.get("dID")
    try:
        class_predicted = classifier.classification_doc(document_text)
        result = {"dID": dID,
                "ClassificationConcepts": [
                    {"Concept": class_predicted}
                ]
                }
        response = jsonify(result)
        response.headers.set("Content-Type", "application/json")
        return response

    except ValueError as e:
        result = {"faultCode": 1, "faultMessage": e.__cause__ }
        return make_response(
            jsonify(result)
        )

0 个答案:

没有答案