From 3fa1cf51de7badedf72379b6b803a961510e9086 Mon Sep 17 00:00:00 2001 From: Aashish <145881415+aashish254@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:49:17 +0545 Subject: [PATCH 1/2] fix(structured): reject a non-array anyOf/oneOf as a schema error, not a TypeError The allOf branch guards its wrapper's shape before iterating it; anyOf/oneOf did not, so a schema whose union is a JSON number (or a bool, which subclasses int) fell straight into the branch comprehension and leaked `TypeError: 'int' object is not iterable` instead of the path-naming SchemaError the module raises for every other malformed property -- including the sibling non-array `enum` case. A non-array union is not a recognised optional wrapper, so the guard now lets it fall through to the terminal "unsupported schema" rejection, which names the field path. Well-formed one-non-null-branch unions still unwrap unchanged. Adds the rejection cases and a positive control to tests/test_structured.py (196 -> 206 checks; the structured docs/api suites stay green). --- laya/structured.py | 8 +++++++- tests/test_structured.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/laya/structured.py b/laya/structured.py index 407c5b33c..32848a416 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 a9fe94bbd..06a2d05fb 100644 --- a/tests/test_structured.py +++ b/tests/test_structured.py @@ -189,6 +189,25 @@ def _bad(schema): 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": {}})) From 6df415fdbae2d2b5a0042b8a4c2635214d0dddff Mon Sep 17 00:00:00 2001 From: Aashish <145881415+aashish254@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:50:57 +0545 Subject: [PATCH 2/2] fix(java): reject a non-array anyOf/oneOf as unsupported schema, matching Python laya/structured.py::_field now guards the union with isinstance(union, list), so a truthy non-array anyOf/oneOf (string, object, number, boolean) falls to the terminal "unsupported schema" rejection instead of leaking a TypeError or reaching the "got 0" union-arity refusal. Decisions.java reproduced the old behaviour: it iterated a truthy non-array to zero branches ("got 0") and raised "'must be an array of branches'" for non-iterables -- the one recorded exception to the message-for-message rule. Gate the unwrap on `union instanceof List` and let any other shape fall past the block, so all three message-producing ports (Python, Java, and TypeScript, which already gated on Array.isArray) agree. Regenerate the fixture with gen_fixtures.py: exactly two refusal rows change, both the truthy-non-array anyOf cases; the genuine list-union "got 0" (union-of-null-only) is untouched. --- laya-java/fixtures/structured.json | 4 +-- .../com/convaiinnovations/laya/Decisions.java | 31 +++++++------------ 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/laya-java/fixtures/structured.json b/laya-java/fixtures/structured.json index 4a0384dae..afc2a3b4b 100644 --- a/laya-java/fixtures/structured.json +++ b/laya-java/fixtures/structured.json @@ -3204,7 +3204,7 @@ } } }, - "error": "properties.x: only 'Optional[...]' unions (one non-null branch) are supported, got 0" + "error": "properties.x: unsupported schema {'anyOf': 'abc'}" }, { "case": "truthy-non-array-anyof-with-a-oneof-to-shadow", @@ -3226,7 +3226,7 @@ } } }, - "error": "properties.x: only 'Optional[...]' unions (one non-null branch) are supported, got 0" + "error": "properties.x: unsupported schema {'anyOf': {'a': 1}, 'oneOf': [{'type': 'boolean'}, {'type': 'null'}]}" }, { "case": "top-level-is-null", diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.java index 127470863..3b15b9dae 100644 --- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.java +++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.java @@ -436,32 +436,23 @@ private static Field field(String path, String name, Object raw, Map> branches = new ArrayList<>(); - if (union instanceof List) { - for (Object candidate : (List) union) { - if (candidate instanceof Map - && !"null".equals(((Map) candidate).get("type"))) { - branches.add((Map) candidate); - } + for (Object candidate : (List) union) { + if (candidate instanceof Map + && !"null".equals(((Map) candidate).get("type"))) { + branches.add((Map) candidate); } - } else if (!(union instanceof String) && !(union instanceof Map)) { - // A string iterates its characters and an object its keys, so both reach the - // refusal below with zero branches, which is what the reference raises. A - // number or a boolean is not iterable at all: the reference raises TypeError - // there, and this is the one recorded exception to the - // message-for-message rule -- see the class note. - throw new SchemaException(path + ": '" - + (truthy(prop.get("anyOf")) ? "anyOf" : "oneOf") - + "' must be an array of branches, got " + pythonType(union)); } if (branches.size() != 1) { throw new SchemaException(path + ": only 'Optional[...]' unions (one non-null"