cwacek/python-jsonschema-objects

unable to parse lottie schema

Closed this issue · 2 comments

python version: 3.11.7
python-jsonschema-objects version: fd28c9c (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])

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:

{
"title": "True",
"const": 1
},

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?

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/.