昂首阔步:从枚举中获取一个或多个值 [英] Swagger: take one or more values from enum

查看:139
本文介绍了昂首阔步:从枚举中获取一个或多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个摇摇欲坠的文件,其中查询参数不能接受任何值或n个值.我该如何在Swagger Yaml中写这个?

I am working on a swagger file, where a query parameter can take none, or n values. How can I write this in Swagger Yaml?

我提供的网址:/sort=field1,field2

在swagger文件中声明的参数

The parameter declared in swagger file

- name: sort
  in: query
  schema:
    type: string
    enum: [field1,field2,field3]
  allowEmptyValue: true
  required: false
  description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)

白天/夜晚都很好.

推荐答案

包含逗号分隔的值列表的查询参数被定义为数组.如果值是预定义的,则它是枚举数组.

A query parameter containing a comma-separated list of values is defined as an array. If the values are predefined, then it's an array of enum.

默认情况下,数组可以具有任意数量的项目,这些项目符合您的无或更多"要求.如果需要,可以使用minItemsmaxItems限制项目的数量,并可以选择强制执行uniqueItems: true.

By default, an array may have any number of items, which matches your "none or more" requirement. If needed, you can restrict the number of items using minItems and maxItems, and optionally enforce uniqueItems: true.

参数定义如下所示. collectionFormat: csv表示值以逗号分隔,但这是默认格式,因此可以省略.

The parameter definition would look as follows. collectionFormat: csv indicates that the values are comma-separated, but this is the default format so it can be omitted.

      parameters:
        - name: sort
          in: query
          type: array  # <-----
          items:
            type: string
            enum: [field1, field2, field3]
          collectionFormat: csv  # <-----
          required: false
          description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)

OpenAPI 3.0

OpenAPI 2.0中的

collectionFormat: csv已被style: form + explode: false取代. style: form是查询参数的默认样式,因此可以省略.

OpenAPI 3.0

collectionFormat: csv from OpenAPI 2.0 has been replaced with style: form + explode: false. style: form is the default style for query parameters, so it can be omitted.

      parameters:
        - name: sort
          in: query
          schema:
            type: array  # <-----
            items:
              type: string
              enum: [field1, field2, field3]
          required: false
          description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)
          explode: false  # <-----

我认为不需要allowEmptyValue,因为在这种情况下,空数组实际上将是一个空值.此外,allowEmptyValue不推荐,因为自OpenAPI起使用3.0.2因为它将在将来的版本中删除."

I think there's no need for allowEmptyValue, because an empty array will be effectively an empty value in this scenario. Moreover, allowEmptyValue is not recommended for use since OpenAPI 3.0.2 "as it will be removed in a future version."

这篇关于昂首阔步:从枚举中获取一个或多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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