使用 Swagger 描述多范围参数 [英] Describe a multiple range parameter with Swagger

查看:458
本文介绍了使用 Swagger 描述多范围参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Swagger 2.0 中描述一个定义如下的参数:

  • 参数在区间内取有效值:-20 <参数<-100 <参数<30

  • 参数无效,如果:-10 ≤ 参数 ≤ 0

这意味着它有两个有效间隔,因此要定义两个最大值和最小值.Swagger 规范是否支持这种定义?

解决方案

这无法在 OpenAPI/Swagger 2.0 中描述,但可以在 OpenAPI 3.x 中使用 oneOf 进行描述.

OpenAPI 3.0

类型:整数其中之一:- 最低:-20最大值:-10唯一最小值:真独家最大值:真- 最低:0最多:30唯一最小值:真独家最大值:真

OpenAPI 3.1

类型:整数其中之一:- 独家最低:-20独家最大:-10- 独家最低:0独家最大:30

exclusiveM* 关键字是 在 JSON Schema Draft 6 中从布尔值更改为数字.OAS 3.1 默认使用 JSON Schema 2020-12.>

I want to describe in Swagger 2.0 a parameter defined as follows :

  • The parameter takes a valid value in the intervals : -20 < parameter < -10 or 0 < parameter < 30

  • The parameter is invalid if : -10 ≤ parameter ≤ 0

This means that it has two valid intervals and thus two max and mins values to define. Does Swagger specification support that kind of definitions?

解决方案

This cannot be described in OpenAPI/Swagger 2.0, but can be described in OpenAPI 3.x using oneOf.

OpenAPI 3.0

type: integer
oneOf:
  - minimum: -20
    maximum: -10
    exclusiveMinimum: true
    exclusiveMaximum: true
  - minimum: 0
    maximum: 30
    exclusiveMinimum: true
    exclusiveMaximum: true

OpenAPI 3.1

type: integer
oneOf:
  - exclusiveMinimum: -20
    exclusiveMaximum: -10
  - exclusiveMinimum: 0
    exclusiveMaximum: 30

exclusiveM* keywords were changed from boolean to numbers in JSON Schema Draft 6. OAS 3.1 uses JSON Schema 2020-12 by default.

这篇关于使用 Swagger 描述多范围参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆