Skip to content

Commit 8904ea0

Browse files
authored
Merge pull request #130 from abravalheri/fix-unexpected-caching
Fix unexpected caching (issue #129)
2 parents f3c8187 + 1fa3ebc commit 8904ea0

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

fastjsonschema/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def compile_to_code(definition, handlers={}, formats={}, use_default=True):
217217

218218

219219
def _factory(definition, handlers, formats={}, use_default=True):
220-
resolver = RefResolver.from_schema(definition, handlers=handlers)
220+
resolver = RefResolver.from_schema(definition, handlers=handlers, store={})
221221
code_generator = _get_code_generator_class(definition)(
222222
definition,
223223
resolver=resolver,

fastjsonschema/generator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, definition, resolver=None):
5454
self._validation_functions_done = set()
5555

5656
if resolver is None:
57-
resolver = RefResolver.from_schema(definition)
57+
resolver = RefResolver.from_schema(definition, store={})
5858
self._resolver = resolver
5959

6060
# add main function to `self._needed_validation_functions`

fastjsonschema/ref_resolver.py

+7
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,19 @@ class RefResolver:
7777
def __init__(self, base_uri, schema, store={}, cache=True, handlers={}):
7878
"""
7979
`base_uri` is URI of the referring document from the `schema`.
80+
`store` is an dictionary that will be used to cache the fetched schemas
81+
(if `cache=True`).
82+
83+
Please notice that you can have caching problems when compiling schemas
84+
with colliding `$ref`. To force overwriting use `cache=False` or
85+
explicitly pass the `store` argument (with a brand new dictionary)
8086
"""
8187
self.base_uri = base_uri
8288
self.resolution_scope = base_uri
8389
self.schema = schema
8490
self.store = store
8591
self.cache = cache
92+
# ^-- Create a brand new cache if the
8693
self.handlers = handlers
8794
self.walk(schema)
8895

tests/test_integration.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from fastjsonschema import JsonSchemaValueException
3+
from fastjsonschema import JsonSchemaValueException, compile
44

55

66
definition = {
@@ -127,3 +127,18 @@ def test_any_of_with_patterns(asserter):
127127
}, {
128128
'hash': 'AAAXXX',
129129
})
130+
131+
132+
def test_swap_handlers():
133+
# Make sure that by swapping resolvers, the schemas do not get cached
134+
repo1 = {
135+
"sch://schema": {"type": "array"}
136+
}
137+
validator1 = compile({"$ref": "sch://schema"}, handlers={"sch": repo1.__getitem__})
138+
assert validator1([1, 2, 3]) is not None
139+
140+
repo2 = {
141+
"sch://schema": {"type": "string"}
142+
}
143+
validator2 = compile({"$ref": "sch://schema"}, handlers={"sch": repo2.__getitem__})
144+
assert validator2("hello world") is not None

0 commit comments

Comments
 (0)