Skip to content

Commit 8937c46

Browse files
authored
fix: Fix multiple discriminator on same type (#285)
1 parent e517a2e commit 8937c46

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ describe("schemaToTypeAliasDeclaration", () => {
610610
mapping: {
611611
foo: "#/components/schemas/Foo",
612612
bar: "#/components/schemas/Bar",
613+
fiz: "#/components/schemas/Baz",
613614
baz: "#/components/schemas/Baz",
614615
},
615616
},
@@ -650,7 +651,7 @@ describe("schemaToTypeAliasDeclaration", () => {
650651
}) | (Omit<Bar, "discriminatorPropertyName"> & {
651652
discriminatorPropertyName: "bar";
652653
}) | (Omit<Baz, "discriminatorPropertyName"> & {
653-
discriminatorPropertyName: "baz";
654+
discriminatorPropertyName: "fiz" | "baz";
654655
});"
655656
`);
656657
});
@@ -670,7 +671,7 @@ describe("schemaToTypeAliasDeclaration", () => {
670671
}) | (Bar & {
671672
discriminatorPropertyName: "bar";
672673
}) | (Baz & {
673-
discriminatorPropertyName: "baz";
674+
discriminatorPropertyName: "fiz" | "baz";
674675
});"
675676
`);
676677
});
@@ -703,7 +704,10 @@ describe("schemaToTypeAliasDeclaration", () => {
703704
type: "object",
704705
properties: {
705706
baz: { type: "string" },
706-
discriminatorPropertyName: { type: "string", enum: ["baz"] },
707+
discriminatorPropertyName: {
708+
type: "string",
709+
enum: ["fiz", "baz"],
710+
},
707711
},
708712

709713
required: ["discriminatorPropertyName"],

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { pascal } from "case";
2-
import { findKey, get, intersection, merge, omit } from "lodash";
2+
import { keys, pickBy, get, intersection, merge, omit } from "lodash";
33
import {
44
ComponentsObject,
55
DiscriminatorObject,
@@ -332,17 +332,23 @@ const withDiscriminator = (
332332
return node;
333333
}
334334

335-
const discriminatedValue = findKey(
336-
discriminator.mapping,
337-
(i) => i === schema.$ref
335+
const discriminatedValues = keys(
336+
pickBy(discriminator.mapping, (i) => i === schema.$ref)
338337
);
339-
if (discriminatedValue) {
338+
339+
if (discriminatedValues.length > 0) {
340+
const discriminatedValuesType = f.createUnionTypeNode(
341+
discriminatedValues.map((val) =>
342+
f.createLiteralTypeNode(f.createStringLiteral(val))
343+
)
344+
);
345+
340346
const propertyNameAsLiteral = f.createTypeLiteralNode([
341347
f.createPropertySignature(
342348
undefined,
343349
f.createIdentifier(discriminator.propertyName),
344350
undefined,
345-
f.createLiteralTypeNode(f.createStringLiteral(discriminatedValue))
351+
discriminatedValuesType
346352
),
347353
]);
348354

@@ -355,8 +361,11 @@ const withDiscriminator = (
355361
if (
356362
property &&
357363
isSchemaObject(property) &&
358-
property.enum?.length === 1 &&
359-
property.enum[0] === discriminatedValue &&
364+
property.enum &&
365+
property.enum.length === discriminatedValues.length &&
366+
property.enum.every((enumVal) =>
367+
discriminatedValues.includes(enumVal)
368+
) &&
360369
spec.required?.includes(discriminator.propertyName)
361370
) {
362371
return node;

0 commit comments

Comments
 (0)