Skip to content

Report keyword errors through the execution context - #1388

Open
TYsewyn wants to merge 7 commits into
networknt:masterfrom
TYsewyn:fix/report-keyword-errors-through-execution-context
Open

TYsewyn wants to merge 7 commits into
networknt:masterfrom
TYsewyn:fix/report-keyword-errors-through-execution-context

Conversation

@TYsewyn

@TYsewyn TYsewyn commented Nov 9, 2026

Copy link
Copy Markdown

OneOfValidator, AnyOfValidator and PropertyNamesValidator added some of their own errors straight to the error list instead of calling ExecutionContext.addError. Those errors skipped fail fast, and an ExecutionContext that overrides addError never saw them.

  • oneOf: the error for zero or several matching branches now goes through addError. With fail fast, a later error no longer replaces it.
  • oneOf and anyOf: the discriminator error for a value that maps to no schema now goes through addError. With fail fast, a later error no longer replaces it. In oneOf the check moves after the branch loop, once the error list is restored, as it already was in anyOf.
  • propertyNames: the property name errors now go through addError once the inner schema has run. The inner schema runs with fail fast off, so fail fast returns the propertyNames error rather than the inner error, such as maxLength. With fail fast, it stops at the first invalid name.

The branch errors that oneOf and anyOf report after their own error were already reported through addError when the branches ran, so they are still added to the list directly.

Tests

Each validator gets two tests, both failing before the change:

  • fail fast returns the keyword's own error, not a later or inner one
  • an ExecutionContext that overrides addError sees every returned error

mvn verify passes: 10,954 tests, 0 failures.

OneOfValidator, AnyOfValidator and PropertyNamesValidator added some of
their own errors straight to the error list instead of calling
ExecutionContext.addError. Those errors skipped fail fast, and an
ExecutionContext that overrides addError never saw them.

- oneOf: the error for zero or several matching branches now goes through
  addError. With fail fast, a later error no longer replaces it.
- oneOf and anyOf: the discriminator error for a value that maps to no
  schema now goes through addError. With fail fast, a later error no longer
  replaces it. In oneOf the check moves after the branch loop, once the
  error list is restored, as it already was in anyOf.
- propertyNames: the property name errors now go through addError once
  the inner schema has run. The inner schema runs with fail fast off, so
  fail fast returns the propertyNames error rather than the inner error,
  such as maxLength. With fail fast, it stops at the first invalid name.

The branch errors that oneOf and anyOf report after their own error were
already reported through addError when the branches ran, so they are still
added to the list directly.
@stevehu

stevehu commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Review of head 3909011.

The issue is real. I ran master and this branch with fail fast on:

  • {"oneOf":[{"type":"string"},{"type":"number"}],"enum":["x"]} with true: master returns only [enum]. The oneOf error is lost and evaluation continues past it. This PR returns [oneOf].
  • {"propertyNames":{"maxLength":3}} with {"foobar":1}: master returns the inner maxLength error at /foobar. This PR returns the propertyNames error.
  • An ExecutionContext that overrides addError did not see these errors on master.

All 8 new tests fail on master and pass here. The full suite passes: 10954 tests, 0 failures. Findings, most-severe first.

1. walk now throws with fail fast (OneOfValidator.java:224)
{"oneOf":[{"type":"string"},{"type":"number"}]} with failFast(true) and schema.walk("true", InputFormat.JSON, true) returns [oneOf] on master. Here it throws FailFastAssertionException. The only catch is in Schema.validate(..., OutputFormat, ...) (Schema.java:1198); the walk entry points have none. type already throws there on master (and so did propertyNames), so the missing catch in walk is the root cause. This PR extends it to oneOf, which worked before. Catching the exception in the walk paths, as validate does, would fix it. No test covers walk.

2. anyOf itself still does not fail fast (AnyOfValidator.java:187)
Only its discriminator error was moved to addError. With {"anyOf":[{"type":"string"},{"type":"number"}],"enum":["x"]} and true, master and this branch both return only [enum], so the anyOf failure is lost just as oneOf's was. With anyOf alone, fail fast returns both branch errors rather than one first error. The branches run with fail fast off, so reporting them through addError earlier does not make fail fast stop. existingErrors.addAll(allErrors) is the same bug class.

3. Discriminated oneOf under fail fast returns fewer errors (OneOfValidator.java:224)
With a discriminator and [{"@type":"Kitchen"}] (Kitchen matched, hasMicrowaveOven missing), master returns [oneOf, required]. This branch returns [oneOf], because addError(message) throws before addAll(discriminatorErrors). anyOf still returns [required], so the two now disagree. The CHANGELOG does not mention it.

4. CHANGELOG (CHANGELOG.md:36)
The entry has no PR or issue link, unlike its neighbours. It also omits point 3 and that the propertyNames error now replaces the inner maxLength or pattern error and its /<property> location.

5. Duplicated test fixtures (AnyOfValidatorTest.java:72)
DISCRIMINATOR_SCHEMA, DISCRIMINATOR_INPUT and the two near-identical tests exist in both AnyOfValidatorTest and OneOfValidatorTest, differing only in oneOf/anyOf. A @ParameterizedTest over the keyword would halve it.

6. Test gaps (PropertyNamesValidatorTest.java:58)
No test covers walk with fail fast, a failing anyOf under fail fast, or more than one invalid property name. The single-property test does not distinguish the new break from a plain loop.

With fail fast on, a keyword error throws FailFastAssertionException.
Schema.validate catches it and returns the error, but the walk entry
points did not, so walk threw instead of returning the result. This
affected type, and now oneOf, whose error goes through addError.

walkAtNodeInternal, which every walk entry point and walkAtNode go
through, now catches it as validate does.
A failing anyOf added its branch errors straight to the error list. Its
branches run with fail fast off, so with fail fast on the anyOf failure
did not stop the evaluation: a later error replaced it, and anyOf alone
returned every branch error rather than the first.

With fail fast on, a failing anyOf now adds its branch errors and throws
FailFastAssertionException with the first one, or the first error of the
branch the discriminator selects, as addError does. The branch errors
already went through addError when the branches ran, so an
ExecutionContext that overrides addError does not see them again.
Without fail fast, the branch errors are added as before.

Issue1276Test characterised the old result, in which anyOf returned
every leaf error even with fail fast, as the BOOLEAN and FLAG formats
turn on. It now expects one error with fail fast for anyOf as for oneOf.
With a discriminator, a failing oneOf reported its own error and then the
errors of the branch the discriminator selects. Now that its own error
goes through addError, fail fast stops there, so it returns only the
oneOf error where it used to return the branch errors too. A failing
anyOf has no error of its own and returns the first error of that branch.
Each keyword returns its first error, so they differ.

Added tests that pin both results, with and without fail fast, and
described the change in the CHANGELOG.
Linked the entry to the pull request, as its neighbours link theirs, and
listed what changes for callers: the propertyNames error now replaces
the error of its inner schema, such as maxLength or pattern, and its
/<property> location; anyOf and walk stop at the first error with fail
fast; and the discriminated oneOf result with fail fast.
… test

AnyOfValidatorTest and OneOfValidatorTest held the same discriminator
schema, inputs and tests, differing only in the keyword. They now live
in AlternativeDiscriminatorTest, which runs each test over oneOf and
anyOf and states the expected errors per keyword where they differ.
Added propertyNames tests for an object with more than one invalid
property name, with and without fail fast, and for walk with fail fast.

With fail fast, propertyNames returns the error of the first invalid name
whether or not it stops checking names, since addError throws on that
error. The test therefore records what reaches an ExecutionContext that
overrides addError: only the first name's maxLength error, which fails
if the inner schema goes on to check the other names.

The walk test for oneOf and the failing anyOf tests came with their
fixes.
@stevehu

stevehu commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Review of head 88fb7d11 (the six commits since 3909011). Full suite passes on JDK 25: 10965 run, 0 failures, 45 skipped. No hard bugs: the anyOf fail-fast, walk and discriminated oneOf points from my last review are handled or documented. I also ran 19 schemas on master and on this head, with and without fail fast. Without fail fast the results are identical. With fail fast, every difference is one the CHANGELOG describes. Remaining points, most-severe first.

1. No test of fail-fast anyOf/oneOf inside if, not, contains (AnyOfValidatorTest.java:68)
The new behavior is only tested at the root or under allOf/items. Nothing covers if, not, contains, dependentSchemas, unevaluated*, propertyNames or $ref. These work today only because each host validator suspends fail fast. If one stopped doing that, a failing if condition would throw out of anyOf and no test would notice. A small parameterized test would pin it.

2. Walk and addError override paths only partly tested (OneOfValidatorTest.java:590)
There is no walk + fail fast test for anyOf (the new throw at AnyOfValidator.java:191) or for a discriminator error. The addError override tests always call super, so a context whose override does not call super, or throws, is not covered.

3. anyOf collects every branch error under fail fast (AnyOfValidator.java:182)
allErrors and discriminatorErrors are still built for every branch, and only branchErrors.get(0) is used. OneOfValidator skips this through reportChildErrors. The same config check would work here.

4. FailFastAssertionException Javadoc (FailFastAssertionException.java:29)
It says the exception is caught in Schema.validate. walk now catches it too, and AnyOfValidator throws it directly.

5. CHANGELOG placement (CHANGELOG.md:36)
walk returning instead of throwing, one anyOf error instead of all, and propertyNames moving from maxLength@/abc to propertyNames@ (probed on {"propertyNames":{"maxLength":2},"required":["q"]}) are caller-visible. They are filed under Fixed; Breaking changes or Changed would be easier for upgraders to spot.

6. Eighth copy of the disable/restore fail-fast block (PropertyNamesValidator.java:45)
If (twice), Not, Contains, UnionType, UnevaluatedProperties, OneOf and AnyOf already have it. A helper on ExecutionContext would remove the duplication, and AnyOf is now the only one that throws by hand.

7. Duplicated fixture (AlternativeDiscriminatorTest.java:37)
The Room/BedRoom/Kitchen schema is already in DiscriminatorValidatorTest and src/test/resources/openapi3/discriminator.json.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants