Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying multiple types for additionalProperties through Swagger/OpenAPI

I am looking to represent the following JSON Object in OpenAPI:

{
  "name": "Bob",
  "age": 4,
  ...
}

The number of properties and the property names are not fully predetermined, so I look to use additionalProperties. However, I'm not too certain how it would be represented through OpenAPI/Swagger 2.0. I tried this:

Person:
  type: object
  additionalProperties:
    type:
      - int
      - string

or the JSON equivalent:

{
  "Person": {
    "type": "object",
    "additionalProperties": {
      "type": ["int", "string"]
    }
  }
}

but that didn't quite work. Is there any way to keep the structure of the JSON Object I want to represent, for specifically strings and integers, and not arbitrary object types?

like image 811
sle Avatar asked Sep 28 '17 15:09

sle


People also ask

How do you declare an array of strings in Swagger?

Firstly, we start by specifying the array of strings in Swagger using YAML notation. In the schema section, we include type: array with items String.

How do you add multiple hosts on Swagger?

Multiple hosts are supported in OpenAPI 3.0. 2.0 supports only one host per API specification (or two if you count HTTP and HTTPS as different hosts). A possible way to target multiple hosts is to omit the host and schemes from your specification and serve it from each host.


1 Answers

OpenAPI 3.1

In OpenAPI 3.1, the type keyword can take a list of types:

Person:
  type: object
  additionalProperties:
    type: [string, integer]

OpenAPI 3.x

OpenAPI 3.0+ supports oneOf so you can use:

Person:
  type: object
  additionalProperties:
    oneOf:
      - type: string
      - type: integer

OpenAPI 2.0

OpenAPI 2.0 does not support multi-type values. The most you can do is use the typeless schema, which means the additional properties can be anything - strings, numbers, booleans, and so on - but you can't specify the exact types.

Person:
  type: object
  additionalProperties: {}

This is equivalent to:

Person:
  type: object
like image 158
Helen Avatar answered Nov 08 '22 00:11

Helen