File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -205,7 +205,10 @@ export function defaultSchemaObjectTransform(
205
205
const discriminator = ctx . discriminators [ discriminatorRef . $ref ] ;
206
206
let value = parseRef ( path ) . path . pop ( ) as string ;
207
207
if ( discriminator . mapping ) {
208
- const matchedValue = Object . entries ( discriminator . mapping ) . find ( ( [ _ , v ] ) => v === value ) ;
208
+ // Mapping value can either be a fully-qualified ref (#/components/schemas/XYZ) or a schema name (XYZ)
209
+ const matchedValue = Object . entries ( discriminator . mapping ) . find (
210
+ ( [ _ , v ] ) => ( v [ 0 ] !== "#" && v === value ) || ( v [ 0 ] === "#" && parseRef ( v ) . path . pop ( ) === value )
211
+ ) ;
209
212
if ( matchedValue ) value = matchedValue [ 0 ] ; // why was this designed backwards!?
210
213
}
211
214
coreType . unshift ( indent ( `${ discriminator . propertyName } : ${ escStr ( value ) } ;` , indentLv + 1 ) ) ;
Original file line number Diff line number Diff line change @@ -246,6 +246,35 @@ describe("Schema Object", () => {
246
246
number: number;
247
247
}]>` ) ;
248
248
} ) ;
249
+
250
+ test ( "discriminator" , ( ) => {
251
+ const schema : SchemaObject = {
252
+ type : "object" ,
253
+ allOf : [
254
+ { $ref : 'components["schemas"]["parent"]' } ,
255
+ { type : "object" , properties : { string : { type : "string" } } } ,
256
+ ] ,
257
+ } ;
258
+ const generated = transformSchemaObject ( schema , {
259
+ path : options . path ,
260
+ ctx : {
261
+ ...options . ctx ,
262
+ discriminators : {
263
+ 'components["schemas"]["parent"]' : {
264
+ propertyName : "operation" ,
265
+ mapping : {
266
+ test : options . path ,
267
+ } ,
268
+ } ,
269
+ } ,
270
+ } ,
271
+ } ) ;
272
+ expect ( generated ) . toBe ( `{
273
+ operation: "test";
274
+ } & Omit<components["schemas"]["parent"], "operation"> & {
275
+ string?: string;
276
+ }` ) ;
277
+ } ) ;
249
278
} ) ;
250
279
251
280
describe ( "allOf" , ( ) => {
You can’t perform that action at this time.
0 commit comments