python-jsonschema-objects icon indicating copy to clipboard operation
python-jsonschema-objects copied to clipboard

unable to parse lottie schema

Open andrew-ld opened this issue 11 months ago • 2 comments

python version: 3.11.7 python-jsonschema-objects version: fd28c9cc4bd623627ef943ecf62ec12e85cd1105 (installed from git)

Describe the bug

/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/__init__.py:60: UserWarning: Schema version https://json-schema.org/draft/2020-12/schema not recognized. Some keywords and features may not be supported.
  warnings.warn(
Traceback (most recent call last):
  File "/home/andrew/PyCharmProjects/json2protobuf/main.py", line 13, in <module>
    main(sys.argv[-1])
  File "/home/andrew/PyCharmProjects/json2protobuf/main.py", line 8, in main
    ns = builder.build_classes()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/__init__.py", line 240, in build_classes
    builder.construct(nm, self.schema, **kw)
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 497, in construct
    ret = self._construct(uri, *args, **kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 532, in _construct
    potential_parents = self.expand_references(uri, clsdata["allOf"])
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 458, in expand_references
    pp.append(self.resolve_type(elem["$ref"], source_uri))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 486, in resolve_type
    self.resolved[uri] = self.construct(uri, resolved.contents, (ProtocolBase,))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 497, in construct
    ret = self._construct(uri, *args, **kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 532, in _construct
    potential_parents = self.expand_references(uri, clsdata["allOf"])
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 458, in expand_references
    pp.append(self.resolve_type(elem["$ref"], source_uri))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 486, in resolve_type
    self.resolved[uri] = self.construct(uri, resolved.contents, (ProtocolBase,))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 497, in construct
    ret = self._construct(uri, *args, **kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 541, in _construct
    self.resolved[uri] = self._build_object(uri, clsdata, parents, **kw)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/PyCharmProjects/json2protobuf/.venv/lib/python3.11/site-packages/python_jsonschema_objects/classbuilder.py", line 768, in _build_object
    required = set.union(*[p.__required__ for p in parents])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unbound method set.union() needs an argument

Example Schema and code lottie.schema.json

import sys

import python_jsonschema_objects


def main(schema_path: str):
    builder = python_jsonschema_objects.ObjectBuilder(schema_path)
    ns = builder.build_classes()
    print(ns.values())


if __name__ == "__main__":
    main(sys.argv[-1])

andrew-ld avatar Mar 02 '24 14:03 andrew-ld

I've dug into this a little bit, and while initially the schema didn't parse due to lack of support for the const keyword, that's been fixed in #283 and it still doesn't parse.

The issue now is definitions which use the const keyword without a type like here: https://github.com/cwacek/python-jsonschema-objects/blob/71323dea962a477fa62c74043286f6d9b97357d4/test/resources/lottie.schema.json#L2935-L2938

This is a case where the jsonschema spec isn't clear but the library currently expects all "types" within the schema to have a type field. Since we need to generate a class for each type, it's not clear what to do when the type isn't defined. I do realize that for const fields this is a bit of a no-op, but my current position is that the schema isn't actually well formed.

Do you have a perspective on this @andrew-ld?

cwacek avatar Mar 05 '24 17:03 cwacek

I honestly don't have enough experience with json schema specifications to be able to answer, I got the schema from https://lottiefiles.github.io/lottie-docs/schema/.

andrew-ld avatar Mar 05 '24 18:03 andrew-ld