Skip to content

Commit daa0ca6

Browse files
committed
hotfix: skip when mapping is not a mapping ref
1 parent b432217 commit daa0ca6

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
@@ -555,6 +555,38 @@ describe('bundle', () => {
555555
expect(problems).toHaveLength(0);
556556
expect(res.parsed).toMatchSnapshot();
557557
});
558+
559+
it('should bundle discriminator with defaultMapping and mapping as component names to the same document', async () => {
560+
const document = outdent`
561+
openapi: 3.2.0
562+
components:
563+
schemas:
564+
Pet:
565+
type: object
566+
discriminator:
567+
propertyName: kind
568+
defaultMapping: Cat
569+
mapping:
570+
cat: Cat
571+
Cat:
572+
type: object
573+
properties:
574+
kind:
575+
type: string
576+
577+
`;
578+
579+
const {
580+
bundle: { parsed },
581+
problems,
582+
} = await bundleFromString({
583+
source: document,
584+
config: await createConfig({}),
585+
});
586+
587+
expect(problems).toMatchInlineSnapshot(`[]`);
588+
expect(parsed).toMatchInlineSnapshot(document);
589+
});
558590
});
559591

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

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
pointerBaseName,
99
refBaseName,
1010
type Location,
11+
isMappingRef,
1112
} from '../ref-utils.js';
1213
import { type ResolvedRefMap, type Document } from '../resolve.js';
1314
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)