Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(structured): reject a non-array anyOf/oneOf as a schema error, not a TypeError

Aashish3fa1cf51de7b
diff --git a/laya/structured.py b/laya/structured.py
index 407c5b33..32848a41 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -211,7 +211,13 @@ def _field(path: str, name: str, prop: Dict[str, Any], root: Optional[Dict[str,
branch.update((k, v) for k, v in prop.items() if k != "allOf")
return _field(path, name, branch, root, seen)
union = prop.get("anyOf") or prop.get("oneOf")
- if union is not None:
+ # The `allOf` branch above guards its wrapper's shape before iterating it; an `anyOf`/`oneOf`
+ # that is not an array (a JSON number, a boolean -- `bool` subclasses `int` and so is neither
+ # `None` nor iterable the way a list is) used to fall straight into the comprehension below
+ # and leak `TypeError: 'int' object is not iterable`. A non-array union is not a recognised
+ # optional wrapper, so it is left to the terminal "unsupported schema" rejection, which names
+ # the path exactly like the non-array `enum` case does.
+ if isinstance(union, list):
branches = [b for b in union if isinstance(b, dict) and b.get("type") != "null"]
if len(branches) != 1:
raise SchemaError(
diff --git a/tests/test_structured.py b/tests/test_structured.py
index a9fe94bb..06a2d05f 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -189,6 +189,25 @@ try:
except SchemaError as exc:
check("reject/non-array enum names the field and type", str(exc),
"properties.x: 'enum' must be an array of values, got str")
+# The same guard the `allOf` branch already applies to its wrapper, `anyOf`/`oneOf` did not: a
+# non-array union leaked `TypeError: 'int' object is not iterable` (and a `bool`, which subclasses
+# `int`, leaked `TypeError: 'bool' object is not iterable`) instead of the path-naming SchemaError
+# the module raises for every other malformed property. Both must now reject as an unsupported schema.
+for nonarray in (3, 1.5, True, False):
+ check_raises("reject/non-array union %s %r" % ("anyOf", nonarray), SchemaError,
+ _bad({"type": "object", "properties": {"x": {"anyOf": nonarray}}}))
+ check_raises("reject/non-array union %s %r" % ("oneOf", nonarray), SchemaError,
+ _bad({"type": "object", "properties": {"x": {"oneOf": nonarray}}}))
+try:
+ questions_from_json_schema({"type": "object", "properties": {"x": {"anyOf": 3}}})
+except SchemaError as exc:
+ check("reject/non-array union names the field and shows the schema", str(exc),
+ "properties.x: unsupported schema {'anyOf': 3}")
+# Positive control: a real one-non-null-branch union still unwraps (the guard is not over-strict).
+check("accept/optional anyOf still maps",
+ questions_from_json_schema(
+ {"type": "object", "properties": {"x": {"anyOf": [{"type": "boolean"}, {"type": "null"}]}}}
+ )["x"]["type"], "noul")
check_raises("reject/non-object root", SchemaError, _bad({"type": "array"}))
check_raises("reject/empty properties", SchemaError, _bad({"type": "object", "properties": {}}))