Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Extend useEnums to all schema files #223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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