Skip to content

Commit d4a5eb2

Browse files
committed
Fix nested oneOf and anyOf
1 parent 1e21491 commit d4a5eb2

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Diff for: CHANGELOG.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
=== UNRELEASED
2+
3+
* Fix nested oneOf and anyOf
4+
5+
16
=== 2.15.1 (2021-05-05)
27

38
* Fix parsing date-time with +hhmm format

Diff for: fastjsonschema/draft04.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def __init__(self, definition, resolver=None, formats={}, use_default=True):
6565
('patternProperties', self.generate_pattern_properties),
6666
('additionalProperties', self.generate_additional_properties),
6767
))
68+
self._any_or_one_of_count = 0
6869

6970
@property
7071
def global_state(self):
@@ -145,16 +146,18 @@ def generate_any_of(self):
145146
146147
Valid values for this definition are 3, 4, 5, 10, 11, ... but not 8 for example.
147148
"""
148-
self.l('{variable}_any_of_count = 0')
149+
self._any_or_one_of_count += 1
150+
count = self._any_or_one_of_count
151+
self.l('{variable}_any_of_count{count} = 0', count=count)
149152
for definition_item in self._definition['anyOf']:
150153
# When we know it's passing (at least once), we do not need to do another expensive try-except.
151-
with self.l('if not {variable}_any_of_count:', optimize=False):
154+
with self.l('if not {variable}_any_of_count{count}:', count=count, optimize=False):
152155
with self.l('try:', optimize=False):
153156
self.generate_func_code_block(definition_item, self._variable, self._variable_name, clear_variables=True)
154-
self.l('{variable}_any_of_count += 1')
157+
self.l('{variable}_any_of_count{count} += 1', count=count)
155158
self.l('except JsonSchemaValueException: pass')
156159

157-
with self.l('if not {variable}_any_of_count:', optimize=False):
160+
with self.l('if not {variable}_any_of_count{count}:', count=count, optimize=False):
158161
self.exc('{name} must be valid by one of anyOf definition', rule='anyOf')
159162

160163
def generate_one_of(self):
@@ -173,16 +176,18 @@ def generate_one_of(self):
173176
174177
Valid values for this definition are 3, 5, 6, ... but not 15 for example.
175178
"""
176-
self.l('{variable}_one_of_count = 0')
179+
self._any_or_one_of_count += 1
180+
count = self._any_or_one_of_count
181+
self.l('{variable}_one_of_count{count} = 0', count=count)
177182
for definition_item in self._definition['oneOf']:
178183
# When we know it's failing (one of means exactly once), we do not need to do another expensive try-except.
179-
with self.l('if {variable}_one_of_count < 2:', optimize=False):
184+
with self.l('if {variable}_one_of_count{count} < 2:', count=count, optimize=False):
180185
with self.l('try:', optimize=False):
181186
self.generate_func_code_block(definition_item, self._variable, self._variable_name, clear_variables=True)
182-
self.l('{variable}_one_of_count += 1')
187+
self.l('{variable}_one_of_count{count} += 1', count=count)
183188
self.l('except JsonSchemaValueException: pass')
184189

185-
with self.l('if {variable}_one_of_count != 1:'):
190+
with self.l('if {variable}_one_of_count{count} != 1:', count=count):
186191
self.exc('{name} must be valid exactly by one of oneOf definition', rule='oneOf')
187192

188193
def generate_not(self):

0 commit comments

Comments
 (0)