-1

I'm trying to describe the following post parameter in swagger:

"AnyOne" = [["971", 50.0, 70.0], ["832", 50.0, 30.0], ["134A", 50.0, 100.0]]

Helen
  • 87,344
  • 17
  • 243
  • 314
  • I guess technically it might be possible, but how do you plan to handle that parameter on java side? A list accepting both `String` and `int`? Why not make it an array of objects instead? – Deltharis Feb 15 '22 at 12:40
  • the problem is that I don't know how to write it correctly. Please can you help me by writing an example? – Ahlam Mohamed Feb 15 '22 at 12:45
  • @AhlamMohamed can you please post what you've tried so far? – Helen Feb 15 '22 at 12:49
  • I diff it like this , but my boss told me it is wrong and I now i have to figure out how. AnyOne: type: array items: type: string – Ahlam Mohamed Feb 15 '22 at 13:10
  • AnyOne: type: array items: type: object properties: one: type: string description: Descrizione hello: type: number description: Valore del per Garanzia format: double hello2: type: number description: – Ahlam Mohamed Feb 15 '22 at 13:51
  • Related: [How to define a nested array in OpenAPI?](https://stackoverflow.com/q/70856802/113116) – Helen Feb 15 '22 at 14:56

1 Answers1

0

OpenAPI 3.1

Your nested array can be considered an array of tuples. Tuples are defined using prefixItems. An array of tuples is defined as follows:

# openapi: 3.1.0

type: array
items:
  type: array
  prefixItems:
    # The 1st item
    - type: string
      description: Description of the 1st item
      example: "971"
    # The 2nd item
    - type: number
      description: Description of the 2nd item
      example: 50.0
    # The 2nd item
    - type: number
      description: Description of the 3nd item
      example: 70.0

  # The total number of items in this tuple
  minItems: 3
  maxItems: 3
  additionalItems: false   # can be omitted if `maxItems` is specified

OpenAPI 3.0.x

The inner array can be defined as a generic mixed-type array, i.e. "can contain strings and numbers". But you cannot specifically define that the 1st element is a string and the rest are numbers. This gives us:

type: array
items:
  type: array
  items:
    oneOf:
      - type: string
      - type: number
  example: ["971", 50.0, 70.0]

OpenAPI 2.0

This version does not really support mixed-type arrays like ["971", 50.0, 70.0]. The most you can do is to use a typeless schema {} for the items of the inner array, which means the inner array can contain any values except nulls. You cannot specify the exact types for items, but you can add an example of an array with different item types.


# swagger: '2.0'

type: array
items:
  type: array
  items: {}
  example: ["971", 50.0, 70.0]
Helen
  • 87,344
  • 17
  • 243
  • 314