Repository: cwacek/python-jsonschema-objects

Issue:
Instances of Generated Classes with Arrays Appear to be Global Across Class
I'm not entirely sure if I captured the problem concisely but here some example code the demonstrates the problem.

I have a simple schema that is as follows:
```
{
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "a" :{
            "type": "array",
            "items": {
                "type": "string"
            },
            "default": []
        }
    }
}
```

I use said schema to create and use objects with the following code:
```python
import python_jsonschema_objects as pjs

ExampleClass = pjs.ObjectBuilder('./schema.json').build_classes().ExampleSchema
foo = ExampleClass()
foo.a.append('foo')
print foo.for_json()

bar = ExampleClass()
bar.a.append('bar')
print bar.for_json()
```

The output I receive is the following:
```
{u'a': ['foo']}
{u'a': ['foo', 'bar']}
```

Why does the `bar` object's a array contain the string `foo`.  What I would have expected is the following output:
```
{u'a': ['foo']}
{u'a': ['bar']}
```

I would have expected each instance of the `ExampleClass` to have independent array contents and them not to be shared.

Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 84cb0d7..8af51f1 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,6 +2,7 @@ language: python
 python:
 - '2.7'
 - '3.5'
+- '3.6'
 env:
   global:
     - CC_TEST_REPORTER_ID=dc84306013e5ab029d115d8c2f9b27f23ee83b8d73c2dc35fed00954769fc76b
diff --git a/python_jsonschema_objects/classbuilder.py b/python_jsonschema_objects/classbuilder.py
index db31098..fa6b259 100644
--- a/python_jsonschema_objects/classbuilder.py
+++ b/python_jsonschema_objects/classbuilder.py
@@ -3,6 +3,7 @@ import python_jsonschema_objects.validators as validators
 import python_jsonschema_objects.pattern_properties as pattern_properties
 from python_jsonschema_objects.literals import LiteralValue
 
+import copy
 import collections
 import itertools
 import six
@@ -164,7 +165,9 @@ class ProtocolBase(collections.MutableMapping):
         # but only for the ones that have defaults set.
         for name in self.__has_default__:
             if name not in props:
-                default_value = self.__propinfo__[name]['default']
+                default_value = copy.deepcopy(
+                    self.__propinfo__[name]['default']
+                )
                 logger.debug(util.lazy_format("Initializing '{0}' to '{1}'",
                                               name, default_value))
                 setattr(self, name, default_value)
diff --git a/tox.ini b/tox.ini
index 1fd08be..4a88451 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,6 +1,6 @@
 
 [tox]
-envlist = py27, py35
+envlist = py27, py35, py36
 
 [testenv]
 ;install_command = pip install {opts} {packages}


Test patch:
diff --git a/test/test_regression_133.py b/test/test_regression_133.py
new file mode 100644
index 0000000..ac2cdbb
--- /dev/null
+++ b/test/test_regression_133.py
@@ -0,0 +1,27 @@
+
+import python_jsonschema_objects as pjs
+
+
+def test_nested_arrays_work_fine():
+    schema = {
+        "title": "Example Schema",
+        "type": "object",
+        "properties": {
+            "a": {"type": "array", "items": {"type": "string"}, "default": []}
+        },
+    }
+
+
+    ns1 = pjs.ObjectBuilder(schema).build_classes()
+
+    foo = ns1.ExampleSchema()
+    foo.a.append('foo')
+    print(foo.for_json())
+
+    assert foo.a == ['foo']
+
+    bar = ns1.ExampleSchema()
+    bar.a.append('bar')
+    print(bar.for_json())
+
+    assert bar.a == ['bar']