Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

body: At '' Invalid type: object (expected array) #745

Closed
betchi207 opened this issue Mar 8, 2017 · 3 comments
Closed

body: At '' Invalid type: object (expected array) #745

betchi207 opened this issue Mar 8, 2017 · 3 comments
Labels
Epic: CLI reporter Dredd's default reporter

Comments

@betchi207
Copy link

betchi207 commented Mar 8, 2017

Describe your problem

I don't know why such an error occurs

What command line options do you use?

none

What is in your dredd.yml?

reporter: apiary
custom:
  apiaryApiKey: ******************
  apiaryApiName: ******************
dry-run: null
hookfiles: null
language: nodejs
sandbox: false
server: null
server-wait: 3
init: false
names: false
only: []
output: []
header: []
sorted: false
user: null
inline-errors: false
details: false
method: []
color: true
level: info
timestamp: false
silent: false
path: []
hooks-worker-timeout: 5000
hooks-worker-connect-timeout: 1500
hooks-worker-connect-retry: 500
hooks-worker-after-connect-wait: 100
hooks-worker-term-timeout: 5000
hooks-worker-term-retry: 500
hooks-worker-handler-host: 127.0.0.1
hooks-worker-handler-port: 61321
config: ./dredd.yml
blueprint: >-
  ./swagger.yaml
endpoint: 'http://localhost:9000'

What's your dredd --version output?

dredd v3.2.0 (Darwin 16.4.0; x64)

Does dredd --level=debug uncover something?

info: Displaying failed tests...
fail: GET /v0/users duration: 45ms
fail: body: At '' Invalid type: object (expected array)

request: 
method: GET
uri: /v0/users
headers: 
    Accept: application/json
    User-Agent: Dredd/3.2.0 (Darwin 16.4.0; x64)

body: 



expected: 
headers: 
    Content-Type: application/json

body: 
{
  "users": [
    {
      "id": 1,
      "userId": "custom-user-id-0001",
      "name": "custom user 0001",
      "unreadCount": 0,
      "created": 1488294000000000000,
      "modified": 1488294000000000000
    }
  ]
}
statusCode: 200
bodySchema: {"type":"array","items":{"type":"object","required":["users"],"properties":{"users":{"type":"array","items":{"type":"object","required":["id","userId","name","unreadCount","created","modified"],"properties":{"id":{"type":"integer","example":1},"userId":{"type":"string","example":"custom-user-id-0001"},"name":{"type":"string","example":"custom user 0001"},"pictureUrl":{"type":"string","example":"http://example.com/img/rick.png"},"informationUrl":{"type":"string","example":"http://example.com/user/rick"},"unreadCount":{"type":"integer","example":0},"created":{"type":"integer","example":1488294000000000000},"modified":{"type":"integer","example":{"key":"value"}}}}}}}}


actual: 
statusCode: 200
headers: 
    access-control-allow-methods: POST, GET, OPTIONS, PUT, PATCH, DELETE
    access-control-allow-origin: *
    access-control-expose-headers: Location
    content-type: application/json
    date: Wed, 08 Mar 2017 08:58:09 GMT
    content-length: 195
    connection: close

body: 
{
  "users": [
    {
      "id": 1,
      "userId": "099b0836-a253-4f69-89d4-e3f095caaf98",
      "name": "sit mollit magna",
      "unreadCount": 0,
      "customData": {
        "k": "v"
      },
      "created": 1488958113425505500,
      "modified": 1488958113425505500
    }
  ]
}

swagger.yaml

swagger: '2.0'
info:
  title: ""
  description: ""
  version: 0.1.0
host: localhost:9000
basePath: /v0
schemes:
- http
paths:
  /users:
    get:
      summary: Get user list.
      produces:
      - application/json
      responses:
        200:
          description: User list
          schema:
            type: array
            items:
              $ref: '#/definitions/users'
          examples:
            application/json: |-
              {
                "users": [
                  {
                    "id": 1,
                    "userId": "custom-user-id-0001",
                    "name": "custom user 0001",
                    "unreadCount": 0,
                    "created": 1488294000000000000,
                    "modified": 1488294000000000000 
                  }
                ]
              }
definitions:
  users:
    type: object
    required:
    - users
    properties:
      users:
        type: array
        items:
          $ref: '#/definitions/user'
  user:
    type: object
    required:
    - id
    - userId
    - name
    - unreadCount
    - created
    - modified
    properties:
      id:
        type: integer
        example: 1
      userId:
        type: string
        example: custom-user-id-0001
      name:
        type: string
        example: custom user 0001
      pictureUrl:
        type: string
        example: http://example.com/img/rick.png
      informationUrl:
        type: string
        example: http://example.com/user/rick
      unreadCount:
        type: integer
        example: 0
      created:
        type: integer
        example: 1488294000000000000
      modified:
        type: integer
        example:
          key: value
@honzajavorek
Copy link
Contributor

Dredd uses the JSON Schema you provided to validate the response:

{"type":"array","items":{"type":"object","required":["users"],"properties":{"users":{"type":"array","items":{"type":"object","required":["id","userId","name","unreadCount","created","modified"],"properties":{"id":{"type":"integer","example":1},"userId":{"type":"string","example":"custom-user-id-0001"},"name":{"type":"string","example":"custom user 0001"},"pictureUrl":{"type":"string","example":"http://example.com/img/rick.png"},"informationUrl":{"type":"string","example":"http://example.com/user/rick"},"unreadCount":{"type":"integer","example":0},"created":{"type":"integer","example":1488294000000000000},"modified":{"type":"integer","example":{"key":"value"}}}}}}}}

The schema says the response should return an array of certain items. Your response, however, returns an object:

{
  "users": [
    {
      "id": 1,
      "userId": "099b0836-a253-4f69-89d4-e3f095caaf98",
      "name": "sit mollit magna",
      "unreadCount": 0,
      "customData": {
        "k": "v"
      },
      "created": 1488958113425505500,
      "modified": 1488958113425505500
    }
  ]
}

Thus Dredd correctly reports fail: body: At '' Invalid type: object (expected array), which means that at root path (that's the empty string '') an array is expected, but the server under test returned an object.

I agree the error isn't very readable 😞

@betchi207
Copy link
Author

Thanks!
It was a very easy mistake.
Sorry!

@honzajavorek
Copy link
Contributor

No problem!

@honzajavorek honzajavorek added the Epic: CLI reporter Dredd's default reporter label Mar 8, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Epic: CLI reporter Dredd's default reporter
Projects
None yet
Development

No branches or pull requests

2 participants