Skip to content

fix(core): fix Ajv error discriminator #2720

Open
harshit078 wants to merge 19 commits intoRedocly:mainfrom
harshit078:fix-ajv-validation-discrimator
Open

fix(core): fix Ajv error discriminator #2720
harshit078 wants to merge 19 commits intoRedocly:mainfrom
harshit078:fix-ajv-validation-discrimator

Conversation

@harshit078
Copy link
Copy Markdown
Contributor

@harshit078 harshit078 commented Apr 6, 2026

What/Why/How?

Reference

Testing

Screenshots (optional)

Check yourself

  • This PR follows the contributing guide
  • All new/updated code is covered by tests
  • Core code changed? - Tested with other Redocly products (internal contributions only)
  • New package installed? - Tested in different environments (browser/node)
  • Documentation update has been considered

Security

  • The security impact of the change has been considered
  • Code follows company security practices and guidelines

Note

Medium Risk
Changes JSON Schema validation behavior by disabling AJV's discriminator keyword, which may alter how some discriminator-based schemas are validated and could surface new example/schema errors that were previously skipped.

Overview
Fixes AJV validation failures for OpenAPI schemas that use discriminator with composition patterns (e.g. allOf + not) by disabling AJV’s discriminator keyword in both the core AJV validator (packages/core/src/rules/ajv.ts) and Respect’s response schema checker (schema-checker.ts).

Removes the special-case error swallowing in validateExample so discriminator-related AJV errors are now reported instead of silently skipping example validation, and updates/adds tests to assert correct pass/fail behavior for these discriminator schemas.

Reviewed by Cursor Bugbot for commit f2da64e. Bugbot is set up for automated code reviews on this repo. Configure here.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 6, 2026

🦋 Changeset detected

Latest commit: f2da64e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@redocly/respect-core Patch
@redocly/openapi-core Patch
@redocly/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@harshit078 harshit078 marked this pull request as ready for review April 8, 2026 12:17
@harshit078 harshit078 requested review from a team as code owners April 8, 2026 12:17
@harshit078 harshit078 changed the title fix: Respect doesn't resolve complex constraint pattern fix(core): Respect doesn't resolve complex constraint pattern Apr 9, 2026
@harshit078 harshit078 changed the title fix(core): Respect doesn't resolve complex constraint pattern fix(core): fix Ajv error discriminator Apr 9, 2026
@DmitryAnansky
Copy link
Copy Markdown
Contributor

@harshit078
Could you please elaborate on why you believe removing discriminators is a good fix for this issue?
Have you considered any alternative approaches?

Comment thread .changeset/sixty-falcons-glow.md Outdated
@@ -0,0 +1,5 @@
---
"@redocly/respect-core": major
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use patch for fixes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure

@harshit078
Copy link
Copy Markdown
Contributor Author

@DmitryAnansky sure,
my intent/logic behind creating a function which removes discriminators is because when I researched about OpenAPI discriminator schema I found out that it serves mainly one purpose which is its optimises routing and act as routing shortcut.
The alternatives I considered before submitting PR were -

  • Selective stripping: this was more complex than intented and had to have many edge cases
  • making discriminator as false : it would have affected globally rather than per schema
  • brute force with simple Catch error and retry: same as first point

This is my POV of issue. If you think approach can be better, I am ready to implement it. Thanks :)

Comment thread packages/respect-core/src/utils/remove-discriminators-from-schema.ts Outdated
Comment thread packages/respect-core/src/utils/remove-discriminators-from-schema.ts Outdated
Comment thread packages/respect-core/src/utils/remove-discriminators-from-schema.ts Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @harshit078 for the contribution. You don't need to remove discriminator manually, you can just drop this setting from the Ajv and it will not cause the error. The issue looks more deeply. Could you please investigate the root cause?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure, I'll look into it

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8c824f0. Configure here.

inlineRefs: false,
validateSchema: false,
discriminator: true,
discriminator: false,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

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