Skip to content

Fix null enum members in validation error messages - #1374

Merged
stevehu merged 2 commits into
masterfrom
fix/1373-null-enum-message
Oct 30, 2026
Merged

stevehu merged 2 commits into
masterfrom
fix/1373-null-enum-message

Conversation

@stevehu

@stevehu stevehu commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1373.

Render explicit null enum members as null instead of an empty string in validation errors. Avoid appending a second null for nullable schemas that already enumerate it. Enum matching, including typeLoose coercion, retains its existing behavior.

Regression tests cover null at the beginning, middle and end of the enum, nullable enums with and without an explicit null, and valid null input. Existing heterogeneous enum message tests remain unchanged.

Validation: message regressions reproduced before the fix; full mvn -q test passed locally (10,573 tests, 0 failures, 0 errors, 30 skipped).

Scope: master/Jackson 3. The duplicate-null nullable message also exists on 2.x; its backport is deferred to a separate PR.

@stevehu

stevehu commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the fix. I read the diff and the full EnumValidator on this branch, but I did not build it or run the tests. The core change looks correct:

  • isNull() now goes through toString(), so null members render as null.
  • The hasNull guard stops null being listed twice for nullable schemas.
  • ConstValidator is not affected, because it passes a toString() default to asString(...).

Requested change

The last assertion in looseNullEnumUsesNullTextRatherThanEmptyString does not test the loose-mode fix.

assertTrue(registry.getSchema("{\"enum\":[null,\"\"]}")
        .validate("\"\"", InputFormat.JSON).isEmpty());

The input "" is already found by nodes.contains(JsonNodeValue.of("")) in validate(). isTypeLooseContainsInEnum is never reached, so this passes even if prepareLooseValue is reverted. That means "the empty string only matches when explicitly enumerated" is untested. A case that exercises the loose set would fix this. For example, check that "" does not match {"enum":[null,1]} or {"enum":[null]} (the second assertion above already covers the latter), and add a positive case that reaches the loose path.

Questions and minor points

  • Loose-mode behaviour change: with typeLoose, the string "null" now satisfies an enum that has a null member. The PR justifies this as restoring Jackson 2 behaviour, which seems plausible, but it is a user-visible semantic choice. The maintainers should confirm it, and the CHANGELOG entry could say so explicitly.
  • Duplicated literal: the text "null" is now spelled out in asString()'s result, in the nullable-append branch, and in prepareLooseValue. A small constant or helper would keep them in step.
  • Test style: com.networknt.schema.dialect.Dialects is referenced by its fully qualified name in nullableEnumListsNullOnce. An import would match the rest of the class.
  • CHANGELOG: the new entry has a blank line separating it from the other ### Changed bullets, and it uses a plain (#1373) where the entries right below use [#NNNN](url) links. The file mixes both styles, so this is cosmetic.
  • 2.x: the duplicate-null message (nullable schema whose enum already lists null) also occurs on 2.x. Jackson 2's asText() rendered null as "null", and the nullable block then appends a second one. The !hasNull guard and its test may be worth backporting.

@stevehu stevehu changed the title Fix null enum rendering and loose matching with Jackson 3 Fix null enum members in validation error messages Oct 30, 2026
@stevehu

stevehu commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Re-reviewed at 1f692dd. This looks good to me, and CI is green on Java 17, 21 and 25.

  • The Dialects import and the CHANGELOG format are fixed.
  • The vacuous loose-mode assertion is gone along with the loose-mode change.
  • The core fix is unchanged and correct: null renders as null, and null is not listed twice for nullable schemas. The two new tests cover it.

Two follow-ups, neither blocking:

  1. Loose mode on Jackson 3: prepareLooseValue still calls node.asString(), which returns "" for null. With typeLoose enabled, an enum containing null therefore matches the string "" rather than "null", which differs from Jackson 2. Dropping it from this PR is reasonable, but it would be worth tracking in a separate issue so it isn't lost.
  2. 2.x: the duplicate-null message (nullable schema whose enum already lists null) also occurs on 2.x, as noted in the PR.

Nit: the test class now ends with a blank line before the closing }.

@stevehu
stevehu merged commit 498143f into master Oct 30, 2026
4 checks passed
@stevehu
stevehu deleted the fix/1373-null-enum-message branch October 30, 2026 22:37
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.

Enum validator error message renders a null enum member as an empty string instead of null

1 participant