Skip to content

Commit 661799a

Browse files
author
rcatoio
committed
set discriminators types before other types than enums
1 parent e4fa687 commit 661799a

File tree

4 files changed

+103
-91
lines changed

4 files changed

+103
-91
lines changed

src/code-gen-process.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ export class CodeGenProcess {
127127
}),
128128
);
129129

130+
//set all discriminators at the top
131+
this.schemaComponentsMap.discriminatorsFirst();
132+
// put all enums at the top (before discriminators)
130133
this.schemaComponentsMap.enumsFirst();
131134

132135
const componentsToParse: SchemaComponent[] =

src/schema-components-map.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ export class SchemaComponentsMap {
7777
return 0;
7878
});
7979
}
80+
81+
// Ensure discriminators are at the top of components list
82+
discriminatorsFirst() {
83+
this._data.sort((a, b) => {
84+
if (Object.keys(a.rawTypeData || {}).includes("discriminator")) return -1;
85+
if (Object.keys(b.rawTypeData || {}).includes("discriminator")) return 1;
86+
return 0;
87+
});
88+
}
8089
}

tests/spec/discriminator/__snapshots__/basic.test.ts.snap

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,74 @@ export enum BlockDTOEnum {
2525
Kek = "kek",
2626
}
2727
28+
/** kek pek */
29+
export type Variant =
30+
| ({
31+
type: "update";
32+
} & VariantUpdate)
33+
| ({
34+
type: "undo";
35+
} & VariantUndo)
36+
| ({
37+
type: "rollback";
38+
} & VariantRollback)
39+
| ({
40+
type: "scale";
41+
} & VariantScale)
42+
| ({
43+
type: "resources";
44+
} & VariantResources)
45+
| ({
46+
type: "firewall";
47+
} & VariantFirewall)
48+
| ({
49+
type: "gateway";
50+
} & VariantGateway);
51+
52+
export type InvalidDiscriminatorPropertyName =
53+
BaseInvalidDiscriminatorPropertyName &
54+
(
55+
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"num", number>
56+
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"str", string>
57+
);
58+
59+
export type PetWithEnum = BasePetWithEnum &
60+
(
61+
| BasePetWithEnumPetTypeMapping<PetEnum.Dog, DogWithEnum>
62+
| BasePetWithEnumPetTypeMapping<PetEnum.Cat, CatWithEnum>
63+
| BasePetWithEnumPetTypeMapping<PetEnum.Lizard, LizardWithEnum>
64+
);
65+
66+
export type PetOnlyDiscriminator =
67+
| ({
68+
pet_type: "dog";
69+
} & Dog)
70+
| ({
71+
pet_type: "cat";
72+
} & Cat)
73+
| ({
74+
pet_type: "lizard";
75+
} & Lizard);
76+
77+
export type Pet = BasePet &
78+
(
79+
| BasePetPetTypeMapping<"dog", Dog>
80+
| BasePetPetTypeMapping<"cat", Cat>
81+
| BasePetPetTypeMapping<"lizard", Lizard>
82+
);
83+
84+
export type BlockDTO = BaseBlockDto &
85+
(
86+
| BaseBlockDtoTypeMapping<"csv", CsvBlockDTO>
87+
| BaseBlockDtoTypeMapping<"file", FileBlockDTO>
88+
);
89+
90+
export type BlockDTOWithEnum = BaseBlockDtoWithEnum &
91+
(
92+
| BaseBlockDtoWithEnumTypeMapping<BlockDTOEnum.Csv, CsvBlockWithEnumDTO>
93+
| BaseBlockDtoWithEnumTypeMapping<BlockDTOEnum.File, FileBlockWithEnumDTO>
94+
);
95+
2896
export type SimpleDiscriminator = SimpleObject | ComplexObject;
2997
3098
export interface SimpleObject {
@@ -35,12 +103,6 @@ export interface ComplexObject {
35103
objectType: string;
36104
}
37105
38-
export type BlockDTOWithEnum = BaseBlockDtoWithEnum &
39-
(
40-
| BaseBlockDtoWithEnumTypeMapping<BlockDTOEnum.Csv, CsvBlockWithEnumDTO>
41-
| BaseBlockDtoWithEnumTypeMapping<BlockDTOEnum.File, FileBlockWithEnumDTO>
42-
);
43-
44106
export type CsvBlockWithEnumDTO = BaseBlockDtoWithEnum & {
45107
type: BlockDTOEnum.Csv;
46108
text: string;
@@ -51,12 +113,6 @@ export type FileBlockWithEnumDTO = BaseBlockDtoWithEnum & {
51113
fileId: string;
52114
};
53115
54-
export type BlockDTO = BaseBlockDto &
55-
(
56-
| BaseBlockDtoTypeMapping<"csv", CsvBlockDTO>
57-
| BaseBlockDtoTypeMapping<"file", FileBlockDTO>
58-
);
59-
60116
export type CsvBlockDTO = BaseBlockDto & {
61117
/** @default "csv" */
62118
type: "csv";
@@ -69,24 +125,6 @@ export type FileBlockDTO = BaseBlockDto & {
69125
fileId: string;
70126
};
71127
72-
export type Pet = BasePet &
73-
(
74-
| BasePetPetTypeMapping<"dog", Dog>
75-
| BasePetPetTypeMapping<"cat", Cat>
76-
| BasePetPetTypeMapping<"lizard", Lizard>
77-
);
78-
79-
export type PetOnlyDiscriminator =
80-
| ({
81-
pet_type: "dog";
82-
} & Dog)
83-
| ({
84-
pet_type: "cat";
85-
} & Cat)
86-
| ({
87-
pet_type: "lizard";
88-
} & Lizard);
89-
90128
export type Cat = BasePet & {
91129
name?: string;
92130
};
@@ -99,13 +137,6 @@ export type Lizard = BasePet & {
99137
lovesRocks?: boolean;
100138
};
101139
102-
export type PetWithEnum = BasePetWithEnum &
103-
(
104-
| BasePetWithEnumPetTypeMapping<PetEnum.Dog, DogWithEnum>
105-
| BasePetWithEnumPetTypeMapping<PetEnum.Cat, CatWithEnum>
106-
| BasePetWithEnumPetTypeMapping<PetEnum.Lizard, LizardWithEnum>
107-
);
108-
109140
export type CatWithEnum = BasePetWithEnum & {
110141
name?: string;
111142
};
@@ -118,37 +149,6 @@ export type LizardWithEnum = BasePetWithEnum & {
118149
lovesRocks?: boolean;
119150
};
120151
121-
export type InvalidDiscriminatorPropertyName =
122-
BaseInvalidDiscriminatorPropertyName &
123-
(
124-
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"num", number>
125-
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"str", string>
126-
);
127-
128-
/** kek pek */
129-
export type Variant =
130-
| ({
131-
type: "update";
132-
} & VariantUpdate)
133-
| ({
134-
type: "undo";
135-
} & VariantUndo)
136-
| ({
137-
type: "rollback";
138-
} & VariantRollback)
139-
| ({
140-
type: "scale";
141-
} & VariantScale)
142-
| ({
143-
type: "resources";
144-
} & VariantResources)
145-
| ({
146-
type: "firewall";
147-
} & VariantFirewall)
148-
| ({
149-
type: "gateway";
150-
} & VariantGateway);
151-
152152
/** Proposal to change firewall rules for deployment. */
153153
export interface VariantFirewall {
154154
/** asdasdasdasdasdsad added to deployment. If not set, no rules are added. */
@@ -196,21 +196,18 @@ export interface VariantRollback {
196196
/** asdasdasdasdasdn */
197197
export type VariantUndo = object;
198198
199-
interface BaseBlockDtoWithEnum {
200-
title: string;
201-
type: BlockDTOEnum;
202-
}
199+
type BaseInvalidDiscriminatorPropertyName = object;
203200
204-
type BaseBlockDtoWithEnumTypeMapping<Key, Type> = {
205-
type: Key;
201+
type BaseInvalidDiscriminatorPropertyNameTypeMapping<Key, Type> = {
202+
"@type": Key;
206203
} & Type;
207204
208-
interface BaseBlockDto {
209-
title: string;
205+
interface BasePetWithEnum {
206+
pet_type: PetEnum;
210207
}
211208
212-
type BaseBlockDtoTypeMapping<Key, Type> = {
213-
type: Key;
209+
type BasePetWithEnumPetTypeMapping<Key, Type> = {
210+
pet_type: Key;
214211
} & Type;
215212
216213
interface BasePet {
@@ -221,18 +218,21 @@ type BasePetPetTypeMapping<Key, Type> = {
221218
pet_type: Key;
222219
} & Type;
223220
224-
interface BasePetWithEnum {
225-
pet_type: PetEnum;
221+
interface BaseBlockDto {
222+
title: string;
226223
}
227224
228-
type BasePetWithEnumPetTypeMapping<Key, Type> = {
229-
pet_type: Key;
225+
type BaseBlockDtoTypeMapping<Key, Type> = {
226+
type: Key;
230227
} & Type;
231228
232-
type BaseInvalidDiscriminatorPropertyName = object;
229+
interface BaseBlockDtoWithEnum {
230+
title: string;
231+
type: BlockDTOEnum;
232+
}
233233
234-
type BaseInvalidDiscriminatorPropertyNameTypeMapping<Key, Type> = {
235-
"@type": Key;
234+
type BaseBlockDtoWithEnumTypeMapping<Key, Type> = {
235+
type: Key;
236236
} & Type;
237237
"
238238
`;

tests/spec/enumNotFirstInComponents/__snapshots__/basic.test.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export enum ExampleEnum {
1818
Example2 = "Example2",
1919
}
2020
21+
export type ExampleObject = BaseExampleObject &
22+
(
23+
| BaseExampleObjectTypeMapping<ExampleEnum.Example1, DtoExample1>
24+
| BaseExampleObjectTypeMapping<ExampleEnum.Example2, DtoExample2>
25+
);
26+
2127
export interface DtoExample1 {
2228
name: string;
2329
age: number;
@@ -29,12 +35,6 @@ export interface DtoExample2 {
2935
description: string;
3036
}
3137
32-
export type ExampleObject = BaseExampleObject &
33-
(
34-
| BaseExampleObjectTypeMapping<ExampleEnum.Example1, DtoExample1>
35-
| BaseExampleObjectTypeMapping<ExampleEnum.Example2, DtoExample2>
36-
);
37-
3838
interface BaseExampleObject {
3939
type?: ExampleEnum;
4040
}

0 commit comments

Comments
 (0)