如何在OpenAPI 3.0中用两个可选参数定义路径?

问题描述 投票:0回答:1

我在SwaggerHub注册并使用OpenAPI 3.0创建了新的API。在我的API中,/tasks路径具有2个非必需参数,但我无法将其设置为非必需参数-编辑器显示“不允许的值”错误。

这是我的API定义:

openapi: 3.0.0
info:
  description: A Simple IP Address API
  title: VTasks
  version: v1
servers:
# Added by API Auto Mocking Plugin
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/petrogromovo/Vtasks/1.0.0
  - description: SwaggerHub API Auto Mocking
    url: http://hosting.tk

paths:

  /tasks:
    get:
      tags:
        - tasks
      summary: Get paginated / filtered tasks listing
      operationId: tasks
      parameters:
        - name: page
          in: path
          description: The page number to be fetched. If missed default 1.
          required: true
          schema:
            type: integer
        - name: order_by
          in: path
          description: The order_by  be fetched. 
          required: false  // ERROR : should be equal to one of the allowed values allowedValues: true
          schema:
            type: string
        - name: filter
          in: path
          description: The filter for title field. 
          required: false // ERROR : should be equal to one of the allowed values allowedValues: true
          schema:
            type: string
      responses:
        '200':
          description: successful operation
        '400':
          description: Invalid tasks supplied
        '404':
          description: tasks were not found

但是如果删除required属性,则会出现2个错误:

[应具有必需的属性”missingProperty:必填

有效语法是什么?

openapi swagger-editor
1个回答
0
投票

这些参数应该是路径参数还是查询参数?

路径参数in: path)是端点路径的一部分,因此必须在路径模板中由{...}表示:

paths:
  /tasks/{page}/{order_by}/{filter}:

始终需要路径参数,即它们必须具有required: true

Query parameters在查询字符串中发送,例如/tasks?page=...&order_by=...。要使用查询参数,请将参数位置更改为in: query

© www.soinside.com 2019 - 2024. All rights reserved.