Skip to content

Commit f5b8902

Browse files
authored
fix: Fix pascal enum reference (#219)
Co-authored-by: Schwager, Sandro <[email protected]>
1 parent 6c02ab3 commit f5b8902

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

plugins/typescript/src/core/schemaToEnumDeclaration.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,30 @@ describe("schemaToTypeAliasDeclaration", () => {
6666
}"
6767
`);
6868
});
69+
70+
it("should generate uppercase type when providing lowercase schema names", () => {
71+
const schema: SchemaObject = {
72+
type: "string",
73+
enum: ["AVAILABLE", "PENDING", "SOLD"],
74+
};
75+
76+
expect(printSchema(schema, "test")).toMatchInlineSnapshot(`
77+
"export enum Test {
78+
AVAILABLE = "AVAILABLE",
79+
PENDING = "PENDING",
80+
SOLD = "SOLD"
81+
}"
82+
`);
83+
});
6984
});
7085

7186
const printSchema = (
7287
schema: SchemaObject,
88+
schemaName: string = "Test",
7389
currentComponent: OpenAPIComponentType = "schemas",
7490
components?: OpenAPIObject["components"],
7591
) => {
76-
const nodes = schemaToEnumDeclaration("Test", schema, {
92+
const nodes = schemaToEnumDeclaration(schemaName, schema, {
7793
currentComponent,
7894
openAPIDocument: { components },
7995
});

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,33 @@ describe("schemaToTypeAliasDeclaration", () => {
8787
},
8888
};
8989

90-
expect(printSchema(schema, "schemas", components, true))
90+
expect(printSchema(schema, "Test", "schemas", components, true))
91+
.toMatchInlineSnapshot(`
92+
"export type Test = {
93+
status?: TestStatus;
94+
};"
95+
`);
96+
});
97+
98+
it("should reference created enum with pascal typename", () => {
99+
const schema: SchemaObject = {
100+
type: "object",
101+
properties: {
102+
status: {
103+
type: "string",
104+
enum: ["AVAILABLE", "PENDING", "SOLD"],
105+
},
106+
},
107+
xml: { name: "pet" },
108+
};
109+
110+
const components: OpenAPIObject["components"] = {
111+
schemas: {
112+
Pet: schema,
113+
},
114+
};
115+
116+
expect(printSchema(schema, "test", "schemas", components, true))
91117
.toMatchInlineSnapshot(`
92118
"export type Test = {
93119
status?: TestStatus;
@@ -348,7 +374,7 @@ describe("schemaToTypeAliasDeclaration", () => {
348374
$ref: "#/components/schemas/User",
349375
};
350376

351-
expect(printSchema(schema, "parameters")).toMatchInlineSnapshot(
377+
expect(printSchema(schema, "Test", "parameters")).toMatchInlineSnapshot(
352378
`"export type Test = Schemas.User;"`,
353379
);
354380
});
@@ -523,7 +549,7 @@ describe("schemaToTypeAliasDeclaration", () => {
523549

524550
it("should omit the base value if present", () => {
525551
expect(
526-
printSchema(schema, "schemas", {
552+
printSchema(schema, "Test", "schemas", {
527553
schemas: {
528554
Foo: {
529555
type: "object",
@@ -563,7 +589,7 @@ describe("schemaToTypeAliasDeclaration", () => {
563589

564590
it("should not add the `Omit` if not necessary", () => {
565591
expect(
566-
printSchema(schema, "schemas", {
592+
printSchema(schema, "Test", "schemas", {
567593
schemas: {
568594
Foo: { type: "object", properties: { foo: { type: "string" } } },
569595
Bar: { type: "object", properties: { bar: { type: "string" } } },
@@ -583,7 +609,7 @@ describe("schemaToTypeAliasDeclaration", () => {
583609

584610
it("should use the original type if compliant", () => {
585611
expect(
586-
printSchema(schema, "schemas", {
612+
printSchema(schema, "Test", "schemas", {
587613
schemas: {
588614
Foo: {
589615
type: "object",
@@ -724,7 +750,8 @@ describe("schemaToTypeAliasDeclaration", () => {
724750
},
725751
};
726752

727-
expect(printSchema(schema, undefined, components)).toMatchInlineSnapshot(`
753+
expect(printSchema(schema, "Test", undefined, components))
754+
.toMatchInlineSnapshot(`
728755
"export type Test = Foo & {
729756
bar?: number;
730757
};"
@@ -747,7 +774,8 @@ describe("schemaToTypeAliasDeclaration", () => {
747774
},
748775
};
749776

750-
expect(printSchema(schema, undefined, components)).toMatchInlineSnapshot(`
777+
expect(printSchema(schema, "Test", undefined, components))
778+
.toMatchInlineSnapshot(`
751779
"export type Test = {
752780
bar: string;
753781
};"
@@ -972,12 +1000,13 @@ describe("schemaToTypeAliasDeclaration", () => {
9721000

9731001
const printSchema = (
9741002
schema: SchemaObject,
1003+
schemaName: string = "Test",
9751004
currentComponent: OpenAPIComponentType = "schemas",
9761005
components?: OpenAPIObject["components"],
9771006
useEnums?: boolean,
9781007
) => {
9791008
const nodes = schemaToTypeAliasDeclaration(
980-
"Test",
1009+
schemaName,
9811010
schema,
9821011
{
9831012
currentComponent,

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const getType = (
150150

151151
if (schema.enum) {
152152
if (isNodeEnum) {
153-
return f.createTypeReferenceNode(f.createIdentifier(name || ""));
153+
return f.createTypeReferenceNode(f.createIdentifier(pascal(name || "")));
154154
}
155155

156156
const unionTypes = f.createUnionTypeNode([

0 commit comments

Comments
 (0)