Skip to content

Commit

Permalink
Merge pull request #144 from abravalheri/issue-114
Browse files Browse the repository at this point in the history
Prevent items generator to create an empty for-loop (issue 114)
  • Loading branch information
horejsek authored Mar 28, 2022
2 parents d63d682 + 1cd455a commit 1f1ac93
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
8 changes: 6 additions & 2 deletions fastjsonschema/draft04.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,19 +413,23 @@ def generate_items(self):
self.exc('{name} must contain only specified items', rule='items')
else:
with self.l('for {variable}_x, {variable}_item in enumerate({variable}[{0}:], {0}):', len(items_definition)):
self.generate_func_code_block(
count = self.generate_func_code_block(
self._definition['additionalItems'],
'{}_item'.format(self._variable),
'{}[{{{}_x}}]'.format(self._variable_name, self._variable),
)
if count == 0:
self.l('pass')
else:
if items_definition:
with self.l('for {variable}_x, {variable}_item in enumerate({variable}):'):
self.generate_func_code_block(
count = self.generate_func_code_block(
items_definition,
'{}_item'.format(self._variable),
'{}[{{{}_x}}]'.format(self._variable_name, self._variable),
)
if count == 0:
self.l('pass')

def generate_min_properties(self):
self.create_variable_is_dict()
Expand Down
14 changes: 11 additions & 3 deletions fastjsonschema/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,40 @@ def generate_validation_function(self, uri, name):
def generate_func_code_block(self, definition, variable, variable_name, clear_variables=False):
"""
Creates validation rules for current definition.
Returns the number of validation rules generated as code.
"""
backup = self._definition, self._variable, self._variable_name
self._definition, self._variable, self._variable_name = definition, variable, variable_name
if clear_variables:
backup_variables = self._variables
self._variables = set()

self._generate_func_code_block(definition)
count = self._generate_func_code_block(definition)

self._definition, self._variable, self._variable_name = backup
if clear_variables:
self._variables = backup_variables

return count

def _generate_func_code_block(self, definition):
if not isinstance(definition, dict):
raise JsonSchemaDefinitionException("definition must be an object")
if '$ref' in definition:
# needed because ref overrides any sibling keywords
self.generate_ref()
return self.generate_ref()
else:
self.run_generate_functions(definition)
return self.run_generate_functions(definition)

def run_generate_functions(self, definition):
"""Returns the number of generate functions that were executed."""
count = 0
for key, func in self._json_keywords_to_function.items():
if key in definition:
func()
count += 1
return count

def generate_ref(self):
"""
Expand Down
19 changes: 19 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,22 @@ def test_mixed_arrays(asserter, value, expected):
},
}, value, expected)


def test_issue_114(asserter):
"""Prevent the faulty scheme to generate an empty for-loop."""
schema = {
"type": "object",
"properties": {
"a": {
"type": "array",
"items": {
"b": {
"type": "string"
}
}
}
}
}
value = {"a": []}
expected = value
asserter(schema, value, expected)

0 comments on commit 1f1ac93

Please sign in to comment.