Skip to content

fix: validate discriminated oneOf schemas against only the branch selected by the discriminator in the drift command#2957

Open
sobanieca-redocly wants to merge 4 commits into
mainfrom
worktree-fix-drift-issues
Open

fix: validate discriminated oneOf schemas against only the branch selected by the discriminator in the drift command#2957
sobanieca-redocly wants to merge 4 commits into
mainfrom
worktree-fix-drift-issues

Conversation

@sobanieca-redocly

@sobanieca-redocly sobanieca-redocly commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

What/Why/How?

Fix for handling oneOf schemas with a discriminator inside drift command. Extended list of well known headers from 3rd party providers.

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 drift reporting for body/schema and header checks; incorrect discriminator handling could hide real issues, though loose schemas keep the prior fallback path.

Overview
Fixes false-positive schema-consistency findings when payloads use oneOf with a discriminator. SchemaValidator turns on Ajv’s discriminator: true so traffic is checked against the branch implied by the discriminator value, not every branch. Schemas Ajv cannot compile for discriminator rules still use a second Ajv instance (discriminator: false) so behavior matches the old “validate all branches” approach.

Separately, the drift command’s default undocumented-header allowlist grows: x-amzn-*, x-github-*, and x-hub-signature / x-hub-signature-256 (replacing a single x-amzn-trace-id entry with the prefix rule). Tests cover both areas.

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

…ected by the discriminator in the drift command
@changeset-bot

changeset-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: df2eb9a

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

This PR includes changesets to release 3 packages
Name Type
@redocly/cli Patch
@redocly/openapi-core Patch
@redocly/respect-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

@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 72.4% (🎯 69%) 8733 / 12061
🔵 Statements 72.08% (🎯 69%) 9085 / 12604
🔵 Functions 77.09% (🎯 73%) 1730 / 2244
🔵 Branches 63.87% (🎯 61%) 5880 / 9205
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/cli/src/commands/drift/engine/schema-validator.ts 78.08% 64.81% 91.66% 77.77% 19, 33, 39, 45, 87, 91, 100, 164, 172-179, 201-209
packages/cli/src/commands/drift/utils/http.ts 55.67% 47.14% 76.47% 55.67% 81, 91-92, 97, 103-112, 117, 121-124, 137, 143, 149-184, 196, 202, 229-241, 260, 277
Generated in workflow #10819 for commit df2eb9a by the Vitest Coverage Report Action

@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Performance Benchmark (Lower is Faster)

CLI Version Bundle Lint Check Config
cli-latest ▓ 1.01x ± 0.01 ▓ 1.01x ± 0.01 ▓ 1.00x (Fastest)
cli-next ▓ 1.00x (Fastest) ▓ 1.00x (Fastest) ▓ 1.00x ± 0.01

@sobanieca-redocly

Copy link
Copy Markdown
Contributor Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit ce416ab. Configure here.

@sobanieca-redocly
sobanieca-redocly marked this pull request as ready for review July 22, 2026 14:22
@sobanieca-redocly
sobanieca-redocly requested review from a team as code owners July 22, 2026 14:22
Comment thread .changeset/drift-infra-header-ignores.md Outdated
Comment thread .changeset/drift-discriminator-validation.md Outdated
Co-authored-by: Jacek Łękawa <164185257+JLekawa@users.noreply.github.com>
// Ajv rejects loosely defined discriminators at compile time (for example
// when the tag property has no `const`/`enum` or is not required); such
// schemas fall back to being validated against every oneOf branch.
this.fallbackAjv = new AjvConstructor({ ...ajvOptions, 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.

How does the lint handle this case? i cant find any fallback

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.

Lint doesn't have a fallback — it just reports that Ajv couldn't compile the schema (the try/catch is in validateExample in packages/core/src/rules/utils.ts), which is fair there, since it points to an issue in the OAD itself. Drift is a different story. Lint only compiles a schema when it has an example attached, so even a spec with no lint issues can contain a loose discriminator. Without the fallback, drift would then report "Schema compilation failed" for every exchange hitting that schema — errors about perfectly valid traffic. The fallback is used in compileSchema() at the bottom of this file: if the strict instance throws, the schema is recompiled with discriminator: false, which validates against every oneOf branch — same as drift did before this PR.

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