首页 > 解决方案 > OpenAPI 规范 - 如何指定接受一系列值的输入参数

问题描述

使用 OpenAPI 3.0.3,我正在定义一个接受两个输入查询参数的 API 规范。

- name: land_area_llimit
  in: query
  description: Lower limit for land area comparison
  required: false
  schema:
      type: integer
- name: land_area_ulimit
  in: query
  description: Upper limit for land area comparison
  required: false
  schema:
      type: integer

理想情况下,我想将两者结合起来,并且只有一个参数,它接受一个范围,比如: [a,b] where a > 0 and b > a > 0。说,类似:

- name: land_area
  in: query
  description: lower and upper bounds for land area comparison
  required: false
  schema:
      type: range     
  ## With some way to specify that this parameter accepts a lower bound and an upper bound. 

我知道minimumand maximum。这将预设范围。我正在寻找作为输入提供的范围。这可以实现吗?

标签: openapispecifications

解决方案


我最终使用了一种object输入类型:

- name: land_area
  in: query
  description: Land area ranges for comparison (in sqft). lower_bound < upper_bound. Return 400 otherwise.
  required: false
  schema:
    type: object
      properties:
        land_area_lower_bound:
          type: integer
        land_area_upper_bound:
          type: integer

签入 Swagger UI,请求 URL 将解析为:

http://<url>/<api>?land_area_lower_bound=1234&land_area_upper_bound=3456

推荐阅读