Skip to content

Commit 4e61b1d

Browse files
committed
hotfix: skip when mapping is not a mapping ref
1 parent a94bfe2 commit 4e61b1d

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

packages/core/src/__tests__/bundle.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,38 @@ describe('bundle', () => {
490490
expect(problems).toHaveLength(0);
491491
expect(res.parsed).toMatchSnapshot();
492492
});
493+
494+
it('should bundle discriminator with defaultMapping and mapping as component names to the same document', async () => {
495+
const document = outdent`
496+
openapi: 3.2.0
497+
components:
498+
schemas:
499+
Pet:
500+
type: object
501+
discriminator:
502+
propertyName: kind
503+
defaultMapping: Cat
504+
mapping:
505+
cat: Cat
506+
Cat:
507+
type: object
508+
properties:
509+
kind:
510+
type: string
511+
512+
`;
513+
514+
const {
515+
bundle: { parsed },
516+
problems,
517+
} = await bundleFromString({
518+
source: document,
519+
config: await createConfig({}),
520+
});
521+
522+
expect(problems).toMatchInlineSnapshot(`[]`);
523+
expect(parsed).toMatchInlineSnapshot(document);
524+
});
493525
});
494526

495527
describe('bundleFromString', () => {

packages/core/src/bundle/bundle-visitor.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isRef,
88
refBaseName,
99
type Location,
10+
isMappingRef,
1011
} from '../ref-utils.js';
1112
import { type ResolvedRefMap, type Document } from '../resolve.js';
1213
import { reportUnresolvedRef } from '../rules/common/no-unresolved-refs.js';
@@ -208,7 +209,12 @@ export function makeBundleVisitor({
208209
const componentType = mapTypeToComponent('Schema', version)!;
209210
visitor.Discriminator = {
210211
leave(discriminator: Oas3Discriminator, ctx: UserContext) {
211-
if (typeof discriminator.defaultMapping !== 'string') return;
212+
if (
213+
typeof discriminator.defaultMapping !== 'string' ||
214+
!isMappingRef(discriminator.defaultMapping)
215+
) {
216+
return;
217+
}
212218

213219
const resolved = ctx.resolve({ $ref: discriminator.defaultMapping });
214220
if (!resolved.location || resolved.node === undefined) {
@@ -222,6 +228,9 @@ export function makeBundleVisitor({
222228
leave(mapping, ctx) {
223229
for (const name of Object.keys(mapping)) {
224230
const $ref = mapping[name];
231+
if (!isMappingRef($ref)) {
232+
continue;
233+
}
225234
const resolved = ctx.resolve({ $ref });
226235
if (!resolved.location || resolved.node === undefined) {
227236
reportUnresolvedRef(resolved, ctx.report, ctx.location.child(name));

0 commit comments

Comments
 (0)