Skip to content

Commit e477b58

Browse files
RoXuSsmorimoto
andauthored
fix: Ensure enums are at the top of the components to avoid issue on recursive schema parsing (#1163)
* fix: Ensure enums are at the top of the components to avoid issue on recursive schema parsing --------- Signed-off-by: Sora Morimoto <sora@morimoto.io> Co-authored-by: Sora Morimoto <sora@morimoto.io>
1 parent f5736ac commit e477b58

File tree

15 files changed

+636
-184
lines changed

15 files changed

+636
-184
lines changed

.changeset/mean-ghosts-post.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Ensure enums are at the top of the components to avoid issue on recursive schema parsing.

src/code-gen-process.ts

+2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ export class CodeGenProcess {
127127
}),
128128
);
129129

130+
this.schemaComponentsMap.enumsFirst();
131+
130132
const componentsToParse: SchemaComponent[] =
131133
this.schemaComponentsMap.filter(
132134
lodash.compact([

src/schema-components-map.ts

+9
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,13 @@ export class SchemaComponentsMap {
6868
get($ref: string) {
6969
return this._data.find((c) => c.$ref === $ref) || null;
7070
}
71+
72+
// Ensure enums are at the top of components list
73+
enumsFirst() {
74+
this._data.sort((a, b) => {
75+
if (Object.keys(a.rawTypeData || {}).includes("enum")) return -1;
76+
if (Object.keys(b.rawTypeData || {}).includes("enum")) return 1;
77+
return 0;
78+
});
79+
}
7180
}

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ exports[`basic > discriminator 1`] = `
1313
* ---------------------------------------------------------------
1414
*/
1515
16+
export enum PetEnum {
17+
Dog = "dog",
18+
Lizard = "lizard",
19+
Cat = "cat",
20+
}
21+
22+
export enum BlockDTOEnum {
23+
Csv = "csv",
24+
File = "file",
25+
Kek = "kek",
26+
}
27+
1628
export type SimpleDiscriminator = SimpleObject | ComplexObject;
1729
1830
export interface SimpleObject {
@@ -23,12 +35,6 @@ export interface ComplexObject {
2335
objectType: string;
2436
}
2537
26-
export enum BlockDTOEnum {
27-
Csv = "csv",
28-
File = "file",
29-
Kek = "kek",
30-
}
31-
3238
export type BlockDTOWithEnum = BaseBlockDtoWithEnum &
3339
(
3440
| BaseBlockDtoWithEnumTypeMapping<BlockDTOEnum.Csv, CsvBlockWithEnumDTO>
@@ -93,12 +99,6 @@ export type Lizard = BasePet & {
9399
lovesRocks?: boolean;
94100
};
95101
96-
export enum PetEnum {
97-
Dog = "dog",
98-
Lizard = "lizard",
99-
Cat = "cat",
100-
}
101-
102102
export type PetWithEnum = BasePetWithEnum &
103103
(
104104
| BasePetWithEnumPetTypeMapping<PetEnum.Dog, DogWithEnum>

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

+32-32
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@ exports[`basic > --enum-names-as-values 1`] = `
1313
* ---------------------------------------------------------------
1414
*/
1515
16-
export type TestAllOfDc = (FooBarBaz & FooBar) & {
17-
prop?: string;
18-
};
19-
20-
export type TestAllOfDc2 = FooBarBaz & {
21-
prop?: string;
22-
};
16+
export enum JobKind {
17+
COMPANY = "COMPANY",
18+
PERSONAL = "PERSONAL",
19+
FREELANCE = "FREELANCE",
20+
OPEN_SOURCE = "OPEN_SOURCE",
21+
}
2322
24-
export type TestAnyOfDc = (FooBarBaz | FooBar) & {
25-
prop?: string;
26-
};
23+
export enum PetIdsWithWrongEnum {
24+
Value10 = 10,
25+
Value20 = 20,
26+
Value30 = 30,
27+
Value40 = 40,
28+
}
2729
28-
export type TestOneOfDc = (FooBarBaz | FooBar) & {
29-
prop?: string;
30-
};
30+
export enum PetIds {
31+
Value10 = 10,
32+
Value20 = 20,
33+
Value30 = 30,
34+
Value40 = 40,
35+
}
3136
3237
/**
3338
* FooBar
@@ -46,19 +51,21 @@ export enum IntEnumWithNames {
4651
BooFar = "BooFar",
4752
}
4853
49-
export enum PetIds {
50-
Value10 = 10,
51-
Value20 = 20,
52-
Value30 = 30,
53-
Value40 = 40,
54-
}
54+
export type TestAllOfDc = (FooBarBaz & FooBar) & {
55+
prop?: string;
56+
};
5557
56-
export enum PetIdsWithWrongEnum {
57-
Value10 = 10,
58-
Value20 = 20,
59-
Value30 = 30,
60-
Value40 = 40,
61-
}
58+
export type TestAllOfDc2 = FooBarBaz & {
59+
prop?: string;
60+
};
61+
62+
export type TestAnyOfDc = (FooBarBaz | FooBar) & {
63+
prop?: string;
64+
};
65+
66+
export type TestOneOfDc = (FooBarBaz | FooBar) & {
67+
prop?: string;
68+
};
6269
6370
/** Information about job */
6471
export interface FooBarBaz {
@@ -91,13 +98,6 @@ export type OmitIdUserType = OmitUserTypeIdOrId;
9198
9299
export type AuthUserType = OmitIdUserType;
93100
94-
export enum JobKind {
95-
COMPANY = "COMPANY",
96-
PERSONAL = "PERSONAL",
97-
FREELANCE = "FREELANCE",
98-
OPEN_SOURCE = "OPEN_SOURCE",
99-
}
100-
101101
/** Information about job */
102102
export interface JobType {
103103
id: string;

0 commit comments

Comments
 (0)