Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mean-ghosts-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"swagger-typescript-api": patch
---

Ensure enums are at the top of the components to avoid issue on recursive schema parsing.
2 changes: 2 additions & 0 deletions src/code-gen-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export class CodeGenProcess {
}),
);

this.schemaComponentsMap.enumsFirst();

const componentsToParse: SchemaComponent[] =
this.schemaComponentsMap.filter(
lodash.compact([
Expand Down
9 changes: 9 additions & 0 deletions src/schema-components-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ export class SchemaComponentsMap {
get($ref: string) {
return this._data.find((c) => c.$ref === $ref) || null;
}

// Ensure enums are at the top of components list
enumsFirst() {
this._data.sort((a, b) => {
if (Object.keys(a.rawTypeData || {}).includes("enum")) return -1;
if (Object.keys(b.rawTypeData || {}).includes("enum")) return 1;
return 0;
});
}
}
24 changes: 12 additions & 12 deletions tests/spec/discriminator/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ exports[`basic > discriminator 1`] = `
* ---------------------------------------------------------------
*/

export enum PetEnum {
Dog = "dog",
Lizard = "lizard",
Cat = "cat",
}

export enum BlockDTOEnum {
Csv = "csv",
File = "file",
Kek = "kek",
}

export type SimpleDiscriminator = SimpleObject | ComplexObject;

export interface SimpleObject {
Expand All @@ -23,12 +35,6 @@ export interface ComplexObject {
objectType: string;
}

export enum BlockDTOEnum {
Csv = "csv",
File = "file",
Kek = "kek",
}

export type BlockDTOWithEnum = BaseBlockDtoWithEnum &
(
| BaseBlockDtoWithEnumTypeMapping<BlockDTOEnum.Csv, CsvBlockWithEnumDTO>
Expand Down Expand Up @@ -93,12 +99,6 @@ export type Lizard = BasePet & {
lovesRocks?: boolean;
};

export enum PetEnum {
Dog = "dog",
Lizard = "lizard",
Cat = "cat",
}

export type PetWithEnum = BasePetWithEnum &
(
| BasePetWithEnumPetTypeMapping<PetEnum.Dog, DogWithEnum>
Expand Down
64 changes: 32 additions & 32 deletions tests/spec/enumNamesAsValues/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@ exports[`basic > --enum-names-as-values 1`] = `
* ---------------------------------------------------------------
*/

export type TestAllOfDc = (FooBarBaz & FooBar) & {
prop?: string;
};

export type TestAllOfDc2 = FooBarBaz & {
prop?: string;
};
export enum JobKind {
COMPANY = "COMPANY",
PERSONAL = "PERSONAL",
FREELANCE = "FREELANCE",
OPEN_SOURCE = "OPEN_SOURCE",
}

export type TestAnyOfDc = (FooBarBaz | FooBar) & {
prop?: string;
};
export enum PetIdsWithWrongEnum {
Value10 = 10,
Value20 = 20,
Value30 = 30,
Value40 = 40,
}

export type TestOneOfDc = (FooBarBaz | FooBar) & {
prop?: string;
};
export enum PetIds {
Value10 = 10,
Value20 = 20,
Value30 = 30,
Value40 = 40,
}

/**
* FooBar
Expand All @@ -46,19 +51,21 @@ export enum IntEnumWithNames {
BooFar = "BooFar",
}

export enum PetIds {
Value10 = 10,
Value20 = 20,
Value30 = 30,
Value40 = 40,
}
export type TestAllOfDc = (FooBarBaz & FooBar) & {
prop?: string;
};

export enum PetIdsWithWrongEnum {
Value10 = 10,
Value20 = 20,
Value30 = 30,
Value40 = 40,
}
export type TestAllOfDc2 = FooBarBaz & {
prop?: string;
};

export type TestAnyOfDc = (FooBarBaz | FooBar) & {
prop?: string;
};

export type TestOneOfDc = (FooBarBaz | FooBar) & {
prop?: string;
};

/** Information about job */
export interface FooBarBaz {
Expand Down Expand Up @@ -91,13 +98,6 @@ export type OmitIdUserType = OmitUserTypeIdOrId;

export type AuthUserType = OmitIdUserType;

export enum JobKind {
COMPANY = "COMPANY",
PERSONAL = "PERSONAL",
FREELANCE = "FREELANCE",
OPEN_SOURCE = "OPEN_SOURCE",
}

/** Information about job */
export interface JobType {
id: string;
Expand Down
Loading