From 1d8adad96aac68b9dcbc4ecdec8ab4d272475bd7 Mon Sep 17 00:00:00 2001 From: Anna Jolly Date: Wed, 23 Oct 2024 14:40:06 -0400 Subject: [PATCH 1/2] Fix types generator when yaml file key includes dashes --- changelog.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- spec/common/types.spec.ts | 4 ++++ spec/common/util.spec.ts | 12 ++++++++++++ src/common/types.ts | 3 ++- src/common/util.ts | 11 +++++++++++ 7 files changed, 36 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index 9c72486..7f628f1 100644 --- a/changelog.md +++ b/changelog.md @@ -47,3 +47,7 @@ A major release with breaking changes. Involves updating type signatures to matc ## 3.4.0 - Update all packages + +## 3.5.1 + +- Fix types generator when yaml file key includes dashes diff --git a/package-lock.json b/package-lock.json index 40bfd48..1d10e55 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rapini", - "version": "3.5.0", + "version": "3.5.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "rapini", - "version": "3.5.0", + "version": "3.5.1", "license": "Apache-2.0", "dependencies": { "@apidevtools/swagger-parser": "^10.1.0", diff --git a/package.json b/package.json index cec3c62..91b69c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rapini", - "version": "3.5.0", + "version": "3.5.1", "description": "Generate React Query hooks, SWR hooks, Axios requests and Typescript types from OpenAPI files", "bin": "dist/cli.js", "scripts": { diff --git a/spec/common/types.spec.ts b/spec/common/types.spec.ts index c9fc4f9..df5e105 100644 --- a/spec/common/types.spec.ts +++ b/spec/common/types.spec.ts @@ -34,6 +34,7 @@ export type Cat = Pet & { }; export type Dog = Pet & { bark?: string; + 'is-cute'?: boolean; }; export type MyResponseType = Cat | Dog; export type MyResponseTypeTwo = Cat | Dog; @@ -194,6 +195,9 @@ describe("makeTypes", () => { bark: { type: "string", }, + "is-cute": { + type: "boolean", + }, }, }, ], diff --git a/spec/common/util.spec.ts b/spec/common/util.spec.ts index af76d20..8d7f645 100644 --- a/spec/common/util.spec.ts +++ b/spec/common/util.spec.ts @@ -1,5 +1,6 @@ import ts from "typescript"; import { + addQuotesWhenHasDashes, capitalizeFirstLetter, combineUniqueParams, lowercaseFirstLetter, @@ -8,6 +9,17 @@ import { nodeId, } from "../../src/common/util"; +describe("addQuotesWhenHasDashes", () => { + it.each` + str | expected + ${""} | ${""} + ${"helloGoodbye"} | ${"helloGoodbye"} + ${"test1-test8-test1_test2"} | ${"'test1-test8-test1_test2'"} + `("addQuotesWhenHasDashes($str) -> $expected", ({ str, expected }) => { + expect(addQuotesWhenHasDashes(str)).toBe(expected); + }); +}); + describe("capitalizeFirstLetter", () => { it.each` str | expected diff --git a/src/common/types.ts b/src/common/types.ts index 78b5673..8410bb3 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -1,6 +1,7 @@ import { OpenAPIV3 } from "openapi-types"; import ts from "typescript"; import { + addQuotesWhenHasDashes, appendNullToUnion, createTypeAliasDeclarationType, createTypeRefOrSchemaObjectIfPathRef, @@ -72,7 +73,7 @@ function createPropertySignature( ) { return ts.factory.createPropertySignature( /*modifiers*/ undefined, - /*name*/ ts.factory.createIdentifier(name), + /*name*/ ts.factory.createIdentifier(addQuotesWhenHasDashes(name)), /*questionToken*/ required ? undefined : ts.factory.createToken(ts.SyntaxKind.QuestionToken), diff --git a/src/common/util.ts b/src/common/util.ts index e5387f1..5aaa1a7 100644 --- a/src/common/util.ts +++ b/src/common/util.ts @@ -325,6 +325,17 @@ export function normalizeOperationId(operationId: string) { return split.join(""); } +/** + * Adds single quotes around operation id if it contains dashes + * @param operationId Raw value from openapi file + * @returns value with single quotes around it if value has dashes + * @example addQuotesWhenHasDashes("nodashes") // nodashes + * @example addQuotesWhenHasDashes("has-dashes-in-it") // 'has-dashes-in-it' + */ +export function addQuotesWhenHasDashes(operationId: string) { + return operationId.includes("-") ? `'${operationId}'` : operationId; +} + export function isRequestBodyObject( obj: OpenAPIV3.ReferenceObject | OpenAPIV3.RequestBodyObject ): obj is OpenAPIV3.RequestBodyObject { From a4f00b8d170b6683007a9fdb52899b6ebfafe253 Mon Sep 17 00:00:00 2001 From: Anna Jolly Date: Wed, 23 Oct 2024 15:52:46 -0400 Subject: [PATCH 2/2] Address PR feedback --- spec/common/types.spec.ts | 2 +- spec/common/util.spec.ts | 12 ------------ src/common/types.ts | 5 +++-- src/common/util.ts | 11 ----------- 4 files changed, 4 insertions(+), 26 deletions(-) diff --git a/spec/common/types.spec.ts b/spec/common/types.spec.ts index df5e105..8e2d7e5 100644 --- a/spec/common/types.spec.ts +++ b/spec/common/types.spec.ts @@ -34,7 +34,7 @@ export type Cat = Pet & { }; export type Dog = Pet & { bark?: string; - 'is-cute'?: boolean; + "is-cute"?: boolean; }; export type MyResponseType = Cat | Dog; export type MyResponseTypeTwo = Cat | Dog; diff --git a/spec/common/util.spec.ts b/spec/common/util.spec.ts index 8d7f645..af76d20 100644 --- a/spec/common/util.spec.ts +++ b/spec/common/util.spec.ts @@ -1,6 +1,5 @@ import ts from "typescript"; import { - addQuotesWhenHasDashes, capitalizeFirstLetter, combineUniqueParams, lowercaseFirstLetter, @@ -9,17 +8,6 @@ import { nodeId, } from "../../src/common/util"; -describe("addQuotesWhenHasDashes", () => { - it.each` - str | expected - ${""} | ${""} - ${"helloGoodbye"} | ${"helloGoodbye"} - ${"test1-test8-test1_test2"} | ${"'test1-test8-test1_test2'"} - `("addQuotesWhenHasDashes($str) -> $expected", ({ str, expected }) => { - expect(addQuotesWhenHasDashes(str)).toBe(expected); - }); -}); - describe("capitalizeFirstLetter", () => { it.each` str | expected diff --git a/src/common/types.ts b/src/common/types.ts index 8410bb3..3cd0914 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -1,7 +1,6 @@ import { OpenAPIV3 } from "openapi-types"; import ts from "typescript"; import { - addQuotesWhenHasDashes, appendNullToUnion, createTypeAliasDeclarationType, createTypeRefOrSchemaObjectIfPathRef, @@ -73,7 +72,9 @@ function createPropertySignature( ) { return ts.factory.createPropertySignature( /*modifiers*/ undefined, - /*name*/ ts.factory.createIdentifier(addQuotesWhenHasDashes(name)), + /*name*/ name.includes("-") + ? ts.factory.createStringLiteral(name) + : ts.factory.createIdentifier(name), /*questionToken*/ required ? undefined : ts.factory.createToken(ts.SyntaxKind.QuestionToken), diff --git a/src/common/util.ts b/src/common/util.ts index 5aaa1a7..e5387f1 100644 --- a/src/common/util.ts +++ b/src/common/util.ts @@ -325,17 +325,6 @@ export function normalizeOperationId(operationId: string) { return split.join(""); } -/** - * Adds single quotes around operation id if it contains dashes - * @param operationId Raw value from openapi file - * @returns value with single quotes around it if value has dashes - * @example addQuotesWhenHasDashes("nodashes") // nodashes - * @example addQuotesWhenHasDashes("has-dashes-in-it") // 'has-dashes-in-it' - */ -export function addQuotesWhenHasDashes(operationId: string) { - return operationId.includes("-") ? `'${operationId}'` : operationId; -} - export function isRequestBodyObject( obj: OpenAPIV3.ReferenceObject | OpenAPIV3.RequestBodyObject ): obj is OpenAPIV3.RequestBodyObject {