Skip to content

Commit

Permalink
fix!: Generated enum should be in pascal case (#282)
Browse files Browse the repository at this point in the history
* Add prettier in the testing flow

* Fix enum generation
  • Loading branch information
fabien0102 authored Feb 3, 2025
1 parent ea21f34 commit e16bc82
Show file tree
Hide file tree
Showing 7 changed files with 2,303 additions and 1,749 deletions.
40 changes: 20 additions & 20 deletions plugins/typescript/src/core/schemaToEnumDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toMatchInlineSnapshot(`
"export enum Test {
AVAILABLE = "AVAILABLE",
PENDING = "PENDING",
SOLD = "SOLD"
}"
"export enum Test {
Available = "AVAILABLE",
Pending = "PENDING",
Sold = "SOLD"
}"
`);
});

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

expect(printSchema(schema)).toMatchInlineSnapshot(`
"export enum Test {
ONE = 1,
TWO = 2,
THREE = 3
One = 1,
Two = 2,
Three = 3
}"
`);
});
Expand All @@ -43,13 +43,13 @@ describe("schemaToTypeAliasDeclaration", () => {

expect(printSchema(schema)).toMatchInlineSnapshot(`
"export enum Test {
ZERO = 0,
SEVEN = 7,
FIFTEEN = 15,
ONE_HUNDRED = 100,
ONE_THOUSAND = 1000,
ONE_THOUSAND_FOUR_HUNDRED_FIFTY_SIX = 1456,
THREE_THOUSAND_TWO_HUNDRED_SEVENTEEN = 3217
Zero = 0,
Seven = 7,
Fifteen = 15,
OneHundred = 100,
OneThousand = 1000,
OneThousandFourHundredFiftySix = 1456,
ThreeThousandTwoHundredSeventeen = 3217
}"
`);
});
Expand All @@ -68,17 +68,17 @@ describe("schemaToTypeAliasDeclaration", () => {
`);
});

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

expect(printSchema(schema, "test")).toMatchInlineSnapshot(`
"export enum Test {
AVAILABLE = "AVAILABLE",
PENDING = "PENDING",
SOLD = "SOLD"
Saimois = "saimois",
Bengal = "bengal",
BritishShorthair = "british shorthair"
}"
`);
});
Expand Down
5 changes: 4 additions & 1 deletion plugins/typescript/src/core/schemaToEnumDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ function getEnumMembers(schema: SchemaObject): ts.EnumMember[] {
throw new Error(`Unsupported enum value type: ${typeof enumValue}`);
}

return f.createEnumMember(f.createIdentifier(enumName), enumValueNode);
return f.createEnumMember(
f.createIdentifier(pascal(enumName)),
enumValueNode
);
});
}
260 changes: 147 additions & 113 deletions plugins/typescript/src/generators/generateFetchers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import { set } from "lodash";
import { OpenAPIObject } from "openapi3-ts";
import { Config, generateFetchers } from "./generateFetchers";
import { createWriteFileMock } from "../testUtils";

const config: Config = {
filenamePrefix: "petstore",
Expand Down Expand Up @@ -59,7 +60,7 @@ describe("generateFetchers", () => {
};

it("should generate fetchers", async () => {
const writeFile = vi.fn();
const writeFile = createWriteFileMock();

await generateFetchers(
{
Expand All @@ -73,37 +74,45 @@ describe("generateFetchers", () => {

expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./petstoreFetcher";
import { petstoreFetch } from "./petstoreFetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./petstoreUtils";
export type ListPetsError = Fetcher.ErrorWrapper<{
status: 404;
payload: Responses.NotFoundError;
} | {
status: ServerErrorStatus;
payload: Schemas.Error;
}>;
export type ListPetsResponse = Schemas.Pet[];
/**
* Get all the pets
*/
export const listPets = (signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
"
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./petstoreFetcher";
import { petstoreFetch } from "./petstoreFetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./petstoreUtils";
export type ListPetsError = Fetcher.ErrorWrapper<
| {
status: 404;
payload: Responses.NotFoundError;
}
| {
status: ServerErrorStatus;
payload: Schemas.Error;
}
>;
export type ListPetsResponse = Schemas.Pet[];
/**
* Get all the pets
*/
export const listPets = (signal?: AbortSignal) =>
petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
url: "/pets",
method: "get",
signal,
});
"
`);
});

it("should generate fetchers without prefix", async () => {
const writeFile = vi.fn();
const writeFile = createWriteFileMock();

await generateFetchers(
{
Expand All @@ -117,37 +126,45 @@ describe("generateFetchers", () => {

expect(writeFile.mock.calls[1][0]).toBe("components.ts");
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./fetcher";
import { fetch } from "./fetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./utils";
export type ListPetsError = Fetcher.ErrorWrapper<{
status: 404;
payload: Responses.NotFoundError;
} | {
status: ServerErrorStatus;
payload: Schemas.Error;
}>;
export type ListPetsResponse = Schemas.Pet[];
/**
* Get all the pets
*/
export const listPets = (signal?: AbortSignal) => fetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
"
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./fetcher";
import { fetch } from "./fetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./utils";
export type ListPetsError = Fetcher.ErrorWrapper<
| {
status: 404;
payload: Responses.NotFoundError;
}
| {
status: ServerErrorStatus;
payload: Schemas.Error;
}
>;
export type ListPetsResponse = Schemas.Pet[];
/**
* Get all the pets
*/
export const listPets = (signal?: AbortSignal) =>
fetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
url: "/pets",
method: "get",
signal,
});
"
`);
});

it("should generate fetcher with injected props", async () => {
const writeFile = vi.fn();
const writeFile = createWriteFileMock();

await generateFetchers(
{
Expand All @@ -168,39 +185,48 @@ describe("generateFetchers", () => {

expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./petstoreFetcher";
import { petstoreFetch, PetstoreFetcherExtraProps } from "./petstoreFetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./petstoreUtils";
export type ListPetsError = Fetcher.ErrorWrapper<{
status: 404;
payload: Responses.NotFoundError;
} | {
status: ServerErrorStatus;
payload: Schemas.Error;
}>;
export type ListPetsResponse = Schemas.Pet[];
export type ListPetsVariables = PetstoreFetcherExtraProps;
/**
* Get all the pets
*/
export const listPets = (variables: ListPetsVariables, signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", ...variables, signal });
"
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./petstoreFetcher";
import { petstoreFetch, PetstoreFetcherExtraProps } from "./petstoreFetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./petstoreUtils";
export type ListPetsError = Fetcher.ErrorWrapper<
| {
status: 404;
payload: Responses.NotFoundError;
}
| {
status: ServerErrorStatus;
payload: Schemas.Error;
}
>;
export type ListPetsResponse = Schemas.Pet[];
export type ListPetsVariables = PetstoreFetcherExtraProps;
/**
* Get all the pets
*/
export const listPets = (variables: ListPetsVariables, signal?: AbortSignal) =>
petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
url: "/pets",
method: "get",
...variables,
signal,
});
"
`);
});

it("should generate fetcher with operations by tag", async () => {
const writeFile = vi.fn();
const writeFile = createWriteFileMock();

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

expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./petstoreFetcher";
import { petstoreFetch } from "./petstoreFetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./petstoreUtils";
export type ListPetsError = Fetcher.ErrorWrapper<{
status: 404;
payload: Responses.NotFoundError;
} | {
status: ServerErrorStatus;
payload: Schemas.Error;
}>;
export type ListPetsResponse = Schemas.Pet[];
/**
* Get all the pets
*/
export const listPets = (signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
export const operationsByTag = { "pets": { listPets } };
"
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import type * as Fetcher from "./petstoreFetcher";
import { petstoreFetch } from "./petstoreFetcher";
import type * as Schemas from "./petstoreSchemas";
import type * as Responses from "./petstoreResponses";
import type { ServerErrorStatus } from "./petstoreUtils";
export type ListPetsError = Fetcher.ErrorWrapper<
| {
status: 404;
payload: Responses.NotFoundError;
}
| {
status: ServerErrorStatus;
payload: Schemas.Error;
}
>;
export type ListPetsResponse = Schemas.Pet[];
/**
* Get all the pets
*/
export const listPets = (signal?: AbortSignal) =>
petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
url: "/pets",
method: "get",
signal,
});
export const operationsByTag = { pets: { listPets } };
"
`);
});
});
Loading

0 comments on commit e16bc82

Please sign in to comment.