OpenApi的架构中的字符串参数不区分大小写 [英] Case Insensitive String parameter in schema of openApi

查看:14
本文介绍了OpenApi的架构中的字符串参数不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个开放的API规范,其参数如下所示:@

- name: platform
  in: query
  description: "Platform of the application"
  required: true
  schema:
    type: string
    enum:
      - "desktop"
      - "online"

当我从URL中获取"Platform"参数时,它可以是这样的:

platform=online or 
platform=ONLINE or 
platform=Online or 
platform=onLine  or ... any other format

但当我要使用它时,它只有在参数都是小写的情况下才有效,如"Platform=Online",显然是为了匹配枚举值。

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

推荐答案

枚举区分大小写。要使用不区分大小写的架构,您可以使用正则表达式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]$'
请注意,pattern是模式本身,不支持JavaScript regex文字语法(/abc/i),这意味着您不能指定flagsLIKEi(不区分大小写搜索)。因此,您需要在模式本身中同时指定大写和小写字母。


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

这篇关于OpenApi的架构中的字符串参数不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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