Skip to content

Commit

Permalink
fix: Fix pascal enum reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Schwager, Sandro committed Feb 19, 2024
1 parent c8152b9 commit 5438b41
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
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
43 changes: 35 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,7 @@ 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 +773,7 @@ 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 +998,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

0 comments on commit 5438b41

Please sign in to comment.