如何在OpenAPI中至少要求两个参数之一? [英] How to require at least one of two parameters in OpenAPI?
问题描述
我使用的是OpenAPI 3,它有两个查询参数,至少需要一个 ,但这无关紧要.
I'm using OpenAPI 3 and have two query parameters, at least one of which is required but it doesn't matter which.
即,作为sudocode:
I.e., as sudocode:
if parameter_1 OR parameter_2:
do stuff
else:
error
在OpenAPI 3中有可能吗?据我所知,在规范中或JSON Schema规范中都没有提及它.
Is this possible in OpenAPI 3? There's no mention of it in the spec as far as I can see, or the JSON Schema spec either.
推荐答案
此方案与互斥参数非常相似.基本上,您可以使用对象类型参数,其中parameter_1
和parameter_2
是对象属性.此类对象将被序列化为?parameter_1=value1¶meter_2=value2
.可以使用minProperties
或anyOf
表示至少一个"约束.
This scenario is very similar to mutually exclusive parameters. Basically, you can use an object-type parameter where parameter_1
and parameter_2
are the object properties; such object will be serialized as ?parameter_1=value1¶meter_2=value2
. The "at least one of" constraint can be represented using minProperties
or anyOf
.
openapi: 3.0.2
...
paths:
/foo:
get:
parameters:
- in: query
name: param
required: true
schema:
type: object
properties:
parameter_1:
type: string
parameter_2:
type: string
additionalProperties: false
# Option 1
minProperties: 1
# Option 2
# anyOf:
# - required: [parameter_1]
# - required: [parameter_2]
这篇关于如何在OpenAPI中至少要求两个参数之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!