Skip to content

oneOf and discriminator value mismatch doesnot fail#1232

Closed
stevehu wants to merge 1 commit intomasterfrom
issue1225
Closed

oneOf and discriminator value mismatch doesnot fail#1232
stevehu wants to merge 1 commit intomasterfrom
issue1225

Conversation

@stevehu
Copy link
Copy Markdown
Contributor

@stevehu stevehu commented Mar 8, 2026

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug (issue 1225) where oneOf validation with a discriminator mapping incorrectly succeeded when the discriminator-indicated schema failed validation but a different, non-discriminated schema happened to pass. The fix adds a new assertion in OneOfValidator to detect this mismatch and report a discriminator error.

Changes:

  • Added a new condition in OneOfValidator.validate() to fail validation when the discriminator-mapped schema has errors but exactly one other schema passes, treating this as a discriminator mismatch.
  • Updated the existing test discriminatorOneOfOneMatchWrongDiscriminatorShouldFail (renamed from ...ShouldSucceed) in OpenApi31Test to verify the fix using the petstore schema.
  • Added a new test oneOfDiscriminatorEnabledWithDiscriminatorMismatch in DiscriminatorValidatorTest to verify the fix with an inline schema using explicit discriminator mappings.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/main/java/com/networknt/schema/keyword/OneOfValidator.java Core fix: adds discriminator mismatch detection when the mapped schema fails but another schema passes in oneOf
src/test/java/com/networknt/schema/oas/OpenApi31Test.java Updates existing test to expect failure and verify discriminator error keyword
src/test/java/com/networknt/schema/DiscriminatorValidatorTest.java Adds new test for discriminator value mismatch with explicit mapping

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@stevehu
Copy link
Copy Markdown
Contributor Author

stevehu commented Mar 8, 2026

After the fix, it breaks an existing test case. I am not sure if this fix is correct or not. Please pay more attention before merging this PR. Thanks.

@justin-tay
Copy link
Copy Markdown
Collaborator

The spec at https://spec.openapis.org/oas/v3.1.1.html#discriminator-object states:

Note that discriminator MUST NOT change the validation outcome of the schema.

So this change likely goes against the spec. Removing or adding the discriminator in a schema shouldn't change the validation outcome.

@stevehu
Copy link
Copy Markdown
Contributor Author

stevehu commented Mar 9, 2026

Based on the comment from @justin-tay, we should drop this PR as the current behaviour is correct.

@stevehu stevehu closed this Mar 9, 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.

3 participants