xeipuuv / gojsonschema

An implementation of JSON Schema, draft v4 v6 & v7 - Go language
2.53k stars 356 forks source link

limit fatal error: stack overflow #116

Open xboston opened 8 years ago

xboston commented 8 years ago

Hi! I have 2 struct:

type Profile struct {
    Name    string `json:"name"`
    Media []Media `json:"media,omitempty"`
}

type Media struct {
    Image    string `json:"image"`
    Profile   *Profile `json:"profile,omitempty"`
}

Profile has many Media, Media has one Profile.

Аnd jsonschema for models ( VoteStatsDaysList so-so ):

{
    "$schema": "http://json-schema.org/draft-04/hyper-schema",
    "$ref": "#/definitions/VoteStatsDaysList",
    "definitions": {
        "Media": {
            "type": "object",
            "properties": {
                "image": {
                    "type": "string"
                },
                "profile": {
                    "$ref": "#/definitions/Profile"
                },
                "time": {
                    "type": "string",
                    "format": "date-time"
                }
            },
            "additionalProperties": false,
            "required": [
                "image"
            ]
        },
        "Profile": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "media": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Media"
                    }
                }
            },
            "additionalProperties": false,
            "required": [
                "name"
            ]
        },
        "VoteStatsDaysList": {
            "type": "object",
            "properties": {
                "items": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Profile"
                    }
                },
                "total_pages": {
                    "type": "integer"
                }
            },
            "additionalProperties": false,
            "required": [
                "total_pages",
                "items"
            ]
        }
    }
}

Validation result:

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0x835d10, 0xe)
    /home/boston/.gvm/gos/go1.6.3/src/runtime/panic.go:547 +0x90
runtime.newstack()
    /home/boston/.gvm/gos/go1.6.3/src/runtime/stack.go:940 +0xb11
runtime.morestack()
    /home/boston/.gvm/gos/go1.6.3/src/runtime/asm_amd64.s:359 +0x7f

goroutine 1 [stack growth]:
github.com/xeipuuv/gojsonschema.(*Schema).parseSchema(0xc82000ee00, 0x7099e0, 0xc8200154a0, 0xc82854b860, 0xc82001bb88, 0xc828378fb8)
    /home/boston/docker/projects/test/golang/src/github.com/xeipuuv/gojsonschema/schema.go:102 fp=0xc840200900 sp=0xc8402008f8
github.com/xeipuuv/gojsonschema.(*Schema).parseProperties(0xc82000ee00, 0x7099e0, 0xc820015470, 0xc82854b680, 0x0, 0x0)
    /home/boston/docker/projects/test/golang/src/github.com/xeipuuv/gojsonschema/schema.go:866 +0x568 fp=0xc840200aa0 sp=0xc840200900
github.com/xeipuuv/gojsonschema.(*Schema).parseSchema(0xc82000ee00, 0x7099e0, 0xc820015440, 0xc82854b680, 0x0, 0x0)
    /home/boston/docker/projects/test/golang/src/github.com/xeipuuv/gojsonschema/schema.go:296 +0x26af fp=0xc840201ae8 sp=0xc840200aa0
github.com/xeipuuv/gojsonschema.(*Schema).parseReference(0xc82000ee00, 0x7099e0, 0xc8200153e0, 0xc82854b4a0, 0xc8200b92a0, 0x15, 0x0, 0x0)
..........
and many errors...

If remove profile from media in shema - are ok =(

johandorland commented 6 years ago

I thought #171 fixed all of these kind of issues but apparently I'm wrong. In this specific instance I suspect the ref "$ref": "#/definitions/VoteStatsDaysList" causes issues, so you might be able to work around it by bringing the #/definitions/VoteStatsDaysList to the root level instead of nested inside a definitions block.

Since writing #171 I've come across more examples of referencing which still don't work properly, so I'll try to fix those issues some point in the future.

chrisdostert commented 6 years ago

@johandorland validating against the opspec pkg schema has recently started hitting this error as well using latest version of this library so we'd be greatful too, cheers