Skip to content

Commit 7b4d9a7

Browse files
committed
fix: resolve discriminator defaultMapping into components schemas when bundling
1 parent 955c91c commit 7b4d9a7

7 files changed

Lines changed: 92 additions & 12 deletions

File tree

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '../ref-utils.js';
1111
import { type ResolvedRefMap, type Document } from '../resolve.js';
1212
import { reportUnresolvedRef } from '../rules/common/no-unresolved-refs.js';
13-
import { type OasRef } from '../typings/openapi.js';
13+
import { type OasRef, type Oas3Discriminator } from '../typings/openapi.js';
1414
import { dequal } from '../utils/dequal.js';
1515
import { isTruthy } from '../utils/is-truthy.js';
1616
import { makeRefId } from '../utils/make-ref-id.js';
@@ -205,19 +205,32 @@ export function makeBundleVisitor({
205205
};
206206

207207
if (version === 'oas3') {
208-
visitor.DiscriminatorMapping = {
209-
leave(mapping: Record<string, string>, ctx: UserContext) {
210-
for (const name of Object.keys(mapping)) {
211-
const $ref = mapping[name];
212-
const resolved = ctx.resolve({ $ref });
213-
if (!resolved.location || resolved.node === undefined) {
214-
reportUnresolvedRef(resolved, ctx.report, ctx.location.child(name));
215-
return;
216-
}
208+
const componentType = mapTypeToComponent('Schema', version)!;
209+
visitor.Discriminator = {
210+
leave(discriminator: Oas3Discriminator, ctx: UserContext) {
211+
if (typeof discriminator.defaultMapping !== 'string') return;
217212

218-
const componentType = mapTypeToComponent('Schema', version)!;
219-
mapping[name] = saveComponent(componentType, resolved, ctx);
213+
const resolved = ctx.resolve({ $ref: discriminator.defaultMapping });
214+
if (!resolved.location || resolved.node === undefined) {
215+
reportUnresolvedRef(resolved, ctx.report, ctx.location.child('defaultMapping'));
216+
return;
220217
}
218+
219+
discriminator.defaultMapping = saveComponent(componentType, resolved, ctx);
220+
},
221+
DiscriminatorMapping: {
222+
leave(mapping, ctx) {
223+
for (const name of Object.keys(mapping)) {
224+
const $ref = mapping[name];
225+
const resolved = ctx.resolve({ $ref });
226+
if (!resolved.location || resolved.node === undefined) {
227+
reportUnresolvedRef(resolved, ctx.report, ctx.location.child(name));
228+
return;
229+
}
230+
231+
mapping[name] = saveComponent(componentType, resolved, ctx);
232+
}
233+
},
221234
},
222235
};
223236
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
allOf:
2+
- $ref: ./Used.yaml
3+
- type: object
4+
properties:
5+
test:
6+
type: string
7+
const: foo
8+
required:
9+
- test
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type: object
2+
properties:
3+
base:
4+
type: string
5+
discriminator:
6+
propertyName: test
7+
defaultMapping: ./Foo.yaml
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openapi: 3.2.0
2+
info: {}
3+
servers: []
4+
paths:
5+
/:
6+
$ref: paths/_.yaml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
get:
2+
responses:
3+
'200':
4+
content:
5+
application/json:
6+
schema:
7+
$ref: ../components/schemas/Used.yaml
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
apis:
2+
main:
3+
root: ./openapi.yaml
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: 3.2.0
2+
info: {}
3+
servers: []
4+
paths:
5+
/:
6+
get:
7+
responses:
8+
'200':
9+
content:
10+
application/json:
11+
schema:
12+
$ref: '#/components/schemas/Used'
13+
components:
14+
schemas:
15+
Used:
16+
type: object
17+
properties:
18+
base:
19+
type: string
20+
discriminator:
21+
propertyName: test
22+
defaultMapping: '#/components/schemas/Foo'
23+
Foo:
24+
allOf:
25+
- $ref: '#/components/schemas/Used'
26+
- type: object
27+
properties:
28+
test:
29+
type: string
30+
const: foo
31+
required:
32+
- test
33+
34+
bundling openapi.yaml using configuration for api 'main'...
35+
📦 Created a bundle for openapi.yaml at stdout <test>ms.

0 commit comments

Comments
 (0)