diff --git a/packages/openapi-typescript/src/transform/components-object.ts b/packages/openapi-typescript/src/transform/components-object.ts index e5c100d26..420dbf092 100644 --- a/packages/openapi-typescript/src/transform/components-object.ts +++ b/packages/openapi-typescript/src/transform/components-object.ts @@ -78,9 +78,14 @@ export default function transformComponentsObject(componentsObject: ComponentsOb conflictCounter++; aliasName = `${componentKey}${changeCase.pascalCase(name)}_${conflictCounter}`; } + const ref = ts.factory.createTypeReferenceNode(`components['${key}']['${name}']`); if (ctx.rootTypesNoSchemaPrefix && key === "schemas") { - aliasName = aliasName.replace(componentKey, ""); + // Skipping --root-types generation only for enums if --enum is set + // while still applying --root-types-no-schema-prefix to other types. + if (!(ctx.enum && item.enum !== undefined)) { + aliasName = aliasName.replace(componentKey, ""); + } } const typeAlias = ts.factory.createTypeAliasDeclaration( /* modifiers */ tsModifiers({ export: true }), diff --git a/packages/openapi-typescript/test/transform/components-object.test.ts b/packages/openapi-typescript/test/transform/components-object.test.ts index 43116c8a7..8c9924fd2 100644 --- a/packages/openapi-typescript/test/transform/components-object.test.ts +++ b/packages/openapi-typescript/test/transform/components-object.test.ts @@ -760,6 +760,74 @@ export type Error = components['schemas']['Error']; options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true }, }, ], + [ + "options > rootTypes: true and rootTypesNoSchemaPrefix: true and enum: true", + { + given: { + schemas: { + Item: { + type: "object", + required: ["name", "url"], + properties: { + name: { type: "string" }, + url: { type: "string" }, + }, + }, + Document: { + type: "object", + required: ["name", "size", "url"], + properties: { + name: { type: "string" }, + size: { type: "number" }, + url: { type: "string" }, + }, + }, + Error: { + type: "object", + required: ["code", "message"], + properties: { + code: { type: "string" }, + message: { type: "string" }, + }, + }, + MyEnum: { + type: "string", + enum: ["first", "second", "last"], + }, + }, + }, + want: `{ + schemas: { + Item: { + name: string; + url: string; + }; + Document: { + name: string; + size: number; + url: string; + }; + Error: { + code: string; + message: string; + }; + /** @enum {string} */ + MyEnum: MyEnum; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} +export type Item = components['schemas']['Item']; +export type Document = components['schemas']['Document']; +export type Error = components['schemas']['Error']; +export type SchemaMyEnum = components['schemas']['MyEnum']; +`, + options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true, enum: true }, + }, + ], [ "transform > with transform object", {