Forum Discussion

ebagaipo's avatar
ebagaipo
New Contributor
6 years ago

Example Model

Hi, my current model example looks like this:

[
{
"id": 1930,
"customername": "Customer 1"
},
{
"id": 1900,
"customername": "Customer 2"
}
]

how to display it like this:

{
"data":
[
{
"id": 1930,
"customername": "Customer 1"
},
{
"id": 1900,
"customername": "Customer 2"
}
]
}


Many thanks,

3 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi ebagaipo,

     

    It looks like your model is currently defined as "type: array". You need to define it as "type: object" with the "data" property, and the type of data should be your array.

        MyModel:
          type: object
          properties:
            data:
              type: array
              items:
                type: object
                properties:
                  id:
                    type: integer
                  customername:
                    type: string
              example:
                - id: 1930
                  customername: Customer 1
                - id: 1900
                  customername: Customer 2
    • ebagaipo's avatar
      ebagaipo
      New Contributor

      Hi Helen,

       

      Thank you for your response.

       

      I tried this:

       

      paths:
        /Customers/GetAllCustomersAU:
          get:
            tags:
            - "Customer"
            summary: Retrieve all customers
            description: |
              Retrieve all customers from a specific laboratory.
            responses:
              '200':
                description: Retrieves all customers
                content:
                  application/json:
                    schema:
                      items:
                        $ref: "#/definitions/data"

       

      definitions:
        data:
          type: object
          properties:
            data:
              type: array
              items:
                type: object
              properties:
                id:
                  type: integer
                customername:
                  type: string
              example:
                - id: 1930
                  customername: Customer 1
                - id: 1900
                  customername: Customer 2

       

      The result is:

      [
        {
          "data": [
            {
              "id": 1930,
              "customername": "Customer 1"
            },
            {
              "id": 1900,
              "customername": "Customer 2"
            }
          ]
        }
      ]

       

      How to remove the first set of square brackets? So that it will appear only as:


        {
          "data": [
            {
              "id": 1930,
              "customername": "Customer 1"
            },
            {
              "id": 1900,
              "customername": "Customer 2"
            }
          ]
        }

       

      Many thanks

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        If the response "schema", put the $ref directly inside "schema". There's no need to have "items" there.

         

        By the way, you seem to be mixing up Swagger 2.0 and OpenAPI 3.0 syntax, which is not right. If you use swagger: 2.0, it should be:

              responses:
                200:
                  description: Retrieves all customers
                  schema: 
                    $ref: "#/definitions/data"
        
        definitions:
          data:
            type: object
            properties:
              ...


        If you use openapi: 3.0.0, schemas live in "components/schemas" instead of "definitions":

              responses:
                '200':
                  description: Retrieves all customers
                  content: 
                    application/json:
                      schema: 
                        $ref: "#/components/schemas/data"
        
        components:
          schemas:
            data:
              type: object
              properties:
                ...