cwacek / python-jsonschema-objects

Automatic Python binding generation from JSON Schemas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Defaults are not used when they are defined in a reference

buchri opened this issue · comments

Describe the bug
If I have a schema definitions with properties that refer to other definitions and the default value is set in the definition and not in the property it will not use that default value.

I use the latest version from GitHub.

Example Schema and code
Sample python code to show the problem

# test.py
import os
import python_jsonschema_objects as pjs

dir_path = os.path.dirname(os.path.realpath(__file__))
SCHEMA = os.path.join(dir_path, 'schema.json')


def get_namespace():
    builder = pjs.ObjectBuilder(SCHEMA)
    ns = builder.build_classes()
    return ns


def schema_defaults():
    ns = get_namespace()
    test = ns.Test()
    print(test.serialize())


if __name__ == "__main__":
    schema_defaults()

Not working schema and output

# schema.json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "$id": "schema.json",
  "$ref": "#/definitions/test",
  "definitions": {
    "test": {
      "type": "object",
      "properties": {
        "name": {
          "$ref": "#/definitions/name"
        },
        "number": {
          "$ref": "#/definitions/number"
        },
        "object": {
          "$ref": "#/definitions/object"
        }
      }
    },
    "name": {
      "type": "string",
      "default": "String"
    },
    "number": {
      "type": "number",
      "default": 10
    },
    "object": {
      "type": "object",
      "default": {}
    }
  }
}
# python test.py
{}

Working schema and output

# schema.json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "$id": "schema.json",
  "$ref": "#/definitions/test",
  "definitions": {
    "test": {
      "type": "object",
      "properties": {
        "name": {
          "$ref": "#/definitions/name",
          "default": "String"
        },
        "number": {
          "$ref": "#/definitions/number",
	  "default": 10
        },
        "object": {
          "$ref": "#/definitions/object",
          "default": {}
        }
      }
    },
    "name": {
      "type": "string"
    },
    "number": {
      "type": "number"
    },
    "object": {
      "type": "object"
    }
  }
}
# python test.py
{"name": "String", "number": 10, "object": {}}

Expected behavior
I would excpect the same output on the not working schema as I have on the working schema.
So the default value should be recursively usable.