Complex schema validation broken on Windows - Wrong os.path use?
lapause opened this issue · comments
Hi,
It get the following error when trying to validate a schema with references on Windows:
Traceback (most recent call last):
File "test.py", line 28, in <module>
"more": 2
File "C:\(...)\lib\site-packages\jsonspec\validators\draft04.py", line 320, in validate
obj = validator.validate_properties(obj, pointer)
File "C:\(...)\lib\site-packages\jsonspec\validators\draft04.py", line 577, in validate_properties
obj[name] = validator(obj[name], pointer_join(pointer, name)) # noqa
File "C:\(...)\lib\site-packages\jsonspec\validators\bases.py", line 57, in __call__
return self.validate(obj, pointer)
File "C:\(...)\lib\site-packages\jsonspec\validators\bases.py", line 104, in validate
return self.validator.validate(obj, pointer)
File "C:\(...)\lib\site-packages\jsonspec\validators\bases.py", line 84, in validator
self._validator = self.context.resolve(self.pointer)
File "C:\(...)\lib\site-packages\jsonspec\validators\factorize.py", line 45, in resolve
self.spec)
File "C:\(...)\lib\site-packages\jsonspec\validators\factorize.py", line 96, in local
return compiler(schema, pointer, context)
File "C:\(...)\lib\site-packages\jsonspec\validators\draft04.py", line 69, in compile
subpointer = os.path.join(pointer, 'additionalProperties')
File "C:\(...)\lib\ntpath.py", line 65, in join
result_drive, result_path = splitdrive(path)
File "C:\(...)\lib\ntpath.py", line 115, in splitdrive
if len(p) > 1:
TypeError: object of type 'DocumentPointer' has no len()
You can reproduce the problem with the following sample code:
from jsonspec.validators import load
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"definitions": {
"test": {
"type": "object",
"properties": {
"foo": {"type": "string"}
},
"additionalProperties": False
}
},
"properties": {
"bar": {
"$ref": "#/definitions/test"
}
}
}
load(schema).validate({
"bar": {
"foo": "test",
"more": 2
}
})
The exception is triggered in the NT version of os.path.join(). Which raise the question: why use os.path.join() on elements that are not filesystem paths? Maybe I'm missing a point, but according to https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-3 it seems to me that references should always follow an URI pattern and even if the exception was not raised we would end up with backslashes in references which seems erroneous.
Suggested fix: os.path import should be removed from draft03.py and draft04.py, and
os.path.join(pointer, 'xxx'...)
occurrences replaced by simple
"/".join(pointer, 'xxx'...)
I hope you can push a minor version bump with the fix on Pypi soon.
hi, i've just pushed a new version to pypi (release 0.9.11). can you tell me if it fixes this error ?
Yes indeed.
Thank you for the fast hotfix!