Skip to content

Commit e16bc82

Browse files
authored
fix!: Generated enum should be in pascal case (#282)
* Add prettier in the testing flow * Fix enum generation
1 parent ea21f34 commit e16bc82

File tree

7 files changed

+2303
-1749
lines changed

7 files changed

+2303
-1749
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ describe("schemaToTypeAliasDeclaration", () => {
1212
};
1313

1414
expect(printSchema(schema)).toMatchInlineSnapshot(`
15-
"export enum Test {
16-
AVAILABLE = "AVAILABLE",
17-
PENDING = "PENDING",
18-
SOLD = "SOLD"
19-
}"
15+
"export enum Test {
16+
Available = "AVAILABLE",
17+
Pending = "PENDING",
18+
Sold = "SOLD"
19+
}"
2020
`);
2121
});
2222

@@ -28,9 +28,9 @@ describe("schemaToTypeAliasDeclaration", () => {
2828

2929
expect(printSchema(schema)).toMatchInlineSnapshot(`
3030
"export enum Test {
31-
ONE = 1,
32-
TWO = 2,
33-
THREE = 3
31+
One = 1,
32+
Two = 2,
33+
Three = 3
3434
}"
3535
`);
3636
});
@@ -43,13 +43,13 @@ describe("schemaToTypeAliasDeclaration", () => {
4343

4444
expect(printSchema(schema)).toMatchInlineSnapshot(`
4545
"export enum Test {
46-
ZERO = 0,
47-
SEVEN = 7,
48-
FIFTEEN = 15,
49-
ONE_HUNDRED = 100,
50-
ONE_THOUSAND = 1000,
51-
ONE_THOUSAND_FOUR_HUNDRED_FIFTY_SIX = 1456,
52-
THREE_THOUSAND_TWO_HUNDRED_SEVENTEEN = 3217
46+
Zero = 0,
47+
Seven = 7,
48+
Fifteen = 15,
49+
OneHundred = 100,
50+
OneThousand = 1000,
51+
OneThousandFourHundredFiftySix = 1456,
52+
ThreeThousandTwoHundredSeventeen = 3217
5353
}"
5454
`);
5555
});
@@ -68,17 +68,17 @@ describe("schemaToTypeAliasDeclaration", () => {
6868
`);
6969
});
7070

71-
it("should generate uppercase type when providing lowercase schema names", () => {
71+
it("should generate valid enum with values that contains spaces", () => {
7272
const schema: SchemaObject = {
7373
type: "string",
74-
enum: ["AVAILABLE", "PENDING", "SOLD"],
74+
enum: ["saimois", "bengal", "british shorthair"],
7575
};
7676

7777
expect(printSchema(schema, "test")).toMatchInlineSnapshot(`
7878
"export enum Test {
79-
AVAILABLE = "AVAILABLE",
80-
PENDING = "PENDING",
81-
SOLD = "SOLD"
79+
Saimois = "saimois",
80+
Bengal = "bengal",
81+
BritishShorthair = "british shorthair"
8282
}"
8383
`);
8484
});

plugins/typescript/src/core/schemaToEnumDeclaration.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ function getEnumMembers(schema: SchemaObject): ts.EnumMember[] {
5252
throw new Error(`Unsupported enum value type: ${typeof enumValue}`);
5353
}
5454

55-
return f.createEnumMember(f.createIdentifier(enumName), enumValueNode);
55+
return f.createEnumMember(
56+
f.createIdentifier(pascal(enumName)),
57+
enumValueNode
58+
);
5659
});
5760
}
Lines changed: 147 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { describe, expect, it, vi } from "vitest";
1+
import { describe, expect, it } from "vitest";
22
import { set } from "lodash";
33
import { OpenAPIObject } from "openapi3-ts";
44
import { Config, generateFetchers } from "./generateFetchers";
5+
import { createWriteFileMock } from "../testUtils";
56

67
const config: Config = {
78
filenamePrefix: "petstore",
@@ -59,7 +60,7 @@ describe("generateFetchers", () => {
5960
};
6061

6162
it("should generate fetchers", async () => {
62-
const writeFile = vi.fn();
63+
const writeFile = createWriteFileMock();
6364

6465
await generateFetchers(
6566
{
@@ -73,37 +74,45 @@ describe("generateFetchers", () => {
7374

7475
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
7576
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
76-
"/**
77-
* Generated by @openapi-codegen
78-
*
79-
* @version 1.0.0
80-
*/
81-
import type * as Fetcher from "./petstoreFetcher";
82-
import { petstoreFetch } from "./petstoreFetcher";
83-
import type * as Schemas from "./petstoreSchemas";
84-
import type * as Responses from "./petstoreResponses";
85-
import type { ServerErrorStatus } from "./petstoreUtils";
86-
87-
export type ListPetsError = Fetcher.ErrorWrapper<{
88-
status: 404;
89-
payload: Responses.NotFoundError;
90-
} | {
91-
status: ServerErrorStatus;
92-
payload: Schemas.Error;
93-
}>;
94-
95-
export type ListPetsResponse = Schemas.Pet[];
96-
97-
/**
98-
* Get all the pets
99-
*/
100-
export const listPets = (signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
101-
"
77+
"/**
78+
* Generated by @openapi-codegen
79+
*
80+
* @version 1.0.0
81+
*/
82+
import type * as Fetcher from "./petstoreFetcher";
83+
import { petstoreFetch } from "./petstoreFetcher";
84+
import type * as Schemas from "./petstoreSchemas";
85+
import type * as Responses from "./petstoreResponses";
86+
import type { ServerErrorStatus } from "./petstoreUtils";
87+
88+
export type ListPetsError = Fetcher.ErrorWrapper<
89+
| {
90+
status: 404;
91+
payload: Responses.NotFoundError;
92+
}
93+
| {
94+
status: ServerErrorStatus;
95+
payload: Schemas.Error;
96+
}
97+
>;
98+
99+
export type ListPetsResponse = Schemas.Pet[];
100+
101+
/**
102+
* Get all the pets
103+
*/
104+
export const listPets = (signal?: AbortSignal) =>
105+
petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
106+
url: "/pets",
107+
method: "get",
108+
signal,
109+
});
110+
"
102111
`);
103112
});
104113

105114
it("should generate fetchers without prefix", async () => {
106-
const writeFile = vi.fn();
115+
const writeFile = createWriteFileMock();
107116

108117
await generateFetchers(
109118
{
@@ -117,37 +126,45 @@ describe("generateFetchers", () => {
117126

118127
expect(writeFile.mock.calls[1][0]).toBe("components.ts");
119128
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
120-
"/**
121-
* Generated by @openapi-codegen
122-
*
123-
* @version 1.0.0
124-
*/
125-
import type * as Fetcher from "./fetcher";
126-
import { fetch } from "./fetcher";
127-
import type * as Schemas from "./petstoreSchemas";
128-
import type * as Responses from "./petstoreResponses";
129-
import type { ServerErrorStatus } from "./utils";
130-
131-
export type ListPetsError = Fetcher.ErrorWrapper<{
132-
status: 404;
133-
payload: Responses.NotFoundError;
134-
} | {
135-
status: ServerErrorStatus;
136-
payload: Schemas.Error;
137-
}>;
138-
139-
export type ListPetsResponse = Schemas.Pet[];
140-
141-
/**
142-
* Get all the pets
143-
*/
144-
export const listPets = (signal?: AbortSignal) => fetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
145-
"
129+
"/**
130+
* Generated by @openapi-codegen
131+
*
132+
* @version 1.0.0
133+
*/
134+
import type * as Fetcher from "./fetcher";
135+
import { fetch } from "./fetcher";
136+
import type * as Schemas from "./petstoreSchemas";
137+
import type * as Responses from "./petstoreResponses";
138+
import type { ServerErrorStatus } from "./utils";
139+
140+
export type ListPetsError = Fetcher.ErrorWrapper<
141+
| {
142+
status: 404;
143+
payload: Responses.NotFoundError;
144+
}
145+
| {
146+
status: ServerErrorStatus;
147+
payload: Schemas.Error;
148+
}
149+
>;
150+
151+
export type ListPetsResponse = Schemas.Pet[];
152+
153+
/**
154+
* Get all the pets
155+
*/
156+
export const listPets = (signal?: AbortSignal) =>
157+
fetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
158+
url: "/pets",
159+
method: "get",
160+
signal,
161+
});
162+
"
146163
`);
147164
});
148165

149166
it("should generate fetcher with injected props", async () => {
150-
const writeFile = vi.fn();
167+
const writeFile = createWriteFileMock();
151168

152169
await generateFetchers(
153170
{
@@ -168,39 +185,48 @@ describe("generateFetchers", () => {
168185

169186
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
170187
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
171-
"/**
172-
* Generated by @openapi-codegen
173-
*
174-
* @version 1.0.0
175-
*/
176-
import type * as Fetcher from "./petstoreFetcher";
177-
import { petstoreFetch, PetstoreFetcherExtraProps } from "./petstoreFetcher";
178-
import type * as Schemas from "./petstoreSchemas";
179-
import type * as Responses from "./petstoreResponses";
180-
import type { ServerErrorStatus } from "./petstoreUtils";
181-
182-
export type ListPetsError = Fetcher.ErrorWrapper<{
183-
status: 404;
184-
payload: Responses.NotFoundError;
185-
} | {
186-
status: ServerErrorStatus;
187-
payload: Schemas.Error;
188-
}>;
189-
190-
export type ListPetsResponse = Schemas.Pet[];
191-
192-
export type ListPetsVariables = PetstoreFetcherExtraProps;
193-
194-
/**
195-
* Get all the pets
196-
*/
197-
export const listPets = (variables: ListPetsVariables, signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", ...variables, signal });
198-
"
188+
"/**
189+
* Generated by @openapi-codegen
190+
*
191+
* @version 1.0.0
192+
*/
193+
import type * as Fetcher from "./petstoreFetcher";
194+
import { petstoreFetch, PetstoreFetcherExtraProps } from "./petstoreFetcher";
195+
import type * as Schemas from "./petstoreSchemas";
196+
import type * as Responses from "./petstoreResponses";
197+
import type { ServerErrorStatus } from "./petstoreUtils";
198+
199+
export type ListPetsError = Fetcher.ErrorWrapper<
200+
| {
201+
status: 404;
202+
payload: Responses.NotFoundError;
203+
}
204+
| {
205+
status: ServerErrorStatus;
206+
payload: Schemas.Error;
207+
}
208+
>;
209+
210+
export type ListPetsResponse = Schemas.Pet[];
211+
212+
export type ListPetsVariables = PetstoreFetcherExtraProps;
213+
214+
/**
215+
* Get all the pets
216+
*/
217+
export const listPets = (variables: ListPetsVariables, signal?: AbortSignal) =>
218+
petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
219+
url: "/pets",
220+
method: "get",
221+
...variables,
222+
signal,
223+
});
224+
"
199225
`);
200226
});
201227

202228
it("should generate fetcher with operations by tag", async () => {
203-
const writeFile = vi.fn();
229+
const writeFile = createWriteFileMock();
204230

205231
const openAPIDocumentWithTags = set(
206232
openAPIDocument,
@@ -220,34 +246,42 @@ describe("generateFetchers", () => {
220246

221247
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
222248
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
223-
"/**
224-
* Generated by @openapi-codegen
225-
*
226-
* @version 1.0.0
227-
*/
228-
import type * as Fetcher from "./petstoreFetcher";
229-
import { petstoreFetch } from "./petstoreFetcher";
230-
import type * as Schemas from "./petstoreSchemas";
231-
import type * as Responses from "./petstoreResponses";
232-
import type { ServerErrorStatus } from "./petstoreUtils";
233-
234-
export type ListPetsError = Fetcher.ErrorWrapper<{
235-
status: 404;
236-
payload: Responses.NotFoundError;
237-
} | {
238-
status: ServerErrorStatus;
239-
payload: Schemas.Error;
240-
}>;
241-
242-
export type ListPetsResponse = Schemas.Pet[];
243-
244-
/**
245-
* Get all the pets
246-
*/
247-
export const listPets = (signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
248-
249-
export const operationsByTag = { "pets": { listPets } };
250-
"
249+
"/**
250+
* Generated by @openapi-codegen
251+
*
252+
* @version 1.0.0
253+
*/
254+
import type * as Fetcher from "./petstoreFetcher";
255+
import { petstoreFetch } from "./petstoreFetcher";
256+
import type * as Schemas from "./petstoreSchemas";
257+
import type * as Responses from "./petstoreResponses";
258+
import type { ServerErrorStatus } from "./petstoreUtils";
259+
260+
export type ListPetsError = Fetcher.ErrorWrapper<
261+
| {
262+
status: 404;
263+
payload: Responses.NotFoundError;
264+
}
265+
| {
266+
status: ServerErrorStatus;
267+
payload: Schemas.Error;
268+
}
269+
>;
270+
271+
export type ListPetsResponse = Schemas.Pet[];
272+
273+
/**
274+
* Get all the pets
275+
*/
276+
export const listPets = (signal?: AbortSignal) =>
277+
petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
278+
url: "/pets",
279+
method: "get",
280+
signal,
281+
});
282+
283+
export const operationsByTag = { pets: { listPets } };
284+
"
251285
`);
252286
});
253287
});

0 commit comments

Comments
 (0)