aromanovich / jsl

A Python DSL for describing JSON schemas
http://jsl.readthedocs.org/
Other
218 stars 21 forks source link

JSL producing circular references #16

Closed pbutler closed 8 years ago

pbutler commented 8 years ago

Using the following code I end up with circular references in the poduced schea where both Children and Child reference themselves and provide no further information about their schema.

class Children(jsl.Document):
     children = jsl.OneOfField([
         jsl.DocumentField("Top", as_ref=True),
         jsl.DocumentField("Child", as_ref=True),
     ])

class Base(jsl.Document):
    mid = jsl.StringField(required=True)
    derivedFrom = jsl.DocumentField("Children", as_ref=True)

class Child(Base):
    abc = jsl.StringField(required=True)

class Top(Base):
    model = jsl.StringField(required=True)

test = """
{
    "mid": "a",
    "model": "a",
    "derivedFrom": {
        "children": {
            "mid": "b",
            "abc": "def"
        }
    }
}
"""

print Top.get_schema(ordered=True)

produces

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "__main__.Children": {
            "$ref": "#/definitions/__main__.Children"
        },
        "__main__.Top": {
            "type": "object",
            "properties": {
                "mid": {
                    "type": "string"
                },
                "derivedFrom": {
                    "$ref": "#/definitions/__main__.Children"
                },
                "model": {
                    "type": "string"
                }
            },
            "required": [
                "mid",
                "model"
            ],
            "additionalProperties": false
        },
        "__main__.Child": {
            "$ref": "#/definitions/__main__.Child"
        }
    },
    "$ref": "#/definitions/__main__.Top"
}
aromanovich commented 8 years ago

Fixed in jsl==0.2.1. Thanks for reporting!