Skip to content

Commit

Permalink
feat: Extend useEnums to all schema files
Browse files Browse the repository at this point in the history
  • Loading branch information
sschw committed May 2, 2024
1 parent 7b7904b commit 9a4d362
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 64 deletions.
32 changes: 32 additions & 0 deletions plugins/typescript/src/fixtures/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ export const petstore: OpenAPIObject = {
type: "string",
},
},
petFilterParam: {
name: "petFilter",
in: "query",
required: false,
schema: {
description: "Filter by type",
type: "string",
enum: ["cat", "dog"],
},
},
},
requestBodies: {
updatePetRequest: {
Expand All @@ -255,6 +265,17 @@ export const petstore: OpenAPIObject = {
},
required: true,
},
searchPetRequest: {
content: {
"application/json": {
schema: {
type: "string",
enum: ["cat", "dog"],
},
},
},
required: true,
},
},
responses: {
NotModified: {
Expand All @@ -270,6 +291,17 @@ export const petstore: OpenAPIObject = {
},
},
},
PetTypeResponse: {
description: "Type of pet",
content: {
"application/json": {
schema: {
type: "string",
enum: ["cat", "dog"],
},
},
},
},
},
schemas: {
Pet: {
Expand Down
120 changes: 120 additions & 0 deletions plugins/typescript/src/generators/generateSchemaTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ describe("generateSchemaTypes", () => {
export type NotModified = void;
export type PetResponse = Schemas.Pet;
export type PetTypeResponse = "cat" | "dog";
"
`);
});
Expand Down Expand Up @@ -356,6 +358,8 @@ describe("generateSchemaTypes", () => {
import type * as Schemas from "./swaggerPetstoreSchemas";
export type UpdatePetRequest = Schemas.NewPet;
export type SearchPetRequest = "cat" | "dog";
"
`);
});
Expand Down Expand Up @@ -386,6 +390,122 @@ describe("generateSchemaTypes", () => {
* Unique identifier
*/
export type IdParam = string;
/**
* Filter by type
*/
export type PetFilterParam = "cat" | "dog";
"
`);
});

it("should generate the responses file with enums instead of string unions", async () => {
const writeFile = jest.fn();
const readFile = jest.fn(() => Promise.resolve(""));
await generateSchemaTypes(
{
openAPIDocument: petstore,
writeFile,
readFile,
existsFile: () => true,
},
{
filenameCase: "camel",
useEnums: true,
},
);
expect(writeFile.mock.calls[1][0]).toBe("swaggerPetstoreResponses.ts");
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Schemas from "./swaggerPetstoreSchemas";
export enum PetTypeResponse {
cat = "cat",
dog = "dog"
}
export type NotModified = void;
export type PetResponse = Schemas.Pet;
"
`);
});

it("should generate the request bodies file with enums instead of string unions", async () => {
const writeFile = jest.fn();
const readFile = jest.fn(() => Promise.resolve(""));
await generateSchemaTypes(
{
openAPIDocument: petstore,
writeFile,
readFile,
existsFile: () => true,
},
{
filenameCase: "camel",
useEnums: true,
},
);
expect(writeFile.mock.calls[2][0]).toBe(
"swaggerPetstoreRequestBodies.ts",
);
expect(writeFile.mock.calls[2][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Schemas from "./swaggerPetstoreSchemas";
export enum SearchPetRequest {
cat = "cat",
dog = "dog"
}
export type UpdatePetRequest = Schemas.NewPet;
"
`);
});

it("should generate the parameters file with enums instead of string unions", async () => {
const writeFile = jest.fn();
const readFile = jest.fn(() => Promise.resolve(""));

await generateSchemaTypes(
{
openAPIDocument: petstore,
writeFile,
readFile,
existsFile: () => true,
},
{
filenameCase: "camel",
useEnums: true,
},
);
expect(writeFile.mock.calls[3][0]).toBe("swaggerPetstoreParameters.ts");
expect(writeFile.mock.calls[3][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
/**
* Filter by type
*/
export enum PetFilterParam {
cat = "cat",
dog = "dog"
}
/**
* Unique identifier
*/
export type IdParam = string;
"
`);
});
Expand Down
Loading

0 comments on commit 9a4d362

Please sign in to comment.