我在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:必填
有效的语法是什么?
答案 0 :(得分:2)
这些参数应该是路径参数还是查询参数?
路径参数(in: path
)是端点路径的一部分,因此必须在路径模板中用{...}
表示:
paths:
/tasks/{page}/{order_by}/{filter}:
始终需要路径参数,即它们必须具有required: true
。
查询参数是在查询字符串中发送的,例如/tasks?page=...&order_by=...
。要使用查询参数,请将参数位置更改为in: query
。