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

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

Closed
jessedc opened this issue Jul 24, 2015 · 10 comments
Labels
bug Context: validation apiaryio/gavel.js et. al

Comments

@jessedc
Copy link

jessedc commented Jul 24, 2015

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?

@stianborgesen
Copy link

+1

@netmilk
Copy link
Contributor

netmilk commented Aug 19, 2015

Hi @jessedc @stianborgesen,

this should work, Dredd/Gavel should support both Json Schema v3 and v4. This is a v4 feature, but the functionality in Dredd depends on underlaying JSON schema validator. I suspect it's a bug or feature of this validator. I'll investigate further...

@netmilk netmilk added bug Context: validation apiaryio/gavel.js et. al labels Aug 19, 2015
@joaosa
Copy link

joaosa commented Sep 19, 2016

@netmilk I am also experiencing this. Any news? :)

@honzajavorek
Copy link
Contributor

@joaosa this is the issue we're solving in apiaryio/gavel.js#25, right?

@joaosa
Copy link

joaosa commented Mar 9, 2017

@honzajavorek yes that seems to be the case. Must've mixed the issues

@honzajavorek
Copy link
Contributor

honzajavorek commented Mar 9, 2017 via email

@honzajavorek
Copy link
Contributor

I think this should be fixed by #767 and will be in the next Dredd release, thanks to @joaosa's work on Gavel.js!

@mrname
Copy link

mrname commented Apr 14, 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!

@honzajavorek
Copy link
Contributor

@mrname Good! I'm glad it works.

@honzajavorek
Copy link
Contributor

Should be fixed in Dredd v3.4.2.

artem-zakharchenko pushed a commit that referenced this issue Oct 9, 2019
Update sinon to the latest version 🚀
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Context: validation apiaryio/gavel.js et. al
Projects
None yet
Development

No branches or pull requests

6 participants