-
Notifications
You must be signed in to change notification settings - Fork 217
fix(core): fix Ajv error discriminator #2720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harshit078
wants to merge
20
commits into
Redocly:main
Choose a base branch
from
harshit078:fix-ajv-validation-discrimator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f8fd185
feat: add function to remove discriminator keywords from schema
harshit078 e54fc2f
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 636419c
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 c68873f
feat: integrated discriminator removal in schema validation process
harshit078 27817b6
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 e6378dd
feat: added tests for remove Discriminators function
harshit078 e999529
feat: added changeset
harshit078 24df583
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 1bc5103
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 ae9fbf6
fix: update from major to patch
harshit078 0063e79
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 c690a69
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 d7ca08e
fix: cursor comment
harshit078 a149430
revert: old logic of different discriminators
harshit078 18d93c5
fix: updated .md file
harshit078 dd7d0e0
fix: updated schema checker and made discriminator as false
harshit078 8c824f0
fix: updated tests and changeset
harshit078 467a7f4
fix: address cursor commentes
harshit078 f2da64e
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 1bd8c4f
Merge branch 'main' into fix-ajv-validation-discrimator
harshit078 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@redocly/respect-core": patch | ||
| "@redocly/openapi-core": patch | ||
| --- | ||
|
|
||
| Fixed AJV validation error when schemas use a discriminator property with complex constraint patterns like `allOf + not`.AJV's `discriminator: true` option requires every discriminator property to have a direct `const` or `enum`, which is incompatible with valid OpenAPI schemas that use composition patterns. Disabled AJV's discriminator keyword in both the respect schema checker and lint example validation, and removed the silent error swallowing in `validateExample` that was skipping validation for schemas with discriminators. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ const ajvStrict = new Ajv({ | |
| strictSchema: false, | ||
| inlineRefs: false, | ||
| validateSchema: false, | ||
| discriminator: true, | ||
| discriminator: false, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d suggest taking a deeper look into this issue instead of disabling discriminator in Ajv options. It would be worthwhile to review the Ajv implementation and verify how it handles this particular case. |
||
| allowUnionTypes: true, | ||
| validateFormats: true, | ||
| logger: false, | ||
|
|
@@ -79,18 +79,14 @@ function checkSchemaFromDescription({ | |
|
|
||
| if (schemaFromDescription && !isSchemaWithCircularRef) { | ||
| try { | ||
| const processedSchema = removeWriteOnlyProperties( | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| schemaFromDescription as JSONSchemaType<unknown> | ||
| ); | ||
| checks.push({ | ||
| name: CHECKS.SCHEMA_CHECK, | ||
| passed: ajvStrict.validate( | ||
| removeWriteOnlyProperties(schemaFromDescription as JSONSchemaType<unknown>), | ||
| resultBody | ||
| ), | ||
| passed: ajvStrict.validate(processedSchema, resultBody), | ||
| message: ajvStrict.errors | ||
| ? printAjvErrors( | ||
| removeWriteOnlyProperties(schemaFromDescription as JSONSchemaType<unknown>), | ||
| resultBody, | ||
| ajvStrict.errors | ||
| ) | ||
| ? printAjvErrors(processedSchema, resultBody, ajvStrict.errors) | ||
| : '', | ||
| severity: ctx.severity['SCHEMA_CHECK'], | ||
| }); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disabling discriminator breaks oneOf validation for overlapping schemas
High Severity
Setting
discriminator: falseglobally disables AJV's discriminator-based schema selection for alloneOf/anyOfschemas. When enabled, AJV uses the discriminator property value to pick exactly one sub-schema to validate against. When disabled, AJV falls back to trying all sub-schemas and requires exactly one to match foroneOf. For the very common OpenAPI pattern where sub-schemas share overlapping properties and rely on the discriminator to disambiguate, multiple sub-schemas may now match, causingoneOfvalidation to fail on previously valid data. This is a regression for any schema using discriminator with overlappingoneOfbranches.Additional Locations (1)
packages/respect-core/src/modules/flow-runner/schema/schema-checker.ts#L22-L23Reviewed by Cursor Bugbot for commit 1bd8c4f. Configure here.