@@ -8,10 +8,11 @@ import {
88 pointerBaseName ,
99 refBaseName ,
1010 type Location ,
11+ isMappingRef ,
1112} from '../ref-utils.js' ;
1213import { type ResolvedRefMap , type Document } from '../resolve.js' ;
1314import { reportUnresolvedRef } from '../rules/common/no-unresolved-refs.js' ;
14- import { type OasRef } from '../typings/openapi.js' ;
15+ import { type OasRef , type Oas3Discriminator } from '../typings/openapi.js' ;
1516import { dequal } from '../utils/dequal.js' ;
1617import { makeRefId } from '../utils/make-ref-id.js' ;
1718import { type Oas3Visitor , type Oas2Visitor } from '../visitors.js' ;
@@ -205,19 +206,40 @@ export function makeBundleVisitor({
205206 } ;
206207
207208 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- }
209+ const componentType = mapTypeToComponent ( 'Schema' , version ) ! ;
210+ visitor . Discriminator = {
211+ leave ( discriminator : Oas3Discriminator , ctx : UserContext ) {
212+ if (
213+ typeof discriminator . defaultMapping !== 'string' ||
214+ ! isMappingRef ( discriminator . defaultMapping )
215+ ) {
216+ return ;
217+ }
217218
218- const componentType = mapTypeToComponent ( 'Schema' , version ) ! ;
219- mapping [ name ] = saveComponent ( componentType , resolved , ctx ) ;
219+ const resolved = ctx . resolve ( { $ref : discriminator . defaultMapping } ) ;
220+ if ( ! resolved . location || resolved . node === undefined ) {
221+ reportUnresolvedRef ( resolved , ctx . report , ctx . location . child ( 'defaultMapping' ) ) ;
222+ return ;
220223 }
224+
225+ discriminator . defaultMapping = saveComponent ( componentType , resolved , ctx ) ;
226+ } ,
227+ DiscriminatorMapping : {
228+ leave ( mapping , ctx ) {
229+ for ( const name of Object . keys ( mapping ) ) {
230+ const $ref = mapping [ name ] ;
231+ if ( ! isMappingRef ( $ref ) ) {
232+ continue ;
233+ }
234+ const resolved = ctx . resolve ( { $ref } ) ;
235+ if ( ! resolved . location || resolved . node === undefined ) {
236+ reportUnresolvedRef ( resolved , ctx . report , ctx . location . child ( name ) ) ;
237+ return ;
238+ }
239+
240+ mapping [ name ] = saveComponent ( componentType , resolved , ctx ) ;
241+ }
242+ } ,
221243 } ,
222244 } ;
223245 }
0 commit comments