30 Days Postman Developer: Day 18 The response body needs to be of type object, but we found an array instead

I have been stuck in this step since almost 2 days - :frowning:
Getting this exception while sending the request - The response body needs to be of type object, but we found an array instead

Request {{baseUrl}}/cosmos

[
    {
        "id": 301,
        "name": "Cassiopeia",
        "tag": "cosmos"
    },
    {
        "id": 302,
        "name": "Orion",
        "tag": "cosmos"
    },
    {
        "id": 303,
        "name": "Scorpius",
        "tag": "cosmos"
    }
]

Here is the API Definition: openapi: "3.0.0"info: version: 1.1.0 title: Cosmos description: A sam - Pastebin.com

Hi @ishaanx, Welcome to the community!

Please check the example in the request.

1 Like

@pranavdavar If we check the examples, there is a slight difference -

This is the example:

{
 "id": 41364023,
 "name": "Ut proident",
 "tag": "sint sunt do ullamc"
}

And this is my actual response -

[
    {
        "id": 301,
        "name": "Cassiopeia",
        "tag": "cosmos"
    },
    {
        "id": 302,
        "name": "Orion",
        "tag": "cosmos"
    },
    {
        "id": 303,
        "name": "Scorpius",
        "tag": "cosmos"
    }
]

The thing that I don’t understand is in my Schema definition the response type is already set as ‘object’. But this error is still here.

To fix the issue you may need to have sync at all places i.e. specifications, example response and actual response.

@pranavdavar I changed the object type in Schema definition to ‘object’ and yet I don’t see any difference in the response or the error.

Hi @ishaanx,

Api is returning array but you have specified object. It will never match, that is actually the error.

Hi @pranavdavar ,
If I understand correctly, API is returning array and my schema definition should also have object type array? Even if I change the ‘object type’ in my schema definition I still see the error. So what am I missing?

Sorry for asking too many questions. I’m a fairly new to Javascript and Postman.

Now you have 2 things in array
1 API Specification
2 Response

Last thing left is example
Change the example, so that it is in array data type

@pranavdavar Nevermind, I figured it out. Thank you for showing your patience with me and for your help. :slight_smile:

Good to hear that, you were able to fix.:blush:

1 Like

Hi guys,
could you please give me some hints on what should I do with this problem ?
I’m facing similar behavior as @ishaanx but I changed example of response, I have type as an array in schemas, and still I see ‘Response body doesn’t match schema’.
In example I have this kind of response:

[
    {

        "id": 301,

        "name": "Cassiopeia",

        "tag": "cosmos"

    },

    {

        "id": 302,

        "name": "Orion",

        "tag": "cosmos"

    },

    {

        "id": 303,

        "name": "Scorpius",

        "tag": "cosmos"

    }
]

thanks in advance

Hey @Matjas_22

What do you have in your schema? Have you changed anything in there?

hi @danny-dainton
this is how it looks:

schemas:

    Cosmos:

      type: array

      required:

        - id

        - name

      properties:

        id:

          type: integer

          format: int64

        name:

          type: string

        tag:

          type: string

    Cosmo:

      type: array

      properties:

        code:

          type: integer

          format: int32

        message:

          type: string

    Error:

      type: array

      required:

        - code

        - message

      properties:

        code:

          type: integer

          format: int32

        message:

          type: string

I’ve put ‘array’ as a type for Cosmos part.

Are you sure that’s all that’s needed here? :thinking:

That array [ ... ] that you have in the response, has objects [ {...},{...} ] inside it, with those listed properties.

Take a look at how you might add items with a type within the schema.

It would kinda defeat the training and learning opportunity, if I just tell you the answer :smiley:

4 Likes

Thank you !
I’ve found the solution, it was a valuable lesson :slight_smile:

1 Like

hey guys

I am facing a similar issue and I have tried a few options, however, it is not working, would you mind helping me?

That is my YAML

paths:
  /makers:
    get:
      summary: 'Returns a list of makers'
      responses:
        '200':
          description: List of Makers
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Makers'
        default:
          description: Empty List
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'

components:
  schemas: 
    Makers:
        required: 
            - "key"
            - "models"
        properties: 
            key: 
                type: "string"
                example: "Nissan"
            models: 
                type: "array"
                items: 
                    type: "object"
                    properties: 
                        name: 
                            type: "string"
                            example: "Sentra"
                        years: 
                            type: "array"
                            items: 
                                type: "number"
                            example: 
                                - 2000
                                - 2001

and that is the response that auto generates from the collection

{
 "key": "Nissan",
 "models": [
  {
   "name": "Sentra",
   "years": [
    2000,
    2001
   ]
  },
  {
   "name": "Sentra",
   "years": [
    2000,
    2001
   ]
  }
 ]
}

that is the actual return

[
    {
        "key": "Nissan",
        "models": [
            {
                "model": "Sentra",
                "years": [
                    2000,
                    2001
                ]
            },
            {
                "model": "Maxima",
                "years": [
                    2000,
                    2001
                ]
            },
            {
                "model": "Skyline",
                "years": [
                    2000,
                    2001
                ]
            }
        ]
    },
    {
        "key": "Ford",
        "models": [
            {
                "model": "Taurus",
                "years": [
                    2000,
                    2001
                ]
            },
            {
                "model": "Escort",
                "years": [
                    2000,
                    2001
                ]
            }
        ]
    }
]

I have already tried to add the array type before $ref, or change the makers and models to array and all other combinations, but I couldn’t make something like ( {…},{…} }

Hi @matheus,

You need to update the YAML to have array in makers.

1 Like

Hey Pranav, do you mean I am supposed to modify the example and save and then validate? Because that raises an issue found during validation.

Plus this is what I wrote for the “missing property items error”:

Hi, I am facing the same issue! Can you help me troubleshoot.

Hi, for the life of me can’t figure out what is wrong. So, I go to List all cosmos GET request and run that and it gives me the following error:

The response body needs to be of type object, but we found an array instead.

The response body is as below which is an array of objects:

[
    {
        "id": 301,
        "name": "Cassiopeia",
        "tag": "cosmos"
    },
    {
        "id": 302,
        "name": "Orion",
        "tag": "cosmos"
    },
    {
        "id": 303,
        "name": "Scorpius",
        "tag": "cosmos"
    }
]

Then I go back to my draft and go to the Definition tab and look to adjust my schema to match the response body that is being returned as above which is an array of objects. I adjust my schema accordingly as below:

components:
  schemas:
    Cosmos:
      type: array
      items:
        type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string

When I go back to List all cosmos GET request and run it I still get the same error (The response body needs to be of type object, but we found an array instead.) Huh??? My schema shows an array of objects just like the response body is displaying.

Please help! What am I missing here?

1 Like