Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive String parameter in schema of openApi

Tags:

enums

openapi

I have an open API spec with a parameter like this: 

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

when I get the "platform" parameter from URL , it can be like this :

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

but when I am going to use it , it is only valid if the parameter is all lower case like "platform=online", obviously to match the enum value.

how can I make schema to be the case insensitive and understand all types of passed parameters?

like image 924
frdsprtrdr Avatar asked Mar 20 '20 10:03

frdsprtrdr


People also ask

Is OpenAPI case sensitive?

This includes all fields that are used as keys in a map, except where explicitly noted that keys are case insensitive. The schema exposes two types of fields: Fixed fields, which have a declared name, and Patterned fields, which declare a regex pattern for the field name.

What are OpenAPI parameters?

In OpenAPI 3.0, parameters are defined in the parameters section of an operation or path. To describe a parameter, you specify its name , location ( in ), data type (defined by either schema or content ) and other attributes, such as description or required . Here is an example: paths: /users/{userId}:

Does OpenAPI use JSON schema?

OpenAPI 3.0 uses an extended subset of JSON Schema Specification Wright Draft 00 (aka Draft 5) to describe the data formats. “Extended subset” means that some keywords are supported and some are not, some keywords have slightly different usage than in JSON Schema, and additional keywords are introduced.


1 Answers

Enums are case-sensitive. To have a case-insensitive schema, you can use a regular expression pattern instead:

- 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]$'

Note that pattern is the pattern itself and does not support JavaScript regex literal syntax (/abc/i), which means you cannot specify flags like i (case insensitive search). As a result you need to specify both uppercase and lowercase letters in the pattern itself.


Alternatively, specify the possible values in the description rather than in pattern/enum, and verify the parameter values on the back end.

like image 61
Helen Avatar answered Sep 20 '22 16:09

Helen