Dredd: Infer default JSON Schema in Swagger (can't validate. Expected body Content-Type is application/json but body is not a parseable JSON)

Created on 19 Oct 2017  路  6Comments  路  Source: apiaryio/dredd

Describe your problem

Dredd reports:

Can't validate. Expected body Content-Type is application/json but body is not a parseable JSON: Parse error on line 1: ^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'EOF'

when the response JSON is present and valid but the Swagger responseObject does not contain a schema, so this error text is misleading. It may refer to parsing the schema, not the response.

What command line options do you use?

$ npx dredd 

What is in your dredd.yml?

dry-run: null
hookfiles: null
language: nodejs
sandbox: false
server: node index.js
server-wait: 3
init: false
custom:
  apiaryApiKey: ''
names: false
only: []
reporter: apiary
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: ./contract/swagger.json
endpoint: 'http://127.0.0.1:3001'

What's your dredd --version output?

dredd v4.5.0 (Windows_NT 10.0.15063; x64)

Does dredd --level=debug uncover something?

https://gist.github.com/MikeRalphson/2b3c35bc05ad9009e4b9c4eaeda48444

There is a line debug: Could not stringify:

Can you send us failing test in a Pull Request?

To follow if required once I've looked at test structure.

As mentioned in #461

The only change required to make the test pass is the following diff to the swagger definition:

--- contract/min.json   2017-10-18 08:41:09.693730500 +0100
+++ contract/minplus.json       2017-10-18 08:46:48.036825400 +0100
@@ -27,7 +27,8 @@
                 ],
                 "responses": {
                     "200": {
-                        "description": "default"
+                        "description": "default",
+                        "schema": {}
                     }
                 }
             }
OpenAPI 2 validation bug easy to fix

Most helpful comment

The fix creates a new issue when expected response type is text/html and actual response type is also text/html. This assumes that your expected response type is now application/json

fail: body: Can't validate real media type 'text/plain' against expected media type 'application/schema+json'.

request: 
... (omitted, assume valid request)

expected: 
body: 

headers: 
   Content-Type: text/html; charset=UTF-8

statusCode: 200
bodySchema: {}


actual: 
body: 

headers: 
   ... (Other headers, omitted)
   content-type: text/html; charset=UTF-8

statusCode: 200

This works on previous dredd-transaction 4.5.0

All 6 comments

To me this seems like a bug in Dredd Transactions we should fix. Probably this line:

response.schema = schema if schema

...should be

response.schema = schema or {}

With API Blueprint, it cannot happen there is no schema, because either it's provided by the user, or the parser generates it. In Swagger when it is not provided by user, the parser leaves it blank.

The fix creates a new issue when expected response type is text/html and actual response type is also text/html. This assumes that your expected response type is now application/json

fail: body: Can't validate real media type 'text/plain' against expected media type 'application/schema+json'.

request: 
... (omitted, assume valid request)

expected: 
body: 

headers: 
   Content-Type: text/html; charset=UTF-8

statusCode: 200
bodySchema: {}


actual: 
body: 

headers: 
   ... (Other headers, omitted)
   content-type: text/html; charset=UTF-8

statusCode: 200

This works on previous dredd-transaction 4.5.0

I also saw this where the expected and actual types were text/yaml.

@kylef What would be the final behavior of the Swagger adapter? I recall you thought of some changes after the latest updates. How exactly will the Swagger adapter process produces/consumes and non-JSON media types?

Let's see what would be the ideal behavior for Dredd based on what the adapter will be doing. Then we should write some tests in Dredd Transactions to support the designed behavior and then let's implement it and release a final fix of the situation.

I discovered this is most likely duplicate of https://github.com/apiaryio/dredd/issues/556 or its very close relative. I'm working on a fix.

Was this page helpful?
0 / 5 - 0 ratings