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

Enum names Type and Type2. #624

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
115 changes: 115 additions & 0 deletions packages/codegen/src/__fixtures__/types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"openapi": "3.1.0",
"info": {
"title": "Swagger types example",
"version": "1.0.0"
},
"servers": [
{
"url": "https://api.example.com"
}
],
"paths": {
"/circle/{circle_id}": {
"get": {
"tags": ["circle"],
"summary": "Find circle by ID",
"description": "Returns a single circle",
"operationId": "getCircleById",
"parameters": [
{
"name": "circle_id",
"in": "path",
"description": "ID of circle to return",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Circle"
}
}
}
}
}
}
},
"/triangle/{triangle_id}": {
"get": {
"tags": ["triangle"],
"summary": "Find triangle by ID",
"description": "Returns a single triangle",
"operationId": "getTriangleById",
"parameters": [
{
"name": "triangle_id",
"in": "path",
"description": "ID of triangle to return",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Triangle"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Circle": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"shape": {
"enum": [1, 2],
"type": "integer",
"x-enumNames": [
"round",
"oval"
],
"x-ts-type": "CircleType"
}
}
},
"Triangle": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"shape": {
"enum": [1, 2, 3],
"type": "integer",
"x-enumNames": [
"equilateral",
"isosceles",
"scalene"
],
"x-ts-type": "TriangleType"
}
}
}
}
}
}
7 changes: 6 additions & 1 deletion packages/codegen/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type SchemaObject = OpenAPISchemaObject & {
"x-enumNames"?: string[];
"x-enum-varnames"?: string[];
"x-component-ref-path"?: string;
"x-ts-type"?: string;
prefixItems?: (OpenAPIReferenceObject | SchemaObject)[];
};

Expand Down Expand Up @@ -795,14 +796,18 @@ export default class ApiGenerator {
const baseName = schema.title || _.upperFirst(propName);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code here - have you tried setting the title prop? instead of the new x-ts-type?

(sorry, wasn't aware of this)

Copy link
Author

@2Fast2BCn 2Fast2BCn Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh boy
OK I'll just strip my code changes and leave only the test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This however doesn't fix my problem.
In swagger I cannot add title.
It has to be an extension and start with x-
So should we duplicate this or?

// TODO: use _.camelCase in future major version
// (currently we allow _ and $ for backwards compatibility)
const proposedName = baseName
const autoGeneratedName = baseName
.split(/[^A-Za-z0-9$_]/g)
.map((n) => _.upperFirst(n))
.join("");
const stringEnumValue = this.getEnumValuesString(
schema.enum ? schema.enum : [],
);

const tsName = schema["x-ts-type"];

const proposedName = tsName ? tsName : autoGeneratedName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

const proposedName =
  schema["x-ts-type"] ??
  baseName
    .split(/[^A-Za-z0-9$_]/g)
    .map((n) => _.upperFirst(n))
    .join("");


const name = this.getEnumUniqueAlias(proposedName, stringEnumValue);

if (this.enumRefs[proposedName] && proposedName === name) {
Expand Down
8 changes: 8 additions & 0 deletions packages/codegen/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ describe("useEnumType", () => {
`export enum Category2 { Rich = "rich", Wealthy = "wealthy", Poor = "poor" }`,
);
});

it("should handle x-ts-type", async () => {
const src = await generate(__dirname + "/__fixtures__/types.json", { useEnumType: true });

expect(src).toContain("export enum TriangleType");

expect(src).toContain("export enum CircleType");
});
});

describe("argumentStyle", () => {
Expand Down
Loading