From 817ec510c709232433ee9b32ae2a22d2d03783db Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 3 Apr 2014 21:53:31 -0700 Subject: [PATCH] Deepcopy defaults to prevent weird references in yaml. --- pre_commit/jsonschema_extensions.py | 4 +++- tests/jsonschema_extensions_test.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pre_commit/jsonschema_extensions.py b/pre_commit/jsonschema_extensions.py index 938303c9..75192902 100644 --- a/pre_commit/jsonschema_extensions.py +++ b/pre_commit/jsonschema_extensions.py @@ -16,7 +16,9 @@ def extend_with_default(validator_class): for property, subschema in properties.iteritems(): if "default" in subschema: - instance.setdefault(property, subschema["default"]) + instance.setdefault( + property, copy.deepcopy(subschema["default"]), + ) return jsonschema.validators.extend( validator_class, {"properties" : set_defaults}, diff --git a/tests/jsonschema_extensions_test.py b/tests/jsonschema_extensions_test.py index 4ea2686c..071bd8e3 100644 --- a/tests/jsonschema_extensions_test.py +++ b/tests/jsonschema_extensions_test.py @@ -49,3 +49,10 @@ def test_apply_defaults_deep(): }, ) assert ret == {'foo': {'bar': {'baz': 'herp'}}} + + +def test_apply_defaults_copies(): + schema = {'properties': {'foo': {'default': []}}} + ret1 = apply_defaults({}, schema) + ret2 = apply_defaults({}, schema) + assert ret1['foo'] is not ret2['foo']