Repository navigation
fix(structured): reject a non-array anyOf/oneOf as a schema error - #1364
Conversation
|
Holding this one, and I want to be precise about why, because the change itself is an improvement and the reason is not about your code. The old message for The problem is that the old wording is a cross-port contract, and two ports reproduce it deliberately.
So someone already walked into the exact case you are fixing, from the other direction, and wrote the port to match Python's Measured rather than assumed: with this PR on the branch, With this PR removed and the other eight merged, every Java gate is clean again: 20 families match, What would make this mergeable, and I think it is worth doing:
I can carry it across the ports myself if you would rather not, but I would be writing Java I cannot run locally, so it is better from you. Either way the Python half is right and I am not asking you to change it. The other eight PRs in this batch merged clean and are going out in the next release. This is the only one held. |
…t 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).
…hing 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.
e0c30ee to
6df415f
Compare
|
Thanks for the precise read — that's exactly right, and I've carried it across the ports rather than leave the drift for you to write blind in Java. Short answer: the fix is now on the branch across all three ports, rebased on current main, with the fixture regenerated. The Java gate should go green on this push. What changed since your review:
On the number case you flagged: I went with unifying it rather than keeping a third wording — Witness: Appreciate you offering to write the Java yourself — glad to have it from a machine that can actually run the gate. |
8 PRs. Rust and .NET batch prediction and router batch APIs (#1358, #1359), the min_confidence abstention gate and jev_confidence in both ports (#1362, #1363), and four inputs refused where they enter instead of failing later (#1365, #1366, #1367, #1368). Verified before tagging: all 92 suites pass, 612 tests. ruff clean on both lists. Java fixture, Unicode table and language table gates clean; three Go tables_gen.go match. Version agrees across pyproject.toml, laya.__version__ and nix/package.nix. README carries 0.4.16 only. Security audit of the merge diff, 180330 added lines over 349 files: zero dynamic exec, zero network calls, zero URLs, zero base64 or obfuscation, zero secrets in the diff or the tracked tree, zero laya-max boundary hits, no writes outside the test temp dir, the dependency surface untouched, no npm install hooks, no binaries, every new file 100644, and 20 of 20 actions SHA-pinned with codeql-action init, autobuild and analyze all on 4.39.1. Held from this release: #1364 (correct in Python but it moves a cross-port contract Decisions.java reproduces deliberately and structured.ts shares), #1324 (real table regression), and seven dependency and action bumps for a pass of their own. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
rahul05ranjan
left a comment
There was a problem hiding this comment.
Ran the new rejection block against this branch's laya/structured.py (harness with the module's own helpers, no other deps): all pass — non-array anyOf/oneOf values (3, 1.5, True, False) raise SchemaError naming the field and echoing the schema, {"anyOf": 3} produces exactly properties.x: unsupported schema {'anyOf': 3}, a real one-non-null-branch union still unwraps to noul, and the pre-existing rejections (free string, empty properties) are unchanged. A null-only branch list still refuses with the arity message, and a falsy anyOf ([]) still falls through to oneOf — the Python truthiness semantics the class note documents survive the new guard.
The Java side mirrors it correctly: union instanceof List matches isinstance(union, list) in the reference, and the fixture updates show Java now reporting the same "unsupported schema" text (including the Python-repr formatting) instead of the old arity complaint — which was misleading anyway, since the request never had branches to count. Dropping the old SchemaException for non-iterables removes the one recorded exception to the message-for-message rule; a uniform rejection is the better trade.
Trace-only note, since I can't build the Java side here: in Decisions.java a truthy-but-not-a-List anyOf (e.g. a non-empty Map) no longer falls through to oneOf and can't reach the branch loop at all. I read that as intentional and it matches the Python ({"anyOf": {...}} → terminal rejection, per the fixture), just flagging it in case a Map-typed anyOf was ever expected to be unwrapped.
24 PRs. Vietnamese kept off the English checkpoint (#1392), deciding-window attribution preserved through end hooks (#1369), RemoteRouter identifying itself with a configurable User-Agent (#1396), ten more inputs refused where they enter (#1372, #1394, #1397, #1398, #1400, #1401, #1402, #1403, #1404, #1405, #1406), per-language temperatures and per-call budgets in .NET and Rust (#1370, #1371), .NET calibration files (#1384), the Go router lifecycle, SHA-256 pins and routed inference (#1374, #1379, #1395), a C++ foundation (#1399, #1407), and parity-lane sharding that replaces the budget headroom added for 0.4.16 (#1373, #1375). Verified before tagging: all 92 suites pass, 640 tests, up from 612. ruff clean on both lists. laya-ts 1251 tests across 44 files, tsc --noEmit clean. Every generated-artifact gate clean on CPython 3.12 / unicodedata 15.0.0: Java 20 families match laya (9 unverified), UnicodeTables matches CPython 15.0.0, LanguageTables matches laya/lang.py, three Go tables_gen.go match, both laya-cpp .inc files match. Version agrees across pyproject.toml, laya.__version__ and nix/package.nix. README carries 0.4.17 only. Security audit of the merge diff, 362766 added lines over 480 files: zero obfuscation, zero secrets, zero boundary hits, zero binaries, no mode or symlink changes, 20 of 20 actions SHA-pinned including the new cpp.yml, npm install hooks unchanged, and no third-party host in any added line (example.com 90, github.com 2, download.pytorch.org 1, docs.rs 1). The 12 dynamic-exec hits are the C++ test harness invoking its own checker through sys.executable, the Go sample generator reading laya's own example by AST at development time, and one comment in rust.yml. Held: #1364 (unchanged, it moves a cross-port contract), #1381 (a #[doc(hidden)] but pub Rust hook that would ship as public API able to replace the forward pass), #1386 (route_batch switched from self.route to self._route, silently bypassing subclass overrides the docstring promises), and nine stacked port PRs that conflict with each other and need linearizing (#1377, #1378, #1380, #1382, #1383, #1385, #1387, #1388, #1389, #1390). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
You did everything I asked and then some, and the mutation arm is the part that makes this reviewable rather than just assertable. I re-ran it all rather than taking it on trust, and every claim holds:
Gating the Java unwrap on On the number case: unifying it was the right call and I am glad you took that rather than preserving a third wording. Three spellings for one fault is how a cross-port contract becomes unmaintainable. Please do add the Timing, so you are not left guessing. 0.4.17 is already built, verified and in flight: tagged off a candidate with all nine lanes green at job level, so I am not going to reopen it to add a PR, because that would discard the verification. This goes in first thing in 0.4.18, as soon as you push the number row. If you would rather ship it as is, say so and I will merge without that row, but my preference is to pin it. Thanks for the patience across two releases on this one. The reason it was held was never your Python change, which was right from the first push. |
|
Merged in 0.4.18. Thank you for carrying this across the ports rather than fixing Python alone.
Your Python tests cover I added one commit on top rather than send it back for a third round. Two things: The cross-port fixture pinned only the shapes that were iterable, a truthy string and a truthy object. A truthy number and a truthy boolean are the ones that leaked The generator comment above those cases described the behaviour your change replaced, and went further: it warned that a port "testing is it a list gets wrong in the opposite direction" by refusing with "unsupported schema". That is now exactly what the reference does, so the comment argued against the implementation it documents. Rewritten to name the four shapes and why each stays pinned. Worth knowing for next time, since it is not obvious from the diff: Verified before merging: 706 tests across 93 suites, |
… iterable #1364 replaced the iterate-and-count path with `isinstance(union, list)`, which is the right guard, but the cross-port fixture only pinned the two shapes that WERE iterable: a truthy string and a truthy object. A truthy number and a truthy boolean are the shapes that leaked `TypeError: 'int' object is not iterable` (`bool` subclasses `int`), and nothing in the Java fixture held either, so a port could regress to the old custom message without a fixture failing. The generator comment described the behaviour #1364 replaced, and argued against testing "is it a list" when that is now exactly what the reference does. Rewritten to say what the four shapes are and why each is pinned. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Twelve pull requests: #1364, #1409, #1410, #1412, #1413, #1414, #1415, #1416, #1417, #1418, #1419, #1420. Test suites: 706 passing across 93 suites, up from 640 across 92. laya-cpp registers 423 ctest tests, up from 213, and its MIN_TESTS floor is raised to match. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
What
laya/structured.py::_fielditerated a property'sanyOf/oneOfunion without first checking it is an array, while theallOfbranch immediately above guards its wrapper withisinstance(wrapped, list)and_enum_fieldguards its enum the same way. A property whose union is a JSON number — or a boolean, which subclasses int — fell straight into the branch comprehension and leakedTypeError; a truthy non-array string/object iterated to zero branches and reached the union-arity refusal... got 0, which blamed an arity the request never had.Now the union is unwrapped only when
isinstance(union, list); any other shape falls to the existing terminalSchemaErrorthat names the path.Per @NandhaKishorM's review, the same refusal is carried across the ports so all three agree:
Decisions.java): the union unwrap is gated onunion instanceof List, and the"'anyOf' must be an array of branches'"exception is removed so a number/boolean/other non-array now falls to the terminalunsupported schemalike the reference.structured.ts): already gates onArray.isArray(union)and falls through tounsupported schema— verified, no change needed.laya-java/fixtures/structured.jsonis regenerated withgen_fixtures.py; exactly the two truthy-non-array rows change.Why
The module's contract is a
SchemaErrornaming the offending path. Every sibling raise honours it —{'enum': 5}→'enum' must be an array of values, got intand{'allOf': 5}→unsupported schema {'allOf': 5}. This one leak was the odd case out, and the old wording was a cross-port contract that Java reproduced deliberately.The maintainer asked whether the number case should keep a third wording; it now joins
unsupported schema, so a non-array union has one message across all three ports.How it was verified
Checklist
docs/structured.mdrejection table already covers theunsupported schemamessage this path raises — verified bytests/test_structured_docs.py)