【问题标题】:swagger validadtion error for list in pythonpython中列表的招摇验证错误
【发布时间】:2019-06-11 20:47:53
【问题描述】:

我正在开发一个带有flask和swagger的python API,我想将其中一个输入从字符串更改为列表:

这是架构的当前工作代码:

   /question:
    post:
      operationId: processor.convertInputString
      tags:
        - People
      summary: Create a person and add it to the people list
      description: Create a new person in the people list
      parameters:
        - name: input_string_2
          in: body
          description: Person to create
          required: True
          schema:
            type: object
            properties:
              question:
                type: list
                description: question to match

      responses:
        201:
          description: Successfully created person in list

以及我正在使用的请求:

data =  {"question": "processor","num_results":3}
headers = {'content-type': 'application/json'}
url = "http://localhost:5000/api/question"
data = requests.post(url,data=json.dumps(data), headers=headers)

这很好用,但我需要将 {"question": "processor"} 更改为:

{"question": ["processor"]}

但是当我提出这个请求时,我得到了这个错误:

'{\n  "detail": "[\'processor\'] is not of type \'string\'",\n  "status": 400,\n  "title": "Bad Request",\n  "type": "about:blank"\n}\n'

因此我尝试将模式中的数据类型从字符串更改为列表:

  schema:
    type: object
    properties:
      question:
        type: list
        description: question to match

但是我得到了另一个错误。

Failed validating 'oneOf' in schema['properties']['paths']['patternProperties']['^/']['properties']['post']['properties']['parameters']['items']:
    {'oneOf': [{'$ref': '#/definitions/parameter'},
               {'$ref': '#/definitions/jsonReference'}]}

On instance['paths']['/question']['post']['parameters'][0]:
    {'description': 'Person to create',
     'in': 'body',
     'name': 'input_string_2',
     'required': True,
     'schema': {'properties': {'question': {'description': 'question to '
                                                           'match',
                                            'type': 'list'}},
                'type': 'object'}}

【问题讨论】:

    标签: python api flask swagger


    【解决方案1】:

    字符串数组/列表定义为

    question:
      type: array
      items:
        type: string
    

    type: list 在 OpenAPI 中不是 type 的有效值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-30
      • 2023-03-10
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多