nested_schemas.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. Validating highly nested schemas shouldn't cause exponential time blowups.
  3. See https://github.com/python-jsonschema/jsonschema/issues/1097.
  4. """
  5. from itertools import cycle
  6. from jsonschema.validators import validator_for
  7. metaschemaish = {
  8. "$id": "https://example.com/draft/2020-12/schema/strict",
  9. "$schema": "https://json-schema.org/draft/2020-12/schema",
  10. "$vocabulary": {
  11. "https://json-schema.org/draft/2020-12/vocab/core": True,
  12. "https://json-schema.org/draft/2020-12/vocab/applicator": True,
  13. "https://json-schema.org/draft/2020-12/vocab/unevaluated": True,
  14. "https://json-schema.org/draft/2020-12/vocab/validation": True,
  15. "https://json-schema.org/draft/2020-12/vocab/meta-data": True,
  16. "https://json-schema.org/draft/2020-12/vocab/format-annotation": True,
  17. "https://json-schema.org/draft/2020-12/vocab/content": True,
  18. },
  19. "$dynamicAnchor": "meta",
  20. "$ref": "https://json-schema.org/draft/2020-12/schema",
  21. "unevaluatedProperties": False,
  22. }
  23. def nested_schema(levels):
  24. """
  25. Produce a schema which validates deeply nested objects and arrays.
  26. """
  27. names = cycle(["foo", "bar", "baz", "quux", "spam", "eggs"])
  28. schema = {"type": "object", "properties": {"ham": {"type": "string"}}}
  29. for _, name in zip(range(levels - 1), names):
  30. schema = {"type": "object", "properties": {name: schema}}
  31. return schema
  32. validator = validator_for(metaschemaish)(metaschemaish)
  33. if __name__ == "__main__":
  34. from pyperf import Runner
  35. runner = Runner()
  36. not_nested = nested_schema(levels=1)
  37. runner.bench_func("not nested", lambda: validator.is_valid(not_nested))
  38. for levels in range(1, 11, 3):
  39. schema = nested_schema(levels=levels)
  40. runner.bench_func(
  41. f"nested * {levels}",
  42. lambda schema=schema: validator.is_valid(schema),
  43. )