brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

Expanding json schema causes stack level too deep error #50

Closed okitan closed 8 years ago

okitan commented 8 years ago

Here is the brief POC code.

json = <<_JSON_
{
    "id": "http://example.com/poc.json",
    "$schema": "http://json-schema.org/draft-04/hyper-schema#",
    "definitions": {
        "self":    { "$ref": "#" },
        "synonym": { "$ref": "#/definitions/self" }
    },
    "properties": {
        "id":   { "type": "string" }
    }
}
_JSON_

schema = JsonSchema::Parser.new.parse!(JSON.parse(json))
schema.expand_references #=> SystemStackError

I found #/definitions/synonym is treated as original, and schema_children of it includes #/definitions/synonym. It means schema.parent returns schema itself, and it cause infinite loop and stack level too deep error when schema.parent is called against #/definitions/synonym. (See: https://github.com/brandur/json_schema/blob/master/lib/json_schema/reference_expander.rb#L100-L104)

I also found # were not treated as original. This is also wired.

I tried to fix "original problem", but I can't....