Skip to content

Commit

Permalink
Fix exception when additionalProperties is boolean
Browse files Browse the repository at this point in the history
jsonschema draft 4 allows additionalProperties to be true and false, in
addition to containing a subschema.

Related to: #30
  • Loading branch information
avian2 committed Apr 21, 2018
1 parent 60b2f02 commit 92b05d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions jsonmerge/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ def merge(self, walk, base, head, schema, meta, objclass_menu=None, objClass='_d

if subschema.is_undef():
p = schema.get('additionalProperties')
if not p.is_undef():
# additionalProperties can be boolean in draft 4
if not p.is_undef() and walk.is_type(p, "object"):
subschema = p

base[k] = walk.descend(subschema, base.get(k), v, meta)
Expand All @@ -281,8 +282,9 @@ def descend_keyword(keyword):
descend_keyword("properties")
descend_keyword("patternProperties")

# additionalProperties can be boolean in draft 4
p = schema.get("additionalProperties")
if not p.is_undef():
if not p.is_undef() and walk.is_type(p, "object"):
schema2["additionalProperties"] = walk.descend(p, meta)

return schema2
23 changes: 23 additions & 0 deletions tests/test_jsonmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ def test_merge_append_additional(self):

self.assertEqual(base, {'a': ["a", "b"], 'b': 'c'})

def test_merge_additional_bool(self):

schema = {'additionalProperties': True}

base = {}
head = {'a': 'a'}

base = jsonmerge.merge(base, head, schema)

self.assertEqual(base, {'a': 'a'})

def test_example(self):

head1 = {
Expand Down Expand Up @@ -1857,6 +1868,18 @@ def test_merge_append_additional(self):

self.assertEqual(schema2, expected)

def test_merge_additional_bool(self):

schema = {'additionalProperties': True}

base = {}
head = {'a': 'a'}

merger = jsonmerge.Merger(schema)
schema2 = merger.get_schema()

self.assertEqual(schema2, schema)

def test_oneof(self):

schema = {
Expand Down

0 comments on commit 92b05d6

Please sign in to comment.