Skip to content

Fix uniqueItems validation for non-array inputs - #1297

Merged
stevehu merged 2 commits into
networknt:masterfrom
PHJ2000:fix-uniqueitems-array-only-master
Oct 12, 2026
Merged

stevehu merged 2 commits into
networknt:masterfrom
PHJ2000:fix-uniqueitems-array-only-master

Conversation

@PHJ2000

@PHJ2000 PHJ2000 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1294. The 2.x counterpart is #1298.

Only run uniqueItems validation for arrays so repeated object property values do not produce duplicate-item errors.

Regression tests cover all five supported drafts, including not, both oneOf outcomes, nested properties, and walk. Existing array comparisons and duplicate-error counts are unchanged.

Validation:

  • mvn verify on Java 17: 8,693 tests, 0 failures/errors, 30 skipped.
  • Final UniqueItemsApplicabilityTest run after test naming and formatting cleanup: 50 tests passed.

@stevehu

stevehu commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Code review(xhigh · 5 findings)
src/main/java/com/networknt/schema/keyword/UniqueItemsValidator.java
● 56 [correctness] Inside the touched block, every repeated item adds another identical error: same instance location, no arguments, no item index. The output doesn't say which items are duplicated, and an array with k repeats produces k identical errors.
src/test/java/com/networknt/schema/UniqueItemsApplicabilityTest.java
● 127 [test-design] preservesErrorCountForRepeatedArrayElements asserts that ["x","x","x"] produces exactly 2 errors. That locks in the duplicate-error behaviour from the finding above, so a later fix that reports one error per array will look like a regression.
● 144 [test-quality] The last assertion in walkRejectsObjectWhenUniqueItemsIsNegated walks with validation turned off and expects 0 errors. That holds whether or not the fix is present, so it tests nothing about uniqueItems.
● 35 [test-coverage] No test covers type-loose mode. There, sibling array keywords (minItems, maxItems, items in older drafts) treat a non-array as a one-element array, while uniqueItems now skips it. That is consistent (one item is always unique), but nothing guarantees it.
● 35 [test-quality] ignoresNonArrays and stillChecksArrayElements loop over 9 and 4 instances inside one parameterized test. The first failing instance aborts the loop, so the others go unreported.

PR #1297 is correct and ready to merge from a correctness standpoint. I found no bugs in the change and reported 5 findings: one existing behaviour you might want fixed alongside it, and four about the tests. That's below the usual floor of 7 because I didn't find more real issues.

The fix: the one-line node.isArray() guard fixes #1294.

  • It matches how the other array keywords skip non-arrays (contains, items, prefixItems, unevaluatedItems).
  • In type-loose mode, objects now pass uniqueItems, and that is still consistent: minItems/maxItems treat a non-array as a one-item array, and one item is always unique.
  • The 2.x counterpart, [2.x] Fix uniqueItems validation for non-array inputs #1298, has the same one-line change and the same test file.

Verification:

  • On JDK 25 the new test class (50 cases) passes, along with the existing uniqueItems tests, both JSON suites, and the walk tests.
  • With the fix reverted, 35 of the 50 cases fail, so the tests do guard the regression.
  • The not and oneOf cases you asked for in the issue reply are included.

Findings:

  1. Repeated duplicates (existing behaviour): each repeated item adds another identical error with no item index, so ["x","x","x","y","y"] gives 3 identical errors. You offered this as optional in your issue reply, so it can stay out of this PR.
  2. A test locks that in: preservesErrorCountForRepeatedArrayElements asserts the 2-error count, which makes a later one-error fix look like a regression. It also passes without the fix.
  3. A walk assertion tests nothing: the walk with validation off expects 0 errors, which holds with or without the fix.
  4. No type-loose test for an object with repeated values.
  5. Loops inside single tests (9 and 4 instances) stop at the first failure and hide the rest.

None of these blocks merging. If you want one change requested, I'd pick finding 2: relax or drop that test so the duplicate-error cleanup stays easy later.

@PHJ2000

PHJ2000 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I removed the test that fixed the duplicate-error count and the assertion that ran walk with validation disabled. I also added a test across all five supported drafts to check that an object with repeated property values passes uniqueItems in type-loose mode.

These updates are included in both #1297 and #1298. All 50 cases in UniqueItemsApplicabilityTest pass on each branch.

@stevehu

stevehu commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Code review(xhigh · 5 findings)
src/main/java/com/networknt/schema/keyword/UniqueItemsValidator.java
● 56 [correctness] Still open (existing behaviour, deferred by the author): every repeated item in the touched block adds another identical uniqueItems error with the same instance location, no arguments and no item index.
src/test/java/com/networknt/schema/UniqueItemsApplicabilityTest.java
● 35 [test-quality] Still open (not addressed and not mentioned in the reply): ignoresNonArrays (9 instances, lines 37-48) and stillChecksArrayElements (4 instances, lines 64-70) still loop over instances inside one parameterized test.
✓ 133 [test-design] Fixed: preservesErrorCountForRepeatedArrayElements, which asserted that ["x","x","x"] gives exactly 2 errors, was removed in 4ebf40b.
✓ 141 [test-quality] Fixed: the walk assertion with validation disabled, which expected 0 errors whether or not the fix was present, was removed from walkRejectsObjectWhenUniqueItemsIsNegated.
✓ 53 [test-coverage] Fixed: the new ignoresRepeatedObjectValuesInTypeLooseMode test checks that, in type-loose mode, an object with repeated property values passes {"type":"array","uniqueItems":true} on all five drafts.

The author's update (4ebf40b) fixes 3 of our 5 findings and adds no new problems. My sweep of the changed code found nothing new to report.

Resolved

  • The test that locked in the duplicate-error count was removed.
  • The walk assertion that ran with validation off was removed.
  • A type-loose test was added. I checked it on JDK 25: it passes with the fix and all 5 draft cases fail without it.

Still open

  • The repeated-error behaviour is existing behaviour. The author deferred it, which is fine for this PR.
  • The loops inside single tests are unchanged and the reply doesn't mention them. This is a nit and doesn't block merging.

What I ran

  • On the PR head, UniqueItemsApplicabilityTest passes all 50 cases.
  • With the one-line fix reverted, 40 of 50 fail, up from 35 in our first review. The only cases that still pass are the array-only checks, which should pass either way.
  • The 2.x PR [2.x] Fix uniqueItems validation for non-array inputs #1298 has the same fix and test file. The one difference is that its test uses [1,1] instead of [1,1.0], which is correct because 2.x doesn't treat 1 and 1.0 as equal.

The PR is ready to merge. I haven't posted anything on GitHub. Do you want me to add a follow-up comment on #1297 (and #1298) acknowledging the fixes and noting the loop nit as optional?

@stevehu
stevehu merged commit 45c1fc6 into networknt:master Oct 12, 2026
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.

uniqueItems incorrectly rejects objects with duplicate property values

2 participants