fix(core): fix Ajv error discriminator #2720
fix(core): fix Ajv error discriminator #2720harshit078 wants to merge 13 commits intoRedocly:mainfrom
Conversation
🦋 Changeset detectedLatest commit: d7ca08e The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
.changeset/sixty-falcons-glow.md
Outdated
| @@ -0,0 +1,5 @@ | |||
| --- | |||
| "@redocly/respect-core": major | |||
There was a problem hiding this comment.
Please use patch for fixes.
|
@DmitryAnansky sure,
This is my POV of issue. If you think approach can be better, I am ready to implement it. Thanks :) |
packages/respect-core/src/utils/remove-discriminators-from-schema.ts
Outdated
Show resolved
Hide resolved
|
|
||
| if ('discriminator' in node) { | ||
| delete node.discriminator; | ||
| } |
There was a problem hiding this comment.
Strips any key named "discriminator" not just OpenAPI keyword
Medium Severity
stripDiscriminators removes every key named discriminator from every nested object, not just the OpenAPI discriminator keyword from Schema Objects. When a schema has a data property literally named discriminator inside properties (or $defs, definitions, patternProperties, etc.), the function deletes that property definition, silently corrupting the schema used for validation. The check needs to distinguish the OpenAPI discriminator keyword (which is a sibling of oneOf/anyOf and contains propertyName) from regular map entries.
Reviewed by Cursor Bugbot for commit c690a69. Configure here.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit d7ca08e. Configure here.
|
|
||
| stripDiscriminators(schema); | ||
| return schema; | ||
| } |
There was a problem hiding this comment.
Function mutates input, failing its own non-mutation test
High Severity
removeDiscriminatorsFromSchema mutates its input in-place via delete node.discriminator in stripDiscriminators, but the test "should not mutate the original schema" asserts that the original object retains its discriminator property after the call. This test will fail. The function needs to deep-clone the schema before stripping, or the test expectation is wrong. In the current call site the mutation is masked because removeWriteOnlyProperties already deep-clones, but the function's contract is broken.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit d7ca08e. Configure here.
| strictSchema: false, | ||
| inlineRefs: false, | ||
| validateSchema: false, | ||
| discriminator: true, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
sure, I'll look into it


What/Why/How?
remove-discriminators-from-schemawhich just strips discriminator keywords from schemasReference
Testing
Screenshots (optional)
Check yourself
Security
Note
Medium Risk
Touches response schema validation by preprocessing schemas before Ajv, which could change validation outcomes for discriminator-based schemas if the discriminator keyword was previously relied on during validation. Risk is mitigated by added tests covering the reported
allOf+notdiscriminator pattern and existing validation paths.Overview
Fixes a crash in
checkSchemawhen Ajv is configured withdiscriminator: truebut the OpenAPI discriminator property uses valid constraint compositions likeallOf+not.This preprocesses the dereferenced response schema by removing
discriminatorkeywords (via newremoveDiscriminatorsFromSchema) before running Ajv, and adds regression tests for the discriminator/constraint pattern plus the new utility.Reviewed by Cursor Bugbot for commit d7ca08e. Bugbot is set up for automated code reviews on this repo. Configure here.