fix: resolve discriminator defaultMapping into components schemas when bundling#2736
fix: resolve discriminator defaultMapping into components schemas when bundling#2736
Conversation
🦋 Changeset detectedLatest commit: daa0ca6 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 |
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Missing
isMappingRefcheck fordefaultMappingresolution- Added an
isMappingRefguard before resolvingdefaultMappingand covered the plain schema-name case with a unit test so valid OAS 3.2 documents no longer report unresolved refs.
- Added an
Or push these changes by commenting:
@cursor push 49f3370f76
Preview (49f3370f76)
diff --git a/packages/core/src/__tests__/bundle.test.ts b/packages/core/src/__tests__/bundle.test.ts
--- a/packages/core/src/__tests__/bundle.test.ts
+++ b/packages/core/src/__tests__/bundle.test.ts
@@ -302,6 +302,36 @@
expect(problems).toHaveLength(0);
});
+ it('should not resolve discriminator defaultMapping schema names (OAS 3.2)', async () => {
+ const document = outdent`
+ openapi: 3.2.0
+ paths: {}
+ components:
+ schemas:
+ Pet:
+ type: object
+ discriminator:
+ propertyName: kind
+ defaultMapping: Cat
+ Cat:
+ type: object
+ properties:
+ kind:
+ type: string
+ `;
+
+ const {
+ bundle: { parsed },
+ problems,
+ } = await bundleFromString({
+ source: document,
+ config: await createConfig({}),
+ });
+
+ expect(problems).toHaveLength(0);
+ expect((parsed as any).components.schemas.Pet.discriminator.defaultMapping).toBe('Cat');
+ });
+
it('should pull hosted schema', async () => {
const { bundle: res, problems } = await bundle({
config: await createConfig({}),
diff --git a/packages/core/src/bundle/bundle-visitor.ts b/packages/core/src/bundle/bundle-visitor.ts
--- a/packages/core/src/bundle/bundle-visitor.ts
+++ b/packages/core/src/bundle/bundle-visitor.ts
@@ -2,6 +2,7 @@
import { type SpecMajorVersion } from '../oas-types.js';
import {
isAbsoluteUrl,
+ isMappingRef,
replaceRef,
isExternalValue,
isRef,
@@ -208,7 +209,12 @@
const componentType = mapTypeToComponent('Schema', version)!;
visitor.Discriminator = {
leave(discriminator: Oas3Discriminator, ctx: UserContext) {
- if (typeof discriminator.defaultMapping !== 'string') return;
+ if (
+ typeof discriminator.defaultMapping !== 'string' ||
+ !isMappingRef(discriminator.defaultMapping)
+ ) {
+ return;
+ }
const resolved = ctx.resolve({ $ref: discriminator.defaultMapping });
if (!resolved.location || resolved.node === undefined) {This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a94bfe2. Configure here.
Co-authored-by: Jacek Łękawa <164185257+JLekawa@users.noreply.github.com>
4e61b1d to
daa0ca6
Compare
|
📦 A new experimental 🧪 version v0.0.0-snapshot.1776177415 of Redocly CLI has been published for testing. Install with NPM: npm install @redocly/cli@0.0.0-snapshot.1776177415
# or
npm install @redocly/openapi-core@0.0.0-snapshot.1776177415
# or
npm install @redocly/respect-core@0.0.0-snapshot.1776177415 |


What/Why/How?
Fixed an issue where discriminator
defaultMappingwas not resolved when bundling.Reference
Testing
Screenshots (optional)
Check yourself
Security
Note
Low Risk
Low risk: change is scoped to OAS3 bundling behavior for
discriminator.defaultMappingand is covered by new unit and e2e tests, with minimal surface area outside bundling.Overview
Fixes bundling so OAS3
discriminator.defaultMappingis treated likediscriminator.mapping: mapping-like values are now resolved and saved into#/components/schemas/...instead of being left as an unresolved string.Adds regression coverage via a new unit test for component-name mappings and a new OAS 3.2 e2e fixture/snapshot verifying
defaultMappingto an external schema is rewritten to an internal component ref.Reviewed by Cursor Bugbot for commit daa0ca6. Bugbot is set up for automated code reviews on this repo. Configure here.