From 774e14fe52d16bc6ef0e33f45f99053282111590 Mon Sep 17 00:00:00 2001 From: Lubos Date: Wed, 17 Jul 2024 13:37:37 +0100 Subject: [PATCH] fix: handle references to schemas with illegal names --- .changeset/old-islands-raise.md | 5 +++ .../src/openApi/common/parser/type.ts | 6 ++- .../openApi/v3/parser/getModelComposition.ts | 9 +++-- .../src/openApi/v3/parser/getModels.ts | 35 +++--------------- .../v3/parser/getOperationParameter.ts | 3 +- packages/openapi-ts/src/utils/const.ts | 3 ++ packages/openapi-ts/src/utils/meta.ts | 37 +++++++++++++++++++ packages/openapi-ts/src/utils/type.ts | 14 ++++++- .../test/generated/v3/schemas.gen.ts.snap | 7 +++- .../test/generated/v3/services.gen.ts.snap | 2 +- .../test/generated/v3/types.gen.ts.snap | 13 +++++-- .../generated/v3_angular/schemas.gen.ts.snap | 7 +++- .../generated/v3_angular/services.gen.ts.snap | 2 +- .../generated/v3_angular/types.gen.ts.snap | 13 +++++-- .../generated/v3_axios/schemas.gen.ts.snap | 7 +++- .../generated/v3_axios/services.gen.ts.snap | 2 +- .../test/generated/v3_axios/types.gen.ts.snap | 13 +++++-- .../generated/v3_client/services.gen.ts.snap | 2 +- .../generated/v3_client/types.gen.ts.snap | 13 +++++-- .../test/generated/v3_date/types.gen.ts.snap | 4 +- .../v3_enums_typescript/services.gen.ts.snap | 2 +- .../v3_enums_typescript/types.gen.ts.snap | 13 +++++-- .../schemas.gen.ts.snap | 7 +++- .../v3_hey-api_client-axios/types.gen.ts.snap | 13 +++++-- .../schemas.gen.ts.snap | 7 +++- .../v3_hey-api_client-fetch/types.gen.ts.snap | 13 +++++-- .../generated/v3_node/schemas.gen.ts.snap | 7 +++- .../generated/v3_node/services.gen.ts.snap | 2 +- .../test/generated/v3_node/types.gen.ts.snap | 13 +++++-- .../generated/v3_pascalcase/types.gen.ts.snap | 4 +- .../v3_schemas_form/schemas.gen.ts.snap | 6 ++- .../v3_schemas_json/schemas.gen.ts.snap | 7 +++- .../v3_tree_shakeable/services.gen.ts.snap | 2 +- .../v3_tree_shakeable/types.gen.ts.snap | 13 +++++-- .../test/generated/v3_types/types.gen.ts.snap | 13 +++++-- .../v3_types_no_tree/types.gen.ts.snap | 11 ++++-- .../test/generated/v3_xhr/schemas.gen.ts.snap | 7 +++- .../generated/v3_xhr/services.gen.ts.snap | 2 +- .../test/generated/v3_xhr/types.gen.ts.snap | 13 +++++-- packages/openapi-ts/test/sample.cjs | 1 + packages/openapi-ts/test/spec/v3.json | 8 +++- 41 files changed, 253 insertions(+), 108 deletions(-) create mode 100644 .changeset/old-islands-raise.md create mode 100644 packages/openapi-ts/src/utils/const.ts create mode 100644 packages/openapi-ts/src/utils/meta.ts diff --git a/.changeset/old-islands-raise.md b/.changeset/old-islands-raise.md new file mode 100644 index 000000000..041ace282 --- /dev/null +++ b/.changeset/old-islands-raise.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +fix: handle references to schemas with illegal names diff --git a/packages/openapi-ts/src/openApi/common/parser/type.ts b/packages/openapi-ts/src/openApi/common/parser/type.ts index 430cd60db..63e3584b8 100644 --- a/packages/openapi-ts/src/openApi/common/parser/type.ts +++ b/packages/openapi-ts/src/openApi/common/parser/type.ts @@ -1,6 +1,7 @@ import camelcase from 'camelcase'; import { getConfig, isStandaloneClient } from '../../../utils/config'; +import { refParametersPartial } from '../../../utils/const'; import { reservedWordsRegExp } from '../../../utils/reservedWords'; import { transformTypeName } from '../../../utils/transform'; import { isDefinitionTypeNullable } from '../../v3/parser/inferType'; @@ -136,7 +137,7 @@ export const getType = ({ let encodedType = transformTypeName( ensureValidTypeScriptJavaScriptIdentifier(typeWithoutNamespace), ); - if (type.startsWith('#/components/parameters/')) { + if (type.startsWith(refParametersPartial)) { // prefix parameter names to avoid conflicts, assuming people are mostly // interested in importing schema types and don't care about this naming encodedType = `Parameter${encodedType}`; @@ -166,5 +167,6 @@ export const transformTypeKeyName = (value: string): string => { } const clean = sanitizeOperationParameterName(value).trim(); - return camelcase(clean).replace(reservedWordsRegExp, '_$1'); + const name = camelcase(clean).replace(reservedWordsRegExp, '_$1'); + return name; }; diff --git a/packages/openapi-ts/src/openApi/v3/parser/getModelComposition.ts b/packages/openapi-ts/src/openApi/v3/parser/getModelComposition.ts index ed309ecf7..18e30d534 100644 --- a/packages/openapi-ts/src/openApi/v3/parser/getModelComposition.ts +++ b/packages/openapi-ts/src/openApi/v3/parser/getModelComposition.ts @@ -62,14 +62,15 @@ export const getModelComposition = ({ let properties: Model[] = []; definitions - .map((def) => - getModel({ + .map((def) => { + const modelFromDef = getModel({ definition: def, openApi, parentDefinition: definition, types, - }), - ) + }); + return modelFromDef; + }) .forEach((model) => { composition.$refs = [...composition.$refs, ...model.$refs]; composition.imports = [...composition.imports, ...model.imports]; diff --git a/packages/openapi-ts/src/openApi/v3/parser/getModels.ts b/packages/openapi-ts/src/openApi/v3/parser/getModels.ts index 3bc52d767..433e2818b 100644 --- a/packages/openapi-ts/src/openApi/v3/parser/getModels.ts +++ b/packages/openapi-ts/src/openApi/v3/parser/getModels.ts @@ -1,7 +1,6 @@ import type { Client } from '../../../types/client'; import { getConfig } from '../../../utils/config'; -import { reservedWordsRegExp } from '../../../utils/reservedWords'; -import { getType } from '../../common/parser/type'; +import { getParametersMeta, getSchemasMeta } from '../../../utils/meta'; import type { OpenApi } from '../interfaces/OpenApi'; import { getModel } from './getModel'; import { getParameterSchema } from './parameter'; @@ -23,13 +22,8 @@ export const getModels = ( Object.entries(openApi.components.schemas ?? {}).forEach( ([definitionName, definition]) => { - const definitionType = getType({ type: definitionName }); - const name = definitionType.base.replace(reservedWordsRegExp, '_$1'); - const meta = { - $ref: `#/components/schemas/${definitionName}`, - name, - }; - types[name] = meta; + const meta = getSchemasMeta(definitionName); + types[meta.name] = meta; const model = getModel({ definition, isDefinition: true, @@ -51,27 +45,8 @@ export const getModels = ( return; } - const definitionType = getType({ type: definitionName }); - /** - * Prefix parameter names to avoid name conflicts with schemas. - * Assuming people are mostly interested in importing schema types - * and don't care about this name as much. It should be resolved in - * a cleaner way, there just isn't a good deduplication strategy - * today. This is a workaround in the meantime, hopefully reducing - * the chance of conflicts. - * - * Example where this would break: schema named `ParameterFoo` and - * parameter named `Foo` (this would transform to `ParameterFoo`) - * - * 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 meta = { - $ref: `#/components/parameters/${definitionName}`, - name, - }; - types[name] = meta; + const meta = getParametersMeta(definitionName); + types[meta.name] = meta; const model = getModel({ definition: schema, isDefinition: true, diff --git a/packages/openapi-ts/src/openApi/v3/parser/getOperationParameter.ts b/packages/openapi-ts/src/openApi/v3/parser/getOperationParameter.ts index ed49e7678..7c6712525 100644 --- a/packages/openapi-ts/src/openApi/v3/parser/getOperationParameter.ts +++ b/packages/openapi-ts/src/openApi/v3/parser/getOperationParameter.ts @@ -1,5 +1,6 @@ import type { Client } from '../../../types/client'; import { getConfig, isStandaloneClient } from '../../../utils/config'; +import { refParametersPartial } from '../../../utils/const'; import { enumMeta } from '../../../utils/enum'; import type { OperationParameter } from '../../common/interfaces/client'; import { getDefault } from '../../common/parser/getDefault'; @@ -71,7 +72,7 @@ export const getOperationParameter = ({ let schema = getParameterSchema(parameter); if (schema) { - if (schema.$ref?.startsWith('#/components/parameters/')) { + if (schema.$ref?.startsWith(refParametersPartial)) { schema = getRef(openApi, schema); } diff --git a/packages/openapi-ts/src/utils/const.ts b/packages/openapi-ts/src/utils/const.ts new file mode 100644 index 000000000..3c01b4b75 --- /dev/null +++ b/packages/openapi-ts/src/utils/const.ts @@ -0,0 +1,3 @@ +export const refParametersPartial = '#/components/parameters/'; + +export const refSchemasPartial = '#/components/schemas/'; diff --git a/packages/openapi-ts/src/utils/meta.ts b/packages/openapi-ts/src/utils/meta.ts new file mode 100644 index 000000000..887868862 --- /dev/null +++ b/packages/openapi-ts/src/utils/meta.ts @@ -0,0 +1,37 @@ +import { getType } from '../openApi/common/parser/type'; +import { refParametersPartial, refSchemasPartial } from './const'; +import { reservedWordsRegExp } from './reservedWords'; + +export const getParametersMeta = (definitionName: string) => { + const definitionType = getType({ type: definitionName }); + /** + * Prefix parameter names to avoid name conflicts with schemas. + * Assuming people are mostly interested in importing schema types + * and don't care about this name as much. It should be resolved in + * a cleaner way, there just isn't a good deduplication strategy + * today. This is a workaround in the meantime, hopefully reducing + * the chance of conflicts. + * + * Example where this would break: schema named `ParameterFoo` and + * parameter named `Foo` (this would transform to `ParameterFoo`) + * + * 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 meta = { + $ref: refParametersPartial + definitionName, + name, + }; + return meta; +}; + +export const getSchemasMeta = (definitionName: string) => { + const definitionType = getType({ type: definitionName }); + const name = definitionType.base.replace(reservedWordsRegExp, '_$1'); + const meta = { + $ref: refSchemasPartial + definitionName, + name, + }; + return meta; +}; diff --git a/packages/openapi-ts/src/utils/type.ts b/packages/openapi-ts/src/utils/type.ts index e774f7dff..a80396dc3 100644 --- a/packages/openapi-ts/src/utils/type.ts +++ b/packages/openapi-ts/src/utils/type.ts @@ -3,8 +3,10 @@ import type { Model } from '../openApi'; import { transformTypeKeyName } from '../openApi/common/parser/type'; import type { Client } from '../types/client'; import { getConfig, isStandaloneClient } from './config'; +import { refSchemasPartial } from './const'; import { enumValue } from './enum'; import { escapeComment, escapeName, unescapeName } from './escape'; +import { getSchemasMeta } from './meta'; import { unique } from './unique'; const base = (model: Model) => { @@ -27,7 +29,17 @@ const base = (model: Model) => { const typeReference = (model: Model) => { // nullable is false when base is null to avoid duplicate null statements const isNullable = model.base === 'null' ? false : model.isNullable; - const unionNode = compiler.typedef.union([base(model)], isNullable); + let typeNode = base(model); + // special handling for single reference. The current approach didn't + // handle transformed names, this fixes that. We should add a more robust + // solution, but this will work for now. + if (model.export === 'reference' && model.$refs.length === 1) { + if (model.$refs[0].startsWith(refSchemasPartial)) { + const meta = getSchemasMeta(model.base); + typeNode = compiler.typedef.basic(meta.name); + } + } + const unionNode = compiler.typedef.union([typeNode], isNullable); return unionNode; }; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap index 64841a9cf..44c096b6b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap @@ -509,7 +509,7 @@ export class ResponseService { } /** - * @returns ModelWithString + * @returns import * @throws ApiError */ public static callWithResponse(): CancelablePromise { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap index 1abe591c8..46c9a0b2a 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap @@ -887,8 +887,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -987,6 +987,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1255,7 +1260,7 @@ export type CallWithNoContentResponseResponse = void; export type CallWithResponseAndNoContentResponseResponse = number | void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1530,7 +1535,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap index 8afc9f67a..e3198fd30 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap @@ -566,7 +566,7 @@ export class ResponseService { } /** - * @returns ModelWithString + * @returns import * @throws ApiError */ public callWithResponse(): Observable { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap index 99fea7d67..e215e1bc0 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap @@ -777,8 +777,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -864,6 +864,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1132,7 +1137,7 @@ export type CallWithNoContentResponseResponse = void; export type CallWithResponseAndNoContentResponseResponse = number | void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1407,7 +1412,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/services.gen.ts.snap index 64841a9cf..44c096b6b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/services.gen.ts.snap @@ -509,7 +509,7 @@ export class ResponseService { } /** - * @returns ModelWithString + * @returns import * @throws ApiError */ public static callWithResponse(): CancelablePromise { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap index 1abe591c8..46c9a0b2a 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap @@ -887,8 +887,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -987,6 +987,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1255,7 +1260,7 @@ export type CallWithNoContentResponseResponse = void; export type CallWithResponseAndNoContentResponseResponse = number | void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1530,7 +1535,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap index 5ace9ad56..1c5aa3829 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap @@ -530,7 +530,7 @@ export class ResponseService { } /** - * @returns ModelWithString + * @returns import * @throws ApiError */ public callWithResponse(): CancelablePromise { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap index 3b0652a91..39bc9a6a5 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap @@ -777,8 +777,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -864,6 +864,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1132,7 +1137,7 @@ export type CallWithNoContentResponseResponse = void; export type CallWithResponseAndNoContentResponseResponse = number | void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1407,7 +1412,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/types.gen.ts.snap index f710e9be1..454bd7396 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/types.gen.ts.snap @@ -277,7 +277,7 @@ export type DummyAResponse = void; export type DummyBResponse = void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -568,7 +568,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap index 64841a9cf..44c096b6b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap @@ -509,7 +509,7 @@ export class ResponseService { } /** - * @returns ModelWithString + * @returns import * @throws ApiError */ public static callWithResponse(): CancelablePromise { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap index b4755defb..dd16dce6a 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap @@ -848,8 +848,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -943,6 +943,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1211,7 +1216,7 @@ export type CallWithNoContentResponseResponse = void; export type CallWithResponseAndNoContentResponseResponse = number | void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1486,7 +1491,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap index 0beda34f0..687c785ee 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap @@ -887,8 +887,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -987,6 +987,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1289,7 +1294,7 @@ export type DummyBResponse = void; export type DummyBError = unknown; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithResponseError = unknown; @@ -1615,7 +1620,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap index 0beda34f0..687c785ee 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap @@ -887,8 +887,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -987,6 +987,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1289,7 +1294,7 @@ export type DummyBResponse = void; export type DummyBError = unknown; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithResponseError = unknown; @@ -1615,7 +1620,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/services.gen.ts.snap index 64841a9cf..44c096b6b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/services.gen.ts.snap @@ -509,7 +509,7 @@ export class ResponseService { } /** - * @returns ModelWithString + * @returns import * @throws ApiError */ public static callWithResponse(): CancelablePromise { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap index 1abe591c8..46c9a0b2a 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap @@ -887,8 +887,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -987,6 +987,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1255,7 +1260,7 @@ export type CallWithNoContentResponseResponse = void; export type CallWithResponseAndNoContentResponseResponse = number | void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1530,7 +1535,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_pascalcase/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_pascalcase/types.gen.ts.snap index 00ab48ec0..88ff7cc9e 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_pascalcase/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_pascalcase/types.gen.ts.snap @@ -278,7 +278,7 @@ export type DummyAResponse = void; export type DummyBResponse = void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = Import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -569,7 +569,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: Import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap index b425dc3dc..bb12ef52b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap @@ -1518,7 +1518,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1605,4 +1605,8 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { type: 'string' +} as const; + +export const $import = { + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/services.gen.ts.snap index df65e1f51..3a7637ad9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/services.gen.ts.snap @@ -428,7 +428,7 @@ export const dummyB = (): CancelablePromise => { return __reques }); }; /** - * @returns ModelWithString + * @returns import * @throws ApiError */ export const callWithResponse = (): CancelablePromise => { return __request(OpenAPI, { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap index a8772d59f..1dcf807e0 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap @@ -887,8 +887,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -987,6 +987,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1259,7 +1264,7 @@ export type DummyAResponse = void; export type DummyBResponse = void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1550,7 +1555,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_types/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_types/types.gen.ts.snap index 30188512c..eac5dda0e 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_types/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_types/types.gen.ts.snap @@ -777,8 +777,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -864,6 +864,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1136,7 +1141,7 @@ export type DummyAResponse = void; export type DummyBResponse = void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1427,7 +1432,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_types_no_tree/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_types_no_tree/types.gen.ts.snap index b7291d70f..6b6dcc609 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_types_no_tree/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_types_no_tree/types.gen.ts.snap @@ -777,8 +777,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -864,6 +864,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1136,7 +1141,7 @@ export type DummyAResponse = void; export type DummyBResponse = void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap index 0fc34061f..2f090254b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap @@ -1637,7 +1637,7 @@ export const $ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = { type: 'number' }, { - type: 'string' + '$ref': '#/components/schemas/import' } ] }, @@ -1758,4 +1758,9 @@ export const $DeleteFooData = { export const $DeleteFooData2 = { description: 'Model used to test deduplication strategy', type: 'string' +} as const; + +export const $import = { + description: 'Model with restricted keyword name', + type: 'string' } as const; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/services.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/services.gen.ts.snap index 64841a9cf..44c096b6b 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/services.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/services.gen.ts.snap @@ -509,7 +509,7 @@ export class ResponseService { } /** - * @returns ModelWithString + * @returns import * @throws ApiError */ public static callWithResponse(): CancelablePromise { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap index 1abe591c8..46c9a0b2a 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap @@ -887,8 +887,8 @@ export type ModelWithAnyOfConstantSizeArrayNullable = [ ]; export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [ - number | string, - number | string + number | _import, + number | _import ]; export type ModelWithAnyOfConstantSizeArrayAndIntersect = [ @@ -987,6 +987,11 @@ export type DeleteFooData = string; */ export type DeleteFooData2 = string; +/** + * Model with restricted keyword name + */ +export type _import = string; + /** * This is a reusable parameter */ @@ -1255,7 +1260,7 @@ export type CallWithNoContentResponseResponse = void; export type CallWithResponseAndNoContentResponseResponse = number | void; -export type CallWithResponseResponse = ModelWithString; +export type CallWithResponseResponse = _import; export type CallWithDuplicateResponsesResponse = ModelWithBoolean & ModelWithInteger | ModelWithString; @@ -1530,7 +1535,7 @@ export type $OpenApiTs = { '/api/v{api-version}/response': { get: { res: { - default: ModelWithString; + default: _import; }; }; post: { diff --git a/packages/openapi-ts/test/sample.cjs b/packages/openapi-ts/test/sample.cjs index b01d03518..68e896ff7 100644 --- a/packages/openapi-ts/test/sample.cjs +++ b/packages/openapi-ts/test/sample.cjs @@ -31,6 +31,7 @@ const main = async () => { // include: // '^(ModelWithOneOfAndProperties|CompositionWithOneOfAndProperties)', // name: 'PascalCase', + // tree: false, }, // useOptions: false, }; diff --git a/packages/openapi-ts/test/spec/v3.json b/packages/openapi-ts/test/spec/v3.json index 53a8f10ac..b6cab0a8c 100644 --- a/packages/openapi-ts/test/spec/v3.json +++ b/packages/openapi-ts/test/spec/v3.json @@ -801,7 +801,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/ModelWithString" + "$ref": "#/components/schemas/import" } } } @@ -3240,7 +3240,7 @@ "type": "number" }, { - "type": "string" + "$ref": "#/components/schemas/import" } ] }, @@ -3326,6 +3326,10 @@ "DeleteFooData2": { "description": "Model used to test deduplication strategy", "type": "string" + }, + "import": { + "description": "Model with restricted keyword name", + "type": "string" } } }