Skip to content

fix(core): fix Ajv error discriminator #2720

Closed
harshit078 wants to merge 28 commits intoRedocly:mainfrom
harshit078:fix-ajv-validation-discrimator
Closed

fix(core): fix Ajv error discriminator #2720
harshit078 wants to merge 28 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
Touches core response schema validation behavior; while the change is targeted, normalizing schemas could affect edge-case discriminator matching or validation outcomes if the promoted const/enum is ambiguous.

Overview
Fixes AJV discriminator validation crashes by preprocessing dereferenced schemas so discriminator tag properties expose const/enum at the top level even when they’re nested inside allOf (while keeping the original allOf constraints intact).

checkSchema now runs schemas through the new normalizeDiscriminatorSchemas utility before AJV validation, and tests were added/updated to cover oneOf/anyOf discriminators with allOf + not patterns and to ensure failures are reported as normal validation errors (not Ajv error).

Reviewed by Cursor Bugbot for commit 9ce2215. 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: 9ce2215

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/cli Patch
@redocly/openapi-core 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
strictSchema: false,
inlineRefs: false,
validateSchema: false,
discriminator: true,
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

Comment thread packages/respect-core/src/modules/flow-runner/schema/schema-checker.ts Outdated
Comment thread packages/respect-core/src/modules/flow-runner/schema/schema-checker.ts Outdated
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.

Comment thread packages/core/src/rules/ajv.ts Outdated
Comment thread packages/core/src/rules/utils.ts
Comment thread packages/respect-core/src/utils/normalize-discriminator-schemas.ts Outdated
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 2 potential issues.

There are 3 total unresolved issues (including 1 from previous review).

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 c3ac165. Configure here.

Comment thread packages/respect-core/src/utils/normalize-discriminator-schemas.ts
@DmitryAnansky
Copy link
Copy Markdown
Contributor

Hi, @harshit078
Thank you for the time you’ve invested in investigating this issue — it’s greatly appreciated.
It appears to be a more complex problem that may require deeper investigation or even changes in external libraries (such as Ajv). We’ll take it forward and handle it internally from here.
Closing this PR for now.

@harshit078
Copy link
Copy Markdown
Contributor Author

Hey @DmitryAnansky ,
No worries, while resolving the issue I eventually realised that fixing the issue from the root ajv will solve this problem. On digging deep into ajv repo, I found out that we can recursively search for const/enum terms in addMappings function in index.ts under discriminator. This would resolve the problem once for all.

https://github.com/ajv-validator/ajv/blob/master/lib/vocabularies/discriminator/index.ts

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