Forum Discussion

lnash94's avatar
lnash94
Occasional Contributor
3 years ago

How to document multiple data types array in OpenAPI Definition

Hi all,

I'm looking for an OpenAPI representation for an array type when the array has one specific data type item at one time, the array data type can be multiple types. That means as an example: 

let's assume I have an array called `arry` with two data types integer and string, when I receive `arry`, It can be contained items only one type. It can be an integer array(arry[] -> [integer, integer, integer]) or string array (arry[]-> [string, string, string]).

 

If I use oneOf data model :

....

arry:

  type: array

  items:

    oneOf:

          - type: string

          - type: integer

it will return the mixed type array arry[]->[integer, string, integer, integer, string] 

Could you please let me know, how can I represent array arry[] -> [integer, integer, integer] or arry[]-> [string, string, string]  in the OpenAPI definition?

 

  • You are almost there. Here's how to define a non-mixed array:

     

    arry:
      oneOf:
        - type: array
          items:
            type: integer
        - type: array
          items:
            type: string

     

     

    Depending on how well your tooling handles "oneOf", this can be simplified into:

     

    arry:
      type: array
      oneOf:
        - items:
            type: integer
        - items:
            type: string

     

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    You are almost there. Here's how to define a non-mixed array:

     

    arry:
      oneOf:
        - type: array
          items:
            type: integer
        - type: array
          items:
            type: string

     

     

    Depending on how well your tooling handles "oneOf", this can be simplified into:

     

    arry:
      type: array
      oneOf:
        - items:
            type: integer
        - items:
            type: string

     

    • lnash94's avatar
      lnash94
      Occasional Contributor

      Thank you HKosova , This solution fits to my requirement of tool ğŸ™‚

    • elanh's avatar
      elanh
      Occasional Visitor

      Hey HKosova ,

       

      I'm trying to do a mixed type array for a body parameter called params using the "anyOf" param but it seems to only let me choose ONE of the options instead of adding both into the array. For context, the request I'm trying to replicate looks like this:

       

      URL: https://eth-mainnet.alchemyapi.io/v2/your-api-key
      RequestType: POST
      Body: 
      {
          "jsonrpc":"2.0",
          "method":"eth_getBlockByNumber",
          "params":["0x1b4", true],
          "id":0
      }

       

      The OpenAPI spec for "params" is what I'm planning on using the "anyOf" (or "allOf") field for as follows:

                  "params": {
                    "type": "array",
                    "items": {
                      "anyOf": [
                        {
                          "type": "string",
                          "description": "yo yo"
                        },
                        {
                          "type": "boolean",
                          "description": "hello"
                        }
                      ]
                    },
                    "description": "something"
                  }

       Any ideas on how to achieve this?