From 9b2dcfa63f821bf106acbc82cbfaf474707c5731 Mon Sep 17 00:00:00 2001 From: solufa Date: Sat, 15 May 2021 13:28:07 +0900 Subject: [PATCH] fix: convert names that begin with a number --- samples/openapi.json | 6 +++--- samples/openapi/@types/index.ts | 2 +- samples/openapi/api/v3/login/index.ts | 2 +- samples/openapi/api/v3/me/index.ts | 2 +- src/builderUtils/converters.ts | 4 +++- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/samples/openapi.json b/samples/openapi.json index e9629cce..5d6b333d 100644 --- a/samples/openapi.json +++ b/samples/openapi.json @@ -2149,7 +2149,7 @@ { "type": "object", "required": ["token"], - "properties": { "settings": { "$ref": "#/components/schemas/UserSettings" } } + "properties": { "settings": { "$ref": "#/components/schemas/200-UserSettings" } } } ] } @@ -2381,7 +2381,7 @@ "type": "string", "example": "bc402f28c60255f6af5057434587e5a41e71afb0" }, - "settings": { "$ref": "#/components/schemas/UserSettings" } + "settings": { "$ref": "#/components/schemas/200-UserSettings" } } } ] @@ -3247,7 +3247,7 @@ "url": { "type": "string", "format": "url" } } }, - "UserSettings": { + "200-UserSettings": { "required": ["isAppLocked"], "properties": { "isAppLocked": { "type": "boolean" } } }, diff --git a/samples/openapi/@types/index.ts b/samples/openapi/@types/index.ts index 9f23c170..5f536a8a 100644 --- a/samples/openapi/@types/index.ts +++ b/samples/openapi/@types/index.ts @@ -61,7 +61,7 @@ export type UserInfo = { url: string } -export type UserSettings = { +export type $200_UserSettings = { isAppLocked: boolean } diff --git a/samples/openapi/api/v3/login/index.ts b/samples/openapi/api/v3/login/index.ts index b9f232b5..cd1368a4 100644 --- a/samples/openapi/api/v3/login/index.ts +++ b/samples/openapi/api/v3/login/index.ts @@ -9,7 +9,7 @@ export type Methods = { /** OK */ resBody: Types.UserInfo & { token: string - settings?: Types.UserSettings + settings?: Types.$200_UserSettings } reqBody: { diff --git a/samples/openapi/api/v3/me/index.ts b/samples/openapi/api/v3/me/index.ts index 345e2167..b56bbe4b 100644 --- a/samples/openapi/api/v3/me/index.ts +++ b/samples/openapi/api/v3/me/index.ts @@ -8,7 +8,7 @@ export type Methods = { /** OK */ resBody: Types.UserInfo & { - settings?: Types.UserSettings + settings?: Types.$200_UserSettings } } } diff --git a/src/builderUtils/converters.ts b/src/builderUtils/converters.ts index a55787c1..5c55003b 100644 --- a/src/builderUtils/converters.ts +++ b/src/builderUtils/converters.ts @@ -2,7 +2,9 @@ import { OpenAPIV3 } from 'openapi-types' import { Prop, PropValue } from './props2String' export const defKey2defName = (key: string) => - `${key[0].toUpperCase()}${key.slice(1).replace(/[^a-zA-Z0-9$_]/g, '_')}` + `${key[0].replace(/^([^a-zA-Z$_])$/, '$$$1').toUpperCase()}${key + .slice(1) + .replace(/[^a-zA-Z0-9$_]/g, '_')}` export const $ref2TypeName = (ref: string) => { const [, , , typeName, , propName] = ref.split('/')