查询参数阵列中的数字索引是否在Swagger 2.0中表示?

发布于 2025-02-08 04:40:40 字数 165 浏览 3 评论 0原文

我了解如何用Swagger 2.0表示查询参数中的数组:

?list=x&list=y

是否可以使用索引样式在Swagger 2.0中表示数组?

?list[0]=x&list[1]=y

I understand how to represent the array in query parameters with Swagger 2.0:

?list=x&list=y

Is it possible to represent an array in Swagger 2.0 using an indexed style?

?list[0]=x&list[1]=y

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2025-02-15 04:40:40

OpenAPI 2.0(FKA Swagger 2.0)

list [0]list [1]list> list [2] ... ... list [list [list] [ n]必须定义为单独的查询参数。无法将list定义为单个数组类型参数,并以您想要的方式序列化。

请注意,此方法仅适用于提前已知最大尺寸的固定尺寸阵列。

parameters:
  - in: query
    name: list[0]
    type: string
  - in: query
    name: list[1]
    type: string

OpenAPI 3.0

一种方法是定义list [0]list [1]等作为单独的查询参数(例如,在OpenAPI 2.0示例中)。此方法适用于预先已知最大尺寸的固定尺寸阵列。

parameters:
  - in: query
    name: list[0]
    schema:
      type: string
  - in: query
    name: list[1]
    schema:
      type: string

另一种适用于未知大小数组的方法是使用 free -form查询参数。这种方法的限制在于,它允许任意参数名称 - 不仅是list [n],还abcde。但是,您可以提供描述解释参数名称必须以格式list [n],并且提供示例>示例值值文档目的。

parameters:
  - in: query
    name: params  # arbitrary name, not used in the actual query string
    description: >-
      Parameter names must be in the format `list[n]`
      where `n` is an integer starting from 0.
    schema:
      type: object
    example:
      list[0]: x
      list[1]: y
      list[2]: z

在Swagger UI中,在JSON对象(非数组)格式中输入查询参数,例如:

{
  "list[0]": "x",
  "list[1]": "y",
  "list[2]": "z"
}

Swagger UI会将其转换为以下查询字符串(在URL编码之前):

?list[0]=x&list[1]=y&list[2]=z

OpenAPI 3.1

OAS 3.1将我们向前迈进了一步。您仍然会使用 free-form查询方法,但使用新的 pattern> pattern> pattern> pattern properties 关键字,以指定一个针对正则表达式的关键字允许的参数名称(例如^list \ [\ d+\] $)。

# openapi: 3.1.0

parameters:
  - in: query
    name: params  # arbitrary name, not used in the actual query string
    description: >-
      Parameter names must be in the format `list[n]`
      where `n` is an integer starting from 0.
    schema:
      type: object
      patternProperties:   # <--------
        ^list\[\d+\]$:
          type: string
      additionalProperties: false
    example:
      list[0]: x
      list[1]: y
      list[2]: z

OpenAPI 2.0 (fka Swagger 2.0)

list[0], list[1], list[2] ... list[N] must be defined as separate query parameters. There's no way to define list as a single array-type parameter and have it serialized the way you want.

Note that this approach only works for fixed-sized arrays whose maximum size is known in advance.

parameters:
  - in: query
    name: list[0]
    type: string
  - in: query
    name: list[1]
    type: string

OpenAPI 3.0

One approach is to define list[0], list[1], etc. as separate query parameters (like in the OpenAPI 2.0 example above). This approach is suitable for fixed-sized arrays whose maximum size is known in advance.

parameters:
  - in: query
    name: list[0]
    schema:
      type: string
  - in: query
    name: list[1]
    schema:
      type: string

Another approach - suitable for arrays of unknown size - is to define such an array using free-form query parameters. The limitation of this approach is that it allows arbitrary parameter names - not just list[n] but also abcde. However, you can provide a description explaining that the parameter names must be in the format list[n], and also provide an example value for documentation purposes.

parameters:
  - in: query
    name: params  # arbitrary name, not used in the actual query string
    description: >-
      Parameter names must be in the format `list[n]`
      where `n` is an integer starting from 0.
    schema:
      type: object
    example:
      list[0]: x
      list[1]: y
      list[2]: z

In Swagger UI, enter the query parameters in the JSON object (not array) format, like so:

{
  "list[0]": "x",
  "list[1]": "y",
  "list[2]": "z"
}

Swagger UI will convert this into the following query string (before URL encoding):

?list[0]=x&list[1]=y&list[2]=z

OpenAPI 3.1

OAS 3.1 takes us one step forward. You would still use the free-form query approach but narrow it down by using the new patternProperties keyword to specify a regular expression for the allowed parameter names (e.g. ^list\[\d+\]$).

# openapi: 3.1.0

parameters:
  - in: query
    name: params  # arbitrary name, not used in the actual query string
    description: >-
      Parameter names must be in the format `list[n]`
      where `n` is an integer starting from 0.
    schema:
      type: object
      patternProperties:   # <--------
        ^list\[\d+\]$:
          type: string
      additionalProperties: false
    example:
      list[0]: x
      list[1]: y
      list[2]: z
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文