diff --git a/src/transform/schema-object.ts b/src/transform/schema-object.ts index b814fac75..b3ba4d948 100644 --- a/src/transform/schema-object.ts +++ b/src/transform/schema-object.ts @@ -199,7 +199,10 @@ export function defaultSchemaObjectTransform( const discriminator = ctx.discriminators[discriminatorRef.$ref]; let value = parseRef(path).path.pop() as string; if (discriminator.mapping) { - const matchedValue = Object.entries(discriminator.mapping).find(([_, v]) => v === value); + // Mapping value can either be a fully-qualified ref (#/components/schemas/XYZ) or a schema name (XYZ) + const matchedValue = Object.entries(discriminator.mapping).find( + ([_, v]) => (v[0] !== "#" && v === value) || (v[0] === "#" && parseRef(v).path.pop() === value) + ); if (matchedValue) value = matchedValue[0]; // why was this designed backwards!? } coreType.unshift(indent(`${discriminator.propertyName}: ${escStr(value)};`, indentLv + 1)); diff --git a/test/schema-object.test.ts b/test/schema-object.test.ts index 3dda108fa..08b32f25b 100644 --- a/test/schema-object.test.ts +++ b/test/schema-object.test.ts @@ -233,6 +233,35 @@ describe("Schema Object", () => { number: number; }]>`); }); + + test("discriminator", () => { + const schema: SchemaObject = { + type: "object", + allOf: [ + { $ref: 'components["schemas"]["parent"]' }, + { type: "object", properties: { string: { type: "string" } } }, + ], + }; + const generated = transformSchemaObject(schema, { + path: options.path, + ctx: { + ...options.ctx, + discriminators: { + 'components["schemas"]["parent"]': { + propertyName: "operation", + mapping: { + test: options.path, + }, + }, + }, + }, + }); + expect(generated).toBe(`{ + operation: "test"; +} & Omit & { + string?: string; +}`); + }); }); describe("allOf", () => {