Skip to content

Commit

Permalink
Fixed colliding variables
Browse files Browse the repository at this point in the history
  • Loading branch information
horejsek committed Apr 16, 2019
1 parent b335a40 commit 8c38d0f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
@@ -1,3 +1,8 @@
=== 2.11 (2019-04-16)

* Fix of additionalProperties (colliding variable names).


=== 2.10 (2019-04-15)

* Fix pattern regexps with a space.
Expand Down
8 changes: 4 additions & 4 deletions fastjsonschema/draft04.py
Expand Up @@ -357,10 +357,10 @@ def generate_items(self):
elif isinstance(items_definition, list):
for idx, item_definition in enumerate(items_definition):
with self.l('if {variable}_len > {}:', idx):
self.l('{variable}_{0} = {variable}[{0}]', idx)
self.l('{variable}__{0} = {variable}[{0}]', idx)
self.generate_func_code_block(
item_definition,
'{}_{}'.format(self._variable, idx),
'{}__{}'.format(self._variable, idx),
'{}[{}]'.format(self._variable_name, idx),
)
if isinstance(item_definition, dict) and 'default' in item_definition:
Expand Down Expand Up @@ -433,10 +433,10 @@ def generate_properties(self):
key_name = re.sub(r'($[^a-zA-Z]|[^a-zA-Z0-9])', '', key)
with self.l('if "{}" in {variable}_keys:', key):
self.l('{variable}_keys.remove("{}")', key)
self.l('{variable}_{0} = {variable}["{1}"]', key_name, key)
self.l('{variable}__{0} = {variable}["{1}"]', key_name, key)
self.generate_func_code_block(
prop_definition,
'{}_{}'.format(self._variable, key_name),
'{}__{}'.format(self._variable, key_name),
'{}.{}'.format(self._variable_name, key),
)
if isinstance(prop_definition, dict) and 'default' in prop_definition:
Expand Down
2 changes: 1 addition & 1 deletion fastjsonschema/version.py
@@ -1 +1 @@
VERSION = '2.10'
VERSION = '2.11'
17 changes: 17 additions & 0 deletions tests/test_security.py
Expand Up @@ -44,3 +44,20 @@ def test_generate_code_with_proper_variable_names(asserter, schema, value):
'$schema': 'http://json-schema.org/draft-07/schema',
**schema
}, value, value)


def test_generate_code_without_overriding_variables(asserter):
# We use variable name by property name. In the code is automatically generated
# FOO_keys which could colide with keys parameter. Then the variable is reused and
# for example additionalProperties feature is not working well. We need to make
# sure the name not colide.
value = {
'keys': [1, 2, 3],
}
asserter({
'type': 'object',
'properties': {
'keys': {'type': 'array'},
},
'additionalProperties': False,
}, value, value)

0 comments on commit 8c38d0f

Please sign in to comment.