Skip to content

Fix stack overflow compiling/validating oneOf with many variants - #2644

Open
momomuchu wants to merge 1 commit into
ajv-validator:masterfrom
momomuchu:fix/oneof-stack-overflow-2641
Open

Fix stack overflow compiling/validating oneOf with many variants#2644
momomuchu wants to merge 1 commit into
ajv-validator:masterfrom
momomuchu:fix/oneof-stack-overflow-2641

Conversation

@momomuchu

Copy link
Copy Markdown

fixes #2641

oneOf emitted one nested if/else per variant, so both codegen rendering
(If.render) and the generated validator recursed O(N) deep. That threw
RangeError for oneOf arrays with ~2000+ variants (compile crashes past ~3000).

This flattens the emitted code to one if per variant at the same nesting level
instead of nesting each variant inside the previous one's else. valid/passing
track "exactly one matched so far" the same way as before, and mergeEvaluated
still runs only on the first match, so error output (passingSchemas,
unevaluatedProperties) and short-circuit behavior are unchanged. Ran the full
oneOf test file plus the JSON-Schema-Test-Suite, all green, with byte-identical
generated error objects vs current master across draft-07/2019-09/2020-12.

Left the generic If.render recursion (the codegen-level fix the issue also
suggests) out of scope here since it touches every keyword's codegen, not just
oneOf. Happy to look at that separately if useful.

Emit a flat sequence of if blocks instead of an O(N)-deep nested else
chain, which overflowed the call stack when compiling and running large
oneOf arrays (ajv-validator#2641). Only merge evaluated properties
for the first matching variant so a later, conflict-causing match does
not swallow a legitimate unevaluatedProperties error, matching the stock
nested-else behaviour. Raise the timeout on the large-array tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

oneOf with ~2000+ variants causes "Maximum call stack size exceeded"

2 participants