openApi 模式中不区分大小写的字符串参数

frd*_*rdr 10 enums openapi

我有一个带有如下参数的开放 API 规范: 

- name: platform
  in: query
  description: "Platform of the application"
  required: true
  schema:
    type: string
    enum:
      - "desktop"
      - "online"
Run Code Online (Sandbox Code Playgroud)

当我从 URL 获取“平台”参数时,它可以是这样的:

platform=online or 
platform=ONLINE or 
platform=Online or 
platform=onLine  or ... any other format
Run Code Online (Sandbox Code Playgroud)

但是当我要使用它时,它仅在参数全部为小写时才有效,例如"platform=online",显然要匹配枚举值。

如何使模式不区分大小写并理解所有类型的传递参数?

Hel*_*len 7

枚举区分大小写。要拥有不区分大小写的架构,您可以改用正则表达式pattern

- name: platform
  in: query
  description: 'Platform of the application. Possible values: `desktop` or `online` (case-insensitive)'
  required: true
  schema:
    type: string
    pattern: '^[Dd][Ee][Ss][Kk][Tt][Oo][Pp]|[Oo][Nn][Ll][Ii][Nn][Ee]$'
Run Code Online (Sandbox Code Playgroud)

请注意,这pattern是模式本身,不支持 JavaScript 正则表达式文字语法 ( /abc/i),这意味着您不能指定诸如(不区分大小写的搜索)之类的标志i。因此,您需要在模式本身中同时指定大写和小写字母。


或者,descriptionpattern/ 中而不是在/ 中指定可能的值enum,并在后端验证参数值。