Skip to content

Commit 447ef09

Browse files
authored
feat: Extend useEnums to all schema files (#223)
Co-authored-by: Schwager, Sandro <[email protected]>
1 parent fda377e commit 447ef09

File tree

3 files changed

+230
-64
lines changed

3 files changed

+230
-64
lines changed

plugins/typescript/src/fixtures/petstore.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,16 @@ export const petstore: OpenAPIObject = {
243243
type: "string",
244244
},
245245
},
246+
petFilterParam: {
247+
name: "petFilter",
248+
in: "query",
249+
required: false,
250+
schema: {
251+
description: "Filter by type",
252+
type: "string",
253+
enum: ["cat", "dog"],
254+
},
255+
},
246256
},
247257
requestBodies: {
248258
updatePetRequest: {
@@ -255,6 +265,17 @@ export const petstore: OpenAPIObject = {
255265
},
256266
required: true,
257267
},
268+
searchPetRequest: {
269+
content: {
270+
"application/json": {
271+
schema: {
272+
type: "string",
273+
enum: ["cat", "dog"],
274+
},
275+
},
276+
},
277+
required: true,
278+
},
258279
},
259280
responses: {
260281
NotModified: {
@@ -270,6 +291,17 @@ export const petstore: OpenAPIObject = {
270291
},
271292
},
272293
},
294+
PetTypeResponse: {
295+
description: "Type of pet",
296+
content: {
297+
"application/json": {
298+
schema: {
299+
type: "string",
300+
enum: ["cat", "dog"],
301+
},
302+
},
303+
},
304+
},
273305
},
274306
schemas: {
275307
Pet: {

plugins/typescript/src/generators/generateSchemaTypes.test.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ describe("generateSchemaTypes", () => {
326326
export type NotModified = void;
327327
328328
export type PetResponse = Schemas.Pet;
329+
330+
export type PetTypeResponse = "cat" | "dog";
329331
"
330332
`);
331333
});
@@ -356,6 +358,8 @@ describe("generateSchemaTypes", () => {
356358
import type * as Schemas from "./swaggerPetstoreSchemas";
357359
358360
export type UpdatePetRequest = Schemas.NewPet;
361+
362+
export type SearchPetRequest = "cat" | "dog";
359363
"
360364
`);
361365
});
@@ -386,6 +390,122 @@ describe("generateSchemaTypes", () => {
386390
* Unique identifier
387391
*/
388392
export type IdParam = string;
393+
394+
/**
395+
* Filter by type
396+
*/
397+
export type PetFilterParam = "cat" | "dog";
398+
"
399+
`);
400+
});
401+
402+
it("should generate the responses file with enums instead of string unions", async () => {
403+
const writeFile = jest.fn();
404+
const readFile = jest.fn(() => Promise.resolve(""));
405+
await generateSchemaTypes(
406+
{
407+
openAPIDocument: petstore,
408+
writeFile,
409+
readFile,
410+
existsFile: () => true,
411+
},
412+
{
413+
filenameCase: "camel",
414+
useEnums: true,
415+
},
416+
);
417+
expect(writeFile.mock.calls[1][0]).toBe("swaggerPetstoreResponses.ts");
418+
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
419+
"/**
420+
* Generated by @openapi-codegen
421+
*
422+
* @version 1.0.0
423+
*/
424+
import type * as Schemas from "./swaggerPetstoreSchemas";
425+
426+
export enum PetTypeResponse {
427+
cat = "cat",
428+
dog = "dog"
429+
}
430+
431+
export type NotModified = void;
432+
433+
export type PetResponse = Schemas.Pet;
434+
"
435+
`);
436+
});
437+
438+
it("should generate the request bodies file with enums instead of string unions", async () => {
439+
const writeFile = jest.fn();
440+
const readFile = jest.fn(() => Promise.resolve(""));
441+
await generateSchemaTypes(
442+
{
443+
openAPIDocument: petstore,
444+
writeFile,
445+
readFile,
446+
existsFile: () => true,
447+
},
448+
{
449+
filenameCase: "camel",
450+
useEnums: true,
451+
},
452+
);
453+
expect(writeFile.mock.calls[2][0]).toBe(
454+
"swaggerPetstoreRequestBodies.ts",
455+
);
456+
expect(writeFile.mock.calls[2][1]).toMatchInlineSnapshot(`
457+
"/**
458+
* Generated by @openapi-codegen
459+
*
460+
* @version 1.0.0
461+
*/
462+
import type * as Schemas from "./swaggerPetstoreSchemas";
463+
464+
export enum SearchPetRequest {
465+
cat = "cat",
466+
dog = "dog"
467+
}
468+
469+
export type UpdatePetRequest = Schemas.NewPet;
470+
"
471+
`);
472+
});
473+
474+
it("should generate the parameters file with enums instead of string unions", async () => {
475+
const writeFile = jest.fn();
476+
const readFile = jest.fn(() => Promise.resolve(""));
477+
478+
await generateSchemaTypes(
479+
{
480+
openAPIDocument: petstore,
481+
writeFile,
482+
readFile,
483+
existsFile: () => true,
484+
},
485+
{
486+
filenameCase: "camel",
487+
useEnums: true,
488+
},
489+
);
490+
expect(writeFile.mock.calls[3][0]).toBe("swaggerPetstoreParameters.ts");
491+
expect(writeFile.mock.calls[3][1]).toMatchInlineSnapshot(`
492+
"/**
493+
* Generated by @openapi-codegen
494+
*
495+
* @version 1.0.0
496+
*/
497+
/**
498+
* Filter by type
499+
*/
500+
export enum PetFilterParam {
501+
cat = "cat",
502+
dog = "dog"
503+
}
504+
505+
/**
506+
* Unique identifier
507+
*/
508+
export type IdParam = string;
389509
"
390510
`);
391511
});

0 commit comments

Comments
 (0)