From a88e7d9f8a313c641cf651256a710d0610b95b5d Mon Sep 17 00:00:00 2001 From: Lubos Date: Fri, 6 Dec 2024 20:49:24 +0800 Subject: [PATCH] fix: prefix restricted identifier names with underscore --- .changeset/rude-insects-ring.md | 5 + packages/openapi-ts/src/generate/files.ts | 35 +- .../src/openApi/common/parser/sanitize.ts | 2 +- .../shared/utils/__tests__/identifier.test.ts | 362 ++++++++++++++++++ .../src/openApi/shared/utils/identifier.ts | 35 ++ .../src/openApi/v2/parser/getModels.ts | 7 +- .../v3/parser/__tests__/getModel.test.ts | 12 +- .../src/plugins/@hey-api/schemas/plugin.ts | 11 +- .../src/plugins/@hey-api/sdk/plugin-legacy.ts | 4 +- packages/openapi-ts/src/plugins/zod/plugin.ts | 8 +- packages/openapi-ts/src/utils/meta.ts | 4 +- packages/openapi-ts/src/utils/regexp.ts | 18 +- packages/openapi-ts/src/utils/transform.ts | 4 +- packages/openapi-ts/src/utils/type.ts | 4 +- .../3.0.x/plugins/zod/default/zod.gen.ts | 8 +- .../3.1.x/plugins/zod/default/zod.gen.ts | 8 +- packages/openapi-ts/test/sample.cjs | 11 +- 17 files changed, 474 insertions(+), 64 deletions(-) create mode 100644 .changeset/rude-insects-ring.md create mode 100644 packages/openapi-ts/src/openApi/shared/utils/__tests__/identifier.test.ts create mode 100644 packages/openapi-ts/src/openApi/shared/utils/identifier.ts diff --git a/.changeset/rude-insects-ring.md b/.changeset/rude-insects-ring.md new file mode 100644 index 000000000..afbd68255 --- /dev/null +++ b/.changeset/rude-insects-ring.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +fix: prefix restricted identifier names with underscore diff --git a/packages/openapi-ts/src/generate/files.ts b/packages/openapi-ts/src/generate/files.ts index 9fb44d287..b41fa4997 100644 --- a/packages/openapi-ts/src/generate/files.ts +++ b/packages/openapi-ts/src/generate/files.ts @@ -6,9 +6,8 @@ import type ts from 'typescript'; import { compiler } from '../compiler'; import { type ImportExportItemObject, tsNodeToString } from '../compiler/utils'; import type { IRContext } from '../ir/context'; -import { ensureValidTypeScriptJavaScriptIdentifier } from '../openApi'; +import { ensureValidIdentifier } from '../openApi/shared/utils/identifier'; import type { StringCase } from '../types/config'; -import { reservedWordsRegExp } from '../utils/regexp'; import { stringCase } from '../utils/stringCase'; import { ensureDirSync } from './utils'; @@ -129,22 +128,9 @@ export class TypeScriptFile { }: Omit & { namespace: keyof Namespaces; }): Identifier { - let validNameTransformer: ((name: string) => string) | undefined; - switch (namespace) { - // TODO: parser - add case transformers - case 'type': - case 'value': - validNameTransformer = (name) => - ensureValidTypeScriptJavaScriptIdentifier(name).replace( - reservedWordsRegExp, - '_$1', - ); - break; - } return ensureUniqueIdentifier({ case: this._identifierCase, namespace: this.namespaces[namespace], - validNameTransformer, ...args, }); } @@ -275,8 +261,11 @@ interface EnsureUniqueIdentifierData { case: StringCase | undefined; count?: number; create?: boolean; + /** + * Transforms name obtained from `$ref` before it's passed to `stringCase()`. + */ + nameTransformer?: (name: string) => string; namespace: Namespace; - validNameTransformer?: (value: string) => string; } const ensureUniqueIdentifier = ({ @@ -284,8 +273,8 @@ const ensureUniqueIdentifier = ({ case: identifierCase, count = 1, create = false, + nameTransformer, namespace, - validNameTransformer, }: EnsureUniqueIdentifierData): Identifier => { const parts = $ref.split('/'); const name = parts[parts.length - 1] || ''; @@ -305,7 +294,11 @@ const ensureUniqueIdentifier = ({ }; } - let nameWithCasing = stringCase({ case: identifierCase, value: name }); + const nameWithTransform = nameTransformer?.(name) ?? name; + let nameWithCasing = stringCase({ + case: identifierCase, + value: nameWithTransform, + }); if (count > 1) { nameWithCasing = `${nameWithCasing}${count}`; @@ -325,8 +318,8 @@ const ensureUniqueIdentifier = ({ case: identifierCase, count: count + 1, create, + nameTransformer, namespace, - validNameTransformer, }); } @@ -339,9 +332,7 @@ const ensureUniqueIdentifier = ({ nameValue = { $ref, - name: validNameTransformer - ? validNameTransformer(nameWithCasing) - : nameWithCasing, + name: ensureValidIdentifier(nameWithCasing), }; namespace[nameWithCasing] = nameValue; namespace[nameValue.$ref] = nameValue; diff --git a/packages/openapi-ts/src/openApi/common/parser/sanitize.ts b/packages/openapi-ts/src/openApi/common/parser/sanitize.ts index 49f7d5154..8ae283193 100644 --- a/packages/openapi-ts/src/openApi/common/parser/sanitize.ts +++ b/packages/openapi-ts/src/openApi/common/parser/sanitize.ts @@ -1,8 +1,8 @@ import { illegalStartCharactersRegExp } from '../../../utils/regexp'; export const ensureValidTypeScriptJavaScriptIdentifier = (name: string) => { - illegalStartCharactersRegExp.lastIndex = 0; const replaced = name.replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '_'); + illegalStartCharactersRegExp.lastIndex = 0; const startsWithIllegalCharacter = illegalStartCharactersRegExp.test(replaced); const valid = startsWithIllegalCharacter ? `_${replaced}` : replaced; diff --git a/packages/openapi-ts/src/openApi/shared/utils/__tests__/identifier.test.ts b/packages/openapi-ts/src/openApi/shared/utils/__tests__/identifier.test.ts new file mode 100644 index 000000000..a9c118567 --- /dev/null +++ b/packages/openapi-ts/src/openApi/shared/utils/__tests__/identifier.test.ts @@ -0,0 +1,362 @@ +import { describe, expect, it } from 'vitest'; + +import { ensureValidIdentifier } from '../identifier'; + +describe('ensureValidIdentifier', () => { + const scenarios: Array<{ + name: string; + output: string; + }> = [ + { + name: 'document', + output: '_document', + }, + { + name: 'history', + output: '_history', + }, + { + name: 'location', + output: '_location', + }, + { + name: 'navigator', + output: '_navigator', + }, + { + name: 'window', + output: '_window', + }, + { + name: 'console', + output: '_console', + }, + { + name: 'Array', + output: '_Array', + }, + { + name: 'Date', + output: '_Date', + }, + { + name: 'Error', + output: '_Error', + }, + { + name: 'Function', + output: '_Function', + }, + { + name: 'JSON', + output: '_JSON', + }, + { + name: 'Map', + output: '_Map', + }, + { + name: 'Math', + output: '_Math', + }, + { + name: 'Object', + output: '_Object', + }, + { + name: 'Promise', + output: '_Promise', + }, + { + name: 'RegExp', + output: '_RegExp', + }, + { + name: 'Set', + output: '_Set', + }, + { + name: 'WeakMap', + output: '_WeakMap', + }, + { + name: 'WeakSet', + output: '_WeakSet', + }, + { + name: 'arguments', + output: '_arguments', + }, + { + name: 'async', + output: '_async', + }, + { + name: 'await', + output: '_await', + }, + { + name: 'break', + output: '_break', + }, + { + name: 'case', + output: '_case', + }, + { + name: 'catch', + output: '_catch', + }, + { + name: 'class', + output: '_class', + }, + { + name: 'const', + output: '_const', + }, + { + name: 'continue', + output: '_continue', + }, + { + name: 'debugger', + output: '_debugger', + }, + { + name: 'default', + output: '_default', + }, + { + name: 'delete', + output: '_delete', + }, + { + name: 'do', + output: '_do', + }, + { + name: 'else', + output: '_else', + }, + { + name: 'enum', + output: '_enum', + }, + { + name: 'eval', + output: '_eval', + }, + { + name: 'export', + output: '_export', + }, + { + name: 'extends', + output: '_extends', + }, + { + name: 'false', + output: '_false', + }, + { + name: 'finally', + output: '_finally', + }, + { + name: 'for', + output: '_for', + }, + { + name: 'from', + output: '_from', + }, + { + name: 'function', + output: '_function', + }, + { + name: 'if', + output: '_if', + }, + { + name: 'implements', + output: '_implements', + }, + { + name: 'import', + output: '_import', + }, + { + name: 'in', + output: '_in', + }, + { + name: 'instanceof', + output: '_instanceof', + }, + { + name: 'interface', + output: '_interface', + }, + { + name: 'let', + output: '_let', + }, + { + name: 'new', + output: '_new', + }, + { + name: 'null', + output: '_null', + }, + { + name: 'package', + output: '_package', + }, + { + name: 'private', + output: '_private', + }, + { + name: 'protected', + output: '_protected', + }, + { + name: 'public', + output: '_public', + }, + { + name: 'return', + output: '_return', + }, + { + name: 'static', + output: '_static', + }, + { + name: 'super', + output: '_super', + }, + { + name: 'switch', + output: '_switch', + }, + { + name: 'this', + output: '_this', + }, + { + name: 'throw', + output: '_throw', + }, + { + name: 'true', + output: '_true', + }, + { + name: 'try', + output: '_try', + }, + { + name: 'typeof', + output: '_typeof', + }, + { + name: 'var', + output: '_var', + }, + { + name: 'void', + output: '_void', + }, + { + name: 'while', + output: '_while', + }, + { + name: 'with', + output: '_with', + }, + { + name: 'yield', + output: '_yield', + }, + { + name: 'global', + output: '_global', + }, + { + name: 'process', + output: '_process', + }, + { + name: 'Buffer', + output: '_Buffer', + }, + { + name: 'any', + output: '_any', + }, + { + name: 'as', + output: '_as', + }, + { + name: 'bigint', + output: '_bigint', + }, + { + name: 'boolean', + output: '_boolean', + }, + { + name: 'namespace', + output: '_namespace', + }, + { + name: 'never', + output: '_never', + }, + { + name: 'null', + output: '_null', + }, + { + name: 'number', + output: '_number', + }, + { + name: 'string', + output: '_string', + }, + { + name: 'symbol', + output: '_symbol', + }, + { + name: 'type', + output: '_type', + }, + { + name: 'undefined', + output: '_undefined', + }, + { + name: 'unknown', + output: '_unknown', + }, + { + name: 'void', + output: '_void', + }, + ]; + + it.each(scenarios)( + 'transforms $name -> $output', + async ({ name, output }) => { + expect(ensureValidIdentifier(name)).toEqual(output); + }, + ); +}); diff --git a/packages/openapi-ts/src/openApi/shared/utils/identifier.ts b/packages/openapi-ts/src/openApi/shared/utils/identifier.ts new file mode 100644 index 000000000..8f8c7e2df --- /dev/null +++ b/packages/openapi-ts/src/openApi/shared/utils/identifier.ts @@ -0,0 +1,35 @@ +import { + illegalStartCharactersRegExp, + reservedBrowserGlobalsRegExp, + reservedJavaScriptGlobalsRegExp, + reservedJavaScriptKeywordsRegExp, + reservedNodeGlobalsRegExp, + reservedTypeScriptKeywordsRegExp, +} from '../../../utils/regexp'; + +const regexps = [ + reservedJavaScriptKeywordsRegExp, + reservedTypeScriptKeywordsRegExp, + reservedJavaScriptGlobalsRegExp, + reservedNodeGlobalsRegExp, + reservedBrowserGlobalsRegExp, +]; + +export const ensureValidIdentifier = (name: string): string => { + let identifier = name.replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '_'); + + illegalStartCharactersRegExp.lastIndex = 0; + if (illegalStartCharactersRegExp.test(identifier)) { + return `_${identifier}`; + } + + for (const regexp of regexps) { + if (identifier.startsWith('_')) { + return identifier; + } + + identifier = identifier.replace(regexp, '_$1'); + } + + return identifier; +}; diff --git a/packages/openapi-ts/src/openApi/v2/parser/getModels.ts b/packages/openapi-ts/src/openApi/v2/parser/getModels.ts index 76eba07df..5caf1943c 100644 --- a/packages/openapi-ts/src/openApi/v2/parser/getModels.ts +++ b/packages/openapi-ts/src/openApi/v2/parser/getModels.ts @@ -1,5 +1,5 @@ import type { Client } from '../../../types/client'; -import { reservedWordsRegExp } from '../../../utils/regexp'; +import { reservedJavaScriptKeywordsRegExp } from '../../../utils/regexp'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import { getModel } from './getModel'; @@ -13,7 +13,10 @@ export const getModels = ( Object.entries(openApi.definitions ?? {}).forEach( ([definitionName, definition]) => { const definitionType = getType({ type: definitionName }); - const name = definitionType.base.replace(reservedWordsRegExp, '_$1'); + const name = definitionType.base.replace( + reservedJavaScriptKeywordsRegExp, + '_$1', + ); const meta = { $ref: `#/definitions/${definitionName}`, name, diff --git a/packages/openapi-ts/src/openApi/v3/parser/__tests__/getModel.test.ts b/packages/openapi-ts/src/openApi/v3/parser/__tests__/getModel.test.ts index 8914262cf..61a56b8fb 100644 --- a/packages/openapi-ts/src/openApi/v3/parser/__tests__/getModel.test.ts +++ b/packages/openapi-ts/src/openApi/v3/parser/__tests__/getModel.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it, vi } from 'vitest'; import type { Config } from '../../../../types/config'; -import { reservedWordsRegExp } from '../../../../utils/regexp'; +import { reservedJavaScriptKeywordsRegExp } from '../../../../utils/regexp'; import { getType } from '../../../common/parser/type'; import { getModel } from '../getModel'; @@ -102,7 +102,10 @@ describe('getModel', () => { isDefinition: true, meta: { $ref: '', - name: definitionType.base.replace(reservedWordsRegExp, '_$1'), + name: definitionType.base.replace( + reservedJavaScriptKeywordsRegExp, + '_$1', + ), }, openApi, types: {}, @@ -118,7 +121,10 @@ describe('getModel', () => { isDefinition: true, meta: { $ref: '', - name: definitionType.base.replace(reservedWordsRegExp, '_$1'), + name: definitionType.base.replace( + reservedJavaScriptKeywordsRegExp, + '_$1', + ), }, openApi, types: {}, diff --git a/packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts b/packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts index 869452584..8be385afb 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts @@ -1,6 +1,5 @@ import { compiler } from '../../../compiler'; import type { IRContext } from '../../../ir/context'; -import { ensureValidTypeScriptJavaScriptIdentifier } from '../../../openApi'; import type { OpenApiV3_0_X } from '../../../openApi/3.0.x'; import type { ReferenceObject as OpenApiV3_0_XReferenceObject, @@ -8,6 +7,7 @@ import type { } from '../../../openApi/3.0.x/types/spec'; import type { OpenApiV3_1_X } from '../../../openApi/3.1.x'; import type { SchemaObject as OpenApiV3_1_XSchemaObject } from '../../../openApi/3.1.x/types/spec'; +import { ensureValidIdentifier } from '../../../openApi/shared/utils/identifier'; import type { Plugin } from '../../types'; import type { Config } from './types'; @@ -256,13 +256,8 @@ const schemaName = ({ | OpenApiV3_0_XSchemaObject | OpenApiV3_1_XSchemaObject; }): string => { - const validName = ensureValidTypeScriptJavaScriptIdentifier(name); - - if (plugin.nameBuilder) { - return plugin.nameBuilder(validName, schema); - } - - return `${validName}Schema`; + const customName = plugin.nameBuilder?.(name, schema) ?? `${name}Schema`; + return ensureValidIdentifier(customName); }; const schemasV3_0_X = ({ diff --git a/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin-legacy.ts b/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin-legacy.ts index 183fed1b4..1f838ba7b 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin-legacy.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin-legacy.ts @@ -27,7 +27,7 @@ import { legacyNameFromConfig, } from '../../../utils/config'; import { escapeComment, escapeName } from '../../../utils/escape'; -import { reservedWordsRegExp } from '../../../utils/regexp'; +import { reservedJavaScriptKeywordsRegExp } from '../../../utils/regexp'; import { stringCase } from '../../../utils/stringCase'; import { transformServiceName } from '../../../utils/transform'; import { setUniqueTypeName } from '../../../utils/type'; @@ -497,7 +497,7 @@ export const serviceFunctionIdentifier = ({ return config.plugins['@hey-api/sdk'].methodNameBuilder(operation); } - if (handleIllegal && id.match(reservedWordsRegExp)) { + if (handleIllegal && id.match(reservedJavaScriptKeywordsRegExp)) { return `${id}_`; } diff --git a/packages/openapi-ts/src/plugins/zod/plugin.ts b/packages/openapi-ts/src/plugins/zod/plugin.ts index 508411950..f11771b6c 100644 --- a/packages/openapi-ts/src/plugins/zod/plugin.ts +++ b/packages/openapi-ts/src/plugins/zod/plugin.ts @@ -22,6 +22,8 @@ const optionalIdentifier = compiler.identifier({ text: 'optional' }); const readonlyIdentifier = compiler.identifier({ text: 'readonly' }); const zIdentifier = compiler.identifier({ text: 'z' }); +const nameTransformer = (name: string) => `z${name}`; + const arrayTypeToZodSchema = ({ context, namespace, @@ -688,10 +690,11 @@ const schemaToZodSchema = ({ // this could be (maybe?) fixed by reshuffling the generation order const identifier = file.identifier({ $ref: schema.$ref, + nameTransformer, namespace: 'value', }); if (identifier.name) { - expression = compiler.identifier({ text: `z${identifier.name || ''}` }); + expression = compiler.identifier({ text: identifier.name || '' }); } else { const ref = context.resolveIrRef(schema.$ref); expression = schemaToZodSchema({ @@ -753,12 +756,13 @@ const schemaToZodSchema = ({ const identifier = file.identifier({ $ref, create: true, + nameTransformer, namespace: 'value', }); const statement = compiler.constVariable({ exportConst: true, expression, - name: `z${identifier.name || ''}`, + name: identifier.name || '', }); file.add(statement); } diff --git a/packages/openapi-ts/src/utils/meta.ts b/packages/openapi-ts/src/utils/meta.ts index a68f65eee..5110278b1 100644 --- a/packages/openapi-ts/src/utils/meta.ts +++ b/packages/openapi-ts/src/utils/meta.ts @@ -1,6 +1,6 @@ import { getType } from '../openApi'; import { refParametersPartial, refSchemasPartial } from './const'; -import { reservedWordsRegExp } from './regexp'; +import { reservedJavaScriptKeywordsRegExp } from './regexp'; import { cleanAndTransformTypeName } from './transform'; export const getParametersMeta = (definitionName: string) => { @@ -19,7 +19,7 @@ export const getParametersMeta = (definitionName: string) => { * Note: there's a related code to this workaround in `getType()` * method that needs to be cleaned up when this is addressed. */ - const name = `Parameter${definitionType.base.replace(reservedWordsRegExp, '_$1')}`; + const name = `Parameter${definitionType.base.replace(reservedJavaScriptKeywordsRegExp, '_$1')}`; const meta = { $ref: refParametersPartial + definitionName, name, diff --git a/packages/openapi-ts/src/utils/regexp.ts b/packages/openapi-ts/src/utils/regexp.ts index 42e09afb6..1b44e86d6 100644 --- a/packages/openapi-ts/src/utils/regexp.ts +++ b/packages/openapi-ts/src/utils/regexp.ts @@ -8,11 +8,19 @@ export const digitsRegExp = /^\d+$/; */ export const illegalStartCharactersRegExp = /^[^$_\p{ID_Start}]+/u; -/** - * Matches the whole value if it's one of the reserved words. - */ -export const reservedWordsRegExp = - /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; +export const reservedBrowserGlobalsRegExp = + /^(document|history|location|navigator|window)$/g; + +export const reservedJavaScriptGlobalsRegExp = + /^(console|Array|Date|Error|Function|JSON|Map|Math|Object|Promise|RegExp|Set|WeakMap|WeakSet)$/g; + +export const reservedJavaScriptKeywordsRegExp = + /^(arguments|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|from|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; + +export const reservedNodeGlobalsRegExp = /^(global|process|Buffer)$/g; + +export const reservedTypeScriptKeywordsRegExp = + /^(any|as|bigint|boolean|namespace|never|null|number|string|symbol|type|undefined|unknown|void)$/g; /** * Javascript identifier regexp pattern retrieved from diff --git a/packages/openapi-ts/src/utils/transform.ts b/packages/openapi-ts/src/utils/transform.ts index 5dd12d136..767f91151 100644 --- a/packages/openapi-ts/src/utils/transform.ts +++ b/packages/openapi-ts/src/utils/transform.ts @@ -1,7 +1,7 @@ import { ensureValidTypeScriptJavaScriptIdentifier } from '../openApi'; import type { Config } from '../types/config'; import { getConfig } from './config'; -import { reservedWordsRegExp } from './regexp'; +import { reservedJavaScriptKeywordsRegExp } from './regexp'; import { stringCase } from './stringCase'; export const transformServiceName = ({ @@ -47,6 +47,6 @@ export const transformTypeName = (name: string) => { export const cleanAndTransformTypeName = (name: string) => { const transformed = transformTypeName(name); const cleaned = ensureValidTypeScriptJavaScriptIdentifier(transformed); - const result = cleaned.replace(reservedWordsRegExp, '_$1'); + const result = cleaned.replace(reservedJavaScriptKeywordsRegExp, '_$1'); return result; }; diff --git a/packages/openapi-ts/src/utils/type.ts b/packages/openapi-ts/src/utils/type.ts index 3e01e8f1c..2e66f896d 100644 --- a/packages/openapi-ts/src/utils/type.ts +++ b/packages/openapi-ts/src/utils/type.ts @@ -7,7 +7,7 @@ import { refSchemasPartial } from './const'; import { enumValue } from './enum'; import { escapeComment, escapeName, unescapeName } from './escape'; import { getSchemasMeta } from './meta'; -import { reservedWordsRegExp } from './regexp'; +import { reservedJavaScriptKeywordsRegExp } from './regexp'; import { stringCase } from './stringCase'; import { unique } from './unique'; @@ -340,6 +340,6 @@ export const transformTypeKeyName = (value: string): string => { const name = stringCase({ case: 'camelCase', value: sanitizeOperationParameterName(value), - }).replace(reservedWordsRegExp, '_$1'); + }).replace(reservedJavaScriptKeywordsRegExp, '_$1'); return name; }; diff --git a/packages/openapi-ts/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts b/packages/openapi-ts/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts index a49f34663..b0eac6bf3 100644 --- a/packages/openapi-ts/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts +++ b/packages/openapi-ts/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; -export const z_400 = z.string(); +export const z400 = z.string(); export const zcamelCaseCommentWithBreaks = z.number(); @@ -253,7 +253,7 @@ export const zCompositionWithNestedAnyAndTypeNull = z.object({ propA: z.unknown().optional() }); -export const z_3e_num_1Период = z.enum([ +export const z3e_num_1Период = z.enum([ 'Bird', 'Dog' ]); @@ -356,7 +356,7 @@ export const zFile = z.object({ file: z.string().url().readonly().optional() }); -export const z_default = z.object({ +export const zdefault = z.object({ name: z.string().optional() }); @@ -465,7 +465,7 @@ export const zDeleteFooData = z.string(); export const zDeleteFooData2 = z.string(); -export const z_import = z.string(); +export const zimport = z.string(); export const zSchemaWithFormRestrictedKeys = z.object({ description: z.string().optional(), diff --git a/packages/openapi-ts/test/__snapshots__/3.1.x/plugins/zod/default/zod.gen.ts b/packages/openapi-ts/test/__snapshots__/3.1.x/plugins/zod/default/zod.gen.ts index 845b63ae4..06ce5d746 100644 --- a/packages/openapi-ts/test/__snapshots__/3.1.x/plugins/zod/default/zod.gen.ts +++ b/packages/openapi-ts/test/__snapshots__/3.1.x/plugins/zod/default/zod.gen.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; -export const z_400 = z.string(); +export const z400 = z.string(); export const zcamelCaseCommentWithBreaks = z.number(); @@ -253,7 +253,7 @@ export const zCompositionWithNestedAnyAndTypeNull = z.object({ propA: z.unknown().optional() }); -export const z_3e_num_1Период = z.enum([ +export const z3e_num_1Период = z.enum([ 'Bird', 'Dog' ]); @@ -354,7 +354,7 @@ export const zFile = z.object({ file: z.string().url().readonly().optional() }); -export const z_default = z.object({ +export const zdefault = z.object({ name: z.string().optional() }); @@ -459,7 +459,7 @@ export const zDeleteFooData = z.string(); export const zDeleteFooData2 = z.string(); -export const z_import = z.string(); +export const zimport = z.string(); export const zSchemaWithFormRestrictedKeys = z.object({ description: z.string().optional(), diff --git a/packages/openapi-ts/test/sample.cjs b/packages/openapi-ts/test/sample.cjs index f11cb4933..d93ab50c9 100644 --- a/packages/openapi-ts/test/sample.cjs +++ b/packages/openapi-ts/test/sample.cjs @@ -13,7 +13,7 @@ const main = async () => { exclude: '^#/components/schemas/ModelWithCircularReference$', // include: // '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$', - path: './test/spec/3.1.x/full.json', + path: './test/spec/3.0.x/full.json', // path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json', // path: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml', }, @@ -36,7 +36,7 @@ const main = async () => { { // asClass: true, // include... - name: '@hey-api/sdk', + // name: '@hey-api/sdk', // operationId: false, // serviceNameBuilder: '^Parameters', }, @@ -49,17 +49,18 @@ const main = async () => { // enums: 'typescript+namespace', enums: 'javascript', // exportInlineEnums: true, - name: '@hey-api/typescript', + identifierCase: 'preserve', + // name: '@hey-api/typescript', // tree: true, }, { // name: 'fastify', }, { - name: '@tanstack/vue-query', + // name: '@tanstack/vue-query', }, { - // name: 'zod', + name: 'zod', }, ], // useOptions: false,