Skip to content

Commit 1f1ac93

Browse files
authored
Merge pull request #144 from abravalheri/issue-114
Prevent items generator to create an empty for-loop (issue 114)
2 parents d63d682 + 1cd455a commit 1f1ac93

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

fastjsonschema/draft04.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -413,19 +413,23 @@ def generate_items(self):
413413
self.exc('{name} must contain only specified items', rule='items')
414414
else:
415415
with self.l('for {variable}_x, {variable}_item in enumerate({variable}[{0}:], {0}):', len(items_definition)):
416-
self.generate_func_code_block(
416+
count = self.generate_func_code_block(
417417
self._definition['additionalItems'],
418418
'{}_item'.format(self._variable),
419419
'{}[{{{}_x}}]'.format(self._variable_name, self._variable),
420420
)
421+
if count == 0:
422+
self.l('pass')
421423
else:
422424
if items_definition:
423425
with self.l('for {variable}_x, {variable}_item in enumerate({variable}):'):
424-
self.generate_func_code_block(
426+
count = self.generate_func_code_block(
425427
items_definition,
426428
'{}_item'.format(self._variable),
427429
'{}[{{{}_x}}]'.format(self._variable_name, self._variable),
428430
)
431+
if count == 0:
432+
self.l('pass')
429433

430434
def generate_min_properties(self):
431435
self.create_variable_is_dict()

fastjsonschema/generator.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -143,32 +143,40 @@ def generate_validation_function(self, uri, name):
143143
def generate_func_code_block(self, definition, variable, variable_name, clear_variables=False):
144144
"""
145145
Creates validation rules for current definition.
146+
147+
Returns the number of validation rules generated as code.
146148
"""
147149
backup = self._definition, self._variable, self._variable_name
148150
self._definition, self._variable, self._variable_name = definition, variable, variable_name
149151
if clear_variables:
150152
backup_variables = self._variables
151153
self._variables = set()
152154

153-
self._generate_func_code_block(definition)
155+
count = self._generate_func_code_block(definition)
154156

155157
self._definition, self._variable, self._variable_name = backup
156158
if clear_variables:
157159
self._variables = backup_variables
158160

161+
return count
162+
159163
def _generate_func_code_block(self, definition):
160164
if not isinstance(definition, dict):
161165
raise JsonSchemaDefinitionException("definition must be an object")
162166
if '$ref' in definition:
163167
# needed because ref overrides any sibling keywords
164-
self.generate_ref()
168+
return self.generate_ref()
165169
else:
166-
self.run_generate_functions(definition)
170+
return self.run_generate_functions(definition)
167171

168172
def run_generate_functions(self, definition):
173+
"""Returns the number of generate functions that were executed."""
174+
count = 0
169175
for key, func in self._json_keywords_to_function.items():
170176
if key in definition:
171177
func()
178+
count += 1
179+
return count
172180

173181
def generate_ref(self):
174182
"""

tests/test_array.py

+19
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,22 @@ def test_mixed_arrays(asserter, value, expected):
180180
},
181181
}, value, expected)
182182

183+
184+
def test_issue_114(asserter):
185+
"""Prevent the faulty scheme to generate an empty for-loop."""
186+
schema = {
187+
"type": "object",
188+
"properties": {
189+
"a": {
190+
"type": "array",
191+
"items": {
192+
"b": {
193+
"type": "string"
194+
}
195+
}
196+
}
197+
}
198+
}
199+
value = {"a": []}
200+
expected = value
201+
asserter(schema, value, expected)

0 commit comments

Comments
 (0)