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

fix: Fix pascal enum reference #219

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
18 changes: 17 additions & 1 deletion plugins/typescript/src/core/schemaToEnumDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,30 @@ describe("schemaToTypeAliasDeclaration", () => {
}"
`);
});

it("should generate uppercase type when providing lowercase schema names", () => {
const schema: SchemaObject = {
type: "string",
enum: ["AVAILABLE", "PENDING", "SOLD"],
};

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

const printSchema = (
schema: SchemaObject,
schemaName: string = "Test",
currentComponent: OpenAPIComponentType = "schemas",
components?: OpenAPIObject["components"],
) => {
const nodes = schemaToEnumDeclaration("Test", schema, {
const nodes = schemaToEnumDeclaration(schemaName, schema, {
currentComponent,
openAPIDocument: { components },
});
Expand Down
45 changes: 37 additions & 8 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,33 @@ describe("schemaToTypeAliasDeclaration", () => {
},
};

expect(printSchema(schema, "schemas", components, true))
expect(printSchema(schema, "Test", "schemas", components, true))
.toMatchInlineSnapshot(`
"export type Test = {
status?: TestStatus;
};"
`);
});

it("should reference created enum with pascal typename", () => {
const schema: SchemaObject = {
type: "object",
properties: {
status: {
type: "string",
enum: ["AVAILABLE", "PENDING", "SOLD"],
},
},
xml: { name: "pet" },
};

const components: OpenAPIObject["components"] = {
schemas: {
Pet: schema,
},
};

expect(printSchema(schema, "test", "schemas", components, true))
.toMatchInlineSnapshot(`
"export type Test = {
status?: TestStatus;
Expand Down Expand Up @@ -348,7 +374,7 @@ describe("schemaToTypeAliasDeclaration", () => {
$ref: "#/components/schemas/User",
};

expect(printSchema(schema, "parameters")).toMatchInlineSnapshot(
expect(printSchema(schema, "Test", "parameters")).toMatchInlineSnapshot(
`"export type Test = Schemas.User;"`,
);
});
Expand Down Expand Up @@ -523,7 +549,7 @@ describe("schemaToTypeAliasDeclaration", () => {

it("should omit the base value if present", () => {
expect(
printSchema(schema, "schemas", {
printSchema(schema, "Test", "schemas", {
schemas: {
Foo: {
type: "object",
Expand Down Expand Up @@ -563,7 +589,7 @@ describe("schemaToTypeAliasDeclaration", () => {

it("should not add the `Omit` if not necessary", () => {
expect(
printSchema(schema, "schemas", {
printSchema(schema, "Test", "schemas", {
schemas: {
Foo: { type: "object", properties: { foo: { type: "string" } } },
Bar: { type: "object", properties: { bar: { type: "string" } } },
Expand All @@ -583,7 +609,7 @@ describe("schemaToTypeAliasDeclaration", () => {

it("should use the original type if compliant", () => {
expect(
printSchema(schema, "schemas", {
printSchema(schema, "Test", "schemas", {
schemas: {
Foo: {
type: "object",
Expand Down Expand Up @@ -724,7 +750,8 @@ describe("schemaToTypeAliasDeclaration", () => {
},
};

expect(printSchema(schema, undefined, components)).toMatchInlineSnapshot(`
expect(printSchema(schema, "Test", undefined, components))
.toMatchInlineSnapshot(`
"export type Test = Foo & {
bar?: number;
};"
Expand All @@ -747,7 +774,8 @@ describe("schemaToTypeAliasDeclaration", () => {
},
};

expect(printSchema(schema, undefined, components)).toMatchInlineSnapshot(`
expect(printSchema(schema, "Test", undefined, components))
.toMatchInlineSnapshot(`
"export type Test = {
bar: string;
};"
Expand Down Expand Up @@ -972,12 +1000,13 @@ describe("schemaToTypeAliasDeclaration", () => {

const printSchema = (
schema: SchemaObject,
schemaName: string = "Test",
currentComponent: OpenAPIComponentType = "schemas",
components?: OpenAPIObject["components"],
useEnums?: boolean,
) => {
const nodes = schemaToTypeAliasDeclaration(
"Test",
schemaName,
schema,
{
currentComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const getType = (

if (schema.enum) {
if (isNodeEnum) {
return f.createTypeReferenceNode(f.createIdentifier(name || ""));
return f.createTypeReferenceNode(f.createIdentifier(pascal(name || "")));
}

const unionTypes = f.createUnionTypeNode([
Expand Down