question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dredd/Gavel incorrectly validates response body as valid when a JSON schema contains definitions and $ref

See original GitHub issue

I have noticed that dredd incorrectly marks my API responses as valid if I supply a JSON Schema that uses “definitions” as outlined here.

My server is returning the following JSON for a simple GET request:

{
    "address": {
        "street": "123 Test Street",
        "postcode": "12345"
    }
}

My API Blueprint has two test cases, one that validates the above JSON against a JSON Schema expecting the “address” object to contain three fields. The second request uses a JSON Schema that extracts the “address” object into a definition and references this with a JSON Pointer.

As expected the first request is failing with message body: At '/address/country' Missing required property: country. But the second request is happily passing.

# Test Endpoint [/api.php]

Tests the validity of server responses against json-schema.

## Schema with no definitions [GET]

+ Response 200 (application/json; charset=utf-8)

    + Schema

            {
                "$schema": "http://json-schema.org/draft-04/schema",
                "id": "schema-no-definitions",
                "type": "object",
                "properties": {
                    "address": {
                        "type": "object",
                        "properties": {
                            "street": {
                                "type": "string",
                                "minLength": 1
                            },
                            "postcode": {
                                "type": "string",
                                "minLength": 1
                            },
                            "country": {
                                "type": "string",
                                "minLength": 1
                            }
                        },
                        "required": [
                            "street", "postcode", "country"
                        ]
                    }
                },
                "required" : [
                    "address"
                ]
            }

## Test schema with definitions [GET]

+ Response 200 (application/json; charset=utf-8)

    + Schema

            {
                "$schema": "http://json-schema.org/draft-04/schema",
                "id": "schema-definitions",
                "definitions": {
                    "addressItem": {
                        "type": "object",
                        "properties": {
                            "street": {
                                "type": "string",
                                "minLength": 1
                            },
                            "postcode": {
                                "type": "string",
                                "minLength": 1
                            },
                            "country": {
                                "type": "string",
                                "minLength": 1
                            }
                        },
                        "required": [
                            "street", "postcode", "country"
                        ]
                    }
                },
                "properties": {
                    "address": {
                        "$ref": "#/definitions/addressItem"
                    }
                },
                "required" : [
                    "address"
                ]
            }

Should dredd/Gavel be supporting these JSON Schema features?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mrnamecommented, Apr 17, 2017

Does #767 also address this issue for swagger specifications? I would assume so, since I am using swagger 2.0 which utilizes json schema draft 4.

Only asking because I just installed from master and seem to be having this problem still.

EDIT: Scratch that, it was an error on my end where the dredd CLI command was still looking at the build installed from npm. Master seems to be working fine, thank you!

0reactions
honzajavorekcommented, Apr 18, 2017

Should be fixed in Dredd v3.4.2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Json schema validation error - Stack Overflow
Therefore you receive an error about wrong type. JSON schema type MUST be single value or array of such strings: "array","boolean","integer"," ...
Read more >
Invalid schema causes "should have required property '$ref ...
When an OpenAPI v3 Response Object contains a schema which has invalid schemas, an error is thrown saying: should have a required $ref...
Read more >
Understanding JSON Schema
JSON Schema is a powerful tool for validating the structure of JSON data. However, learning to use it by reading its.
Read more >
A Vocabulary for Structural Validation of JSON - JSON Schema
A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword. The...
Read more >
A Vocabulary for Structural Validation of JSON - JSON Schema
JSON Schema (application/schema+json) has several purposes, one of which is JSON instance validation. This document specifies a vocabulary for JSON Schema ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found