From 2d85b8bc68758aa1ea27d3ff244b7845fae5794b Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Tue, 10 Nov 2026 21:37:07 +0300 Subject: [PATCH 1/3] feat: add global -c/--config option and OPENAPI_GENERATOR_CLI_CONFIG env var to locate openapitools.json - resolution order: explicit flag (-c/--config or --openapitools) > OPENAPI_GENERATOR_CLI_CONFIG env var > ./openapitools.json (default, unchanged when neither is set) - relative paths are resolved against the cwd - fail fast with a clear error when an explicitly set config path does not exist - version-manager resolve argv detection now also skips the new option forms Fixes #782 --- apps/generator-cli/src/README.md | 28 ++++ apps/generator-cli/src/app/app.module.spec.ts | 4 + apps/generator-cli/src/app/app.module.ts | 20 ++- .../src/app/services/config.service.spec.ts | 154 +++++++++++++++--- .../src/app/services/config.service.ts | 31 +++- 5 files changed, 212 insertions(+), 25 deletions(-) diff --git a/apps/generator-cli/src/README.md b/apps/generator-cli/src/README.md index 3486db1f6cf..5fa7962e6d1 100644 --- a/apps/generator-cli/src/README.md +++ b/apps/generator-cli/src/README.md @@ -165,6 +165,34 @@ Further it is also possible to configure generators, for example: If `openapi-generator-cli generate` is called without further arguments, then the configuration is automatically used to generate your code. 🎉 +### Config file location + +By default, `openapi-generator-cli` reads and writes *openapitools.json* in the current working directory. +To use a config file somewhere else (e.g. in a monorepo with a shared configuration), pass a global option +before the command or set an environment variable: + +```sh +# global option (place it before the command) +openapi-generator-cli -c /path/to/openapitools.json generate -i spec.yaml -g ruby + +# the long form works as well and is equivalent to the existing --openapitools option +openapi-generator-cli --config /path/to/openapitools.json version-manager list + +# via environment variable, e.g. in CI +OPENAPI_GENERATOR_CLI_CONFIG=/path/to/openapitools.json openapi-generator-cli generate +``` + +The config file location is resolved in the following order: + +1. the global option `-c, --config ` or `--openapitools ` +2. the `OPENAPI_GENERATOR_CLI_CONFIG` environment variable +3. *./openapitools.json* (default) + +Relative paths are resolved against the current working directory. When an explicit path is given +(via option or environment variable) but the file does not exist, the CLI exits with an error instead +of silently creating a new configuration file. When neither the option nor the environment variable is +set, the default *./openapitools.json* is used (and created on first usage), just like before. + ##### Available placeholders diff --git a/apps/generator-cli/src/app/app.module.spec.ts b/apps/generator-cli/src/app/app.module.spec.ts index df3a81b6cdd..f38fe1f29a9 100644 --- a/apps/generator-cli/src/app/app.module.spec.ts +++ b/apps/generator-cli/src/app/app.module.spec.ts @@ -149,6 +149,10 @@ describe('AppModule', () => { describe.each([ ['the separate option value', ['--openapitools', 'x.json']], ['the inline option value', ['--openapitools=x.json']], + ['the separate -c option value', ['-c', 'x.json']], + ['the separate --config option value', ['--config', 'x.json']], + ['the inline --config option value', ['--config=x.json']], + ['the attached -c option value', ['-cx.json']], ])('version-manager resolve with %s', (_, configArgs) => { beforeEach(async () => { process.argv = [ diff --git a/apps/generator-cli/src/app/app.module.ts b/apps/generator-cli/src/app/app.module.ts index 3bdd5ee292e..5806dfbc8b9 100644 --- a/apps/generator-cli/src/app/app.module.ts +++ b/apps/generator-cli/src/app/app.module.ts @@ -5,8 +5,8 @@ import { ProxyAgent } from 'proxy-agent'; import { COMMANDER_PROGRAM, LOGGER } from './constants'; import { VersionManagerController } from './controllers/version-manager.controller'; +import { ConfigService } from './services/config.service'; import { - ConfigService, GeneratorService, PassThroughService, UIService, @@ -49,6 +49,12 @@ if (hasHttpsProxyEnvs) { .option( '--openapitools ', 'Use the specified openapi-generator-cli configuration file', + ) + .option( + '-c, --config ', + 'Use the specified openapi-generator-cli configuration file (alternative to --openapitools, also available via the ' + + ConfigService.envConfigVariable + + ' environment variable)', ), }, { provide: LOGGER, useValue: console }, @@ -96,12 +102,20 @@ export class AppModule implements OnApplicationBootstrap { while (commandIndex < args.length) { const arg = args[commandIndex]; - if (arg === '--openapitools') { + if ( + arg === '--openapitools' || + arg === '--config' || + arg === '-c' + ) { commandIndex += 2; continue; } - if (arg.startsWith('--openapitools=')) { + if ( + arg.startsWith('--openapitools=') || + arg.startsWith('--config=') || + (arg.startsWith('-c') && arg.length > 2) + ) { commandIndex += 1; continue; } diff --git a/apps/generator-cli/src/app/services/config.service.spec.ts b/apps/generator-cli/src/app/services/config.service.spec.ts index 9947c2f4f72..fd29de62a52 100644 --- a/apps/generator-cli/src/app/services/config.service.spec.ts +++ b/apps/generator-cli/src/app/services/config.service.spec.ts @@ -1,5 +1,6 @@ import { Test } from '@nestjs/testing'; import { Command, createCommand } from 'commander'; +import * as path from 'path'; import { ConfigService } from './config.service'; import { LOGGER, COMMANDER_PROGRAM } from '../constants'; @@ -194,33 +195,148 @@ describe('ConfigService', () => { }); describe('configFileOrDefault()', () => { + let exitSpy: jest.SpyInstance; + let originalEnv: NodeJS.ProcessEnv; + + const createFixture = async ( + opts: Record = {}, + ): Promise => { + program = createCommand(); + Object.assign(program.opts(), opts); + + fs.writeJSONSync.mockReset(); + fs.readJSONSync.mockReset(); + fs.ensureFileSync.mockReset(); + + const moduleRef = await Test.createTestingModule({ + providers: [ + ConfigService, + { provide: LOGGER, useValue: { log, error } }, + { provide: COMMANDER_PROGRAM, useValue: program }, + ], + }).compile(); + + return moduleRef.get(ConfigService); + }; + + beforeEach(() => { + fs.existsSync.mockReset(); + exitSpy = jest + .spyOn(process, 'exit') + .mockImplementation((() => undefined) as never); + originalEnv = { ...process.env }; + delete process.env[ConfigService.envConfigVariable]; + }); + + afterEach(() => { + exitSpy.mockRestore(); + process.env = { ...originalEnv }; + }); + describe('--openapitools set', () => { - beforeEach(async () => { - program = createCommand(); - program.opts().openapitools = '/tmp/myopenapitools.json'; - - const moduleRef = await Test.createTestingModule({ - providers: [ - ConfigService, - { provide: LOGGER, useValue: { log } }, - { provide: COMMANDER_PROGRAM, useValue: program }, - ], - }).compile(); - - fixture = moduleRef.get(ConfigService); - fs.writeJSONSync.mockReset(); - fs.readJSONSync.mockReset(); - fs.ensureFileSync.mockReset(); - }); - it('returns path set at cli, if openapitools argument provided', () => { + it('returns path set at cli, if openapitools argument provided', async () => { + fs.existsSync.mockReturnValue(true); + const fixture = await createFixture({ + openapitools: '/tmp/myopenapitools.json', + }); expect(fixture.configFile).toEqual('/tmp/myopenapitools.json'); }); }); + + describe('-c/--config set', () => { + it('returns the path provided via --config', async () => { + fs.existsSync.mockReturnValue(true); + const fixture = await createFixture({ + config: '/tmp/my-other-openapitools.json', + }); + expect(fixture.configFile).toEqual('/tmp/my-other-openapitools.json'); + }); + + it('resolves a relative path against the cwd', async () => { + fs.existsSync.mockReturnValue(true); + const fixture = await createFixture({ config: 'conf/openapi.json' }); + expect(fixture.configFile).toEqual( + path.resolve(fixture.cwd, 'conf/openapi.json'), + ); + }); + }); + + describe(ConfigService.envConfigVariable + ' set', () => { + it('returns the path set via the environment variable', async () => { + fs.existsSync.mockReturnValue(true); + process.env[ConfigService.envConfigVariable] = + '/tmp/env-openapitools.json'; + const fixture = await createFixture(); + expect(fixture.configFile).toEqual('/tmp/env-openapitools.json'); + }); + + it('resolves a relative environment variable path against the cwd', async () => { + fs.existsSync.mockReturnValue(true); + process.env[ConfigService.envConfigVariable] = 'conf/openapi.json'; + const fixture = await createFixture(); + expect(fixture.configFile).toEqual( + path.resolve(fixture.cwd, 'conf/openapi.json'), + ); + }); + }); + + describe('a cli option and the environment variable are set', () => { + it('the cli option takes precedence over the environment variable', async () => { + fs.existsSync.mockReturnValue(true); + process.env[ConfigService.envConfigVariable] = + '/tmp/env-openapitools.json'; + const fixture = await createFixture({ + config: '/tmp/cli-openapitools.json', + }); + expect(fixture.configFile).toEqual('/tmp/cli-openapitools.json'); + }); + }); + describe('--openapitools not set', () => { - it('returns default path, if openapitools argument not provided', () => { + it('returns default path, if openapitools argument not provided', async () => { + const fixture = await createFixture(); + expect(fixture.configFile).toEqual( + expect.stringMatching(/[/\\]openapitools\.json$/), + ); + }); + }); + + describe('the explicit config file does not exist', () => { + it('logs an error and exits, if the path was set via a cli option', async () => { + fs.existsSync.mockReturnValue(false); + const fixture = await createFixture({ + config: '/tmp/not-existing-openapitools.json', + }); + expect(fixture.configFile).toEqual('/tmp/not-existing-openapitools.json'); + expect(error).toHaveBeenCalledWith( + expect.stringContaining('/tmp/not-existing-openapitools.json'), + ); + expect(exitSpy).toHaveBeenCalledWith(1); + }); + + it('logs an error and exits, if the path was set via the environment variable', async () => { + fs.existsSync.mockReturnValue(false); + process.env[ConfigService.envConfigVariable] = + '/tmp/not-existing-openapitools.json'; + await createFixture(); + expect(error).toHaveBeenCalledWith( + expect.stringContaining('/tmp/not-existing-openapitools.json'), + ); + expect(error).toHaveBeenCalledWith( + expect.stringContaining(ConfigService.envConfigVariable), + ); + expect(exitSpy).toHaveBeenCalledWith(1); + }); + + it('does not exit, if the default config file does not exist', async () => { + fs.existsSync.mockReturnValue(false); + const errorCallsBefore = error.mock.calls.length; + const fixture = await createFixture(); expect(fixture.configFile).toEqual( expect.stringMatching(/[/\\]openapitools\.json$/), ); + expect(exitSpy).not.toHaveBeenCalled(); + expect(error.mock.calls.length).toBe(errorCallsBefore); }); }); }); diff --git a/apps/generator-cli/src/app/services/config.service.ts b/apps/generator-cli/src/app/services/config.service.ts index e7a68f345c4..44e54950fd6 100644 --- a/apps/generator-cli/src/app/services/config.service.ts +++ b/apps/generator-cli/src/app/services/config.service.ts @@ -6,19 +6,44 @@ import { Command } from 'commander'; @Injectable() export class ConfigService { + /** + * Environment variable pointing to an openapitools.json outside of the cwd. + * Precedence: cli option (-c/--config or --openapitools) > env var > ./openapitools.json + */ + public static readonly envConfigVariable = 'OPENAPI_GENERATOR_CLI_CONFIG'; + public readonly cwd = process.env.PWD || process.env.INIT_CWD || process.cwd(); public readonly configFile = this.configFileOrDefault(); private configFileOrDefault() { this.program.parseOptions(process.argv); - const conf = this.program.opts().openapitools; + const opts = this.program.opts(); + const explicitConfigPath = + opts.openapitools || + opts.config || + process.env[ConfigService.envConfigVariable]; - if (!conf) { + if (!explicitConfigPath) { return path.resolve(this.cwd, 'openapitools.json'); } - return path.isAbsolute(conf) ? conf : path.resolve(this.cwd, conf); + const configPath = path.isAbsolute(explicitConfigPath) + ? explicitConfigPath + : path.resolve(this.cwd, explicitConfigPath); + + if (!fs.existsSync(configPath)) { + this.logger.error( + `Config file not found at "${configPath}". It was set via ${ + opts.openapitools || opts.config + ? 'the -c/--config or --openapitools option' + : `the ${ConfigService.envConfigVariable} environment variable` + }. Please make sure the file exists, or unset it to use the default ./openapitools.json.`, + ); + process.exit(1); + } + + return configPath; } public get useDocker() { From 584f43f6637407488a35e90ec0baf454fe027a8b Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Tue, 10 Nov 2026 23:21:15 +0300 Subject: [PATCH 2/3] fix(config): only treat -c/--config as a global option before the command name The global '-c, --config ' option collided with the Java generator's own 'generate -c, --config ' option (upstream Generate.java defines name = {"-c", "--config"} for its generator configuration file). Commander consumes registered root options from the whole argv - in both parseOptions (used by ConfigService) and parse - so '-c' after 'generate' was captured as the openapitools.json path: the CLI exited with 1 when that path did not exist, and the generator config never reached the Java process because commander had removed it from the subcommand arguments. Of the two discussed options, excluding the invoked command's argv from the global -c parsing (option a) is the only correct minimal fix: keeping the option registered would keep the collision, and dropping only the short alias would not be enough because the Java generate command also defines the long '--config' form. The documented UX is preserved: - the global -c/--config option is no longer registered with commander; it is still listed in the root help and only honored BEFORE the command name via extractGlobalConfigArgs(), which understands all option forms (-c , -c, --config , --config=). Everything after the first positional argument belongs to the invoked command and is passed through untouched, which also covers commands other than 'generate'. - AppModule hands commander an argv without the global config options, so an unrecognized option before the command name cannot bury the command (commander would otherwise skip its dispatch and print help). - --openapitools keeps its existing behavior (registered root option, honored in any position). Also clear the shared 'error' mock between tests in config.service.spec.ts so each missing-file assertion only sees its own test's log output. --- apps/generator-cli/src/README.md | 4 + apps/generator-cli/src/app/app.module.spec.ts | 60 +++++++ apps/generator-cli/src/app/app.module.ts | 30 +++- .../src/app/services/config.service.spec.ts | 157 +++++++++++++++--- .../src/app/services/config.service.ts | 96 ++++++++++- 5 files changed, 318 insertions(+), 29 deletions(-) diff --git a/apps/generator-cli/src/README.md b/apps/generator-cli/src/README.md index 5fa7962e6d1..22ea03cd079 100644 --- a/apps/generator-cli/src/README.md +++ b/apps/generator-cli/src/README.md @@ -193,6 +193,10 @@ Relative paths are resolved against the current working directory. When an expli of silently creating a new configuration file. When neither the option nor the environment variable is set, the default *./openapitools.json* is used (and created on first usage), just like before. +`-c` and `--config` must be placed **before** the command name — afterwards they belong to the +invoked command itself. For example, the config file in `openapi-generator-cli generate -c generator-config.yaml` +is the generator configuration read by the Java generator, not the *openapitools.json* location. + ##### Available placeholders diff --git a/apps/generator-cli/src/app/app.module.spec.ts b/apps/generator-cli/src/app/app.module.spec.ts index f38fe1f29a9..bcb6f82f937 100644 --- a/apps/generator-cli/src/app/app.module.spec.ts +++ b/apps/generator-cli/src/app/app.module.spec.ts @@ -195,6 +195,66 @@ describe('AppModule', () => { }); }); + describe('a command is invoked with its own -c/--config option', () => { + beforeEach(async () => { + process.argv = [ + 'foo', + 'baz', + 'generate', + '-i', + 'spec.yaml', + '-c', + 'gen-cfg.yaml', + ]; + programMock.parse.mockImplementation(() => undefined); + versionManagerServiceMock.getSelectedVersion.mockReturnValue('1.2.3'); + await fixture.onApplicationBootstrap(); + }); + + it('hands the command arguments untouched to commander', () => { + expect(programMock.parse).toHaveBeenCalledWith([ + 'foo', + 'baz', + 'generate', + '-i', + 'spec.yaml', + '-c', + 'gen-cfg.yaml', + ]); + }); + }); + + describe.each([ + ['the separate -c option value', ['-c', 'x.json']], + ['the separate --config option value', ['--config', 'x.json']], + ['the inline --config option value', ['--config=x.json']], + ['the attached -c option value', ['-cx.json']], + ])('a global -c/--config option is used as %s before the command', (_, configArgs) => { + beforeEach(async () => { + process.argv = [ + 'foo', + 'baz', + ...configArgs, + 'version-manager', + 'resolve', + 'latest', + ]; + programMock.parse.mockImplementation(() => undefined); + versionManagerServiceMock.getSelectedVersion.mockReturnValue('1.2.3'); + await fixture.onApplicationBootstrap(); + }); + + it('hands commander an argv without the config option, so the command is dispatched', () => { + expect(programMock.parse).toHaveBeenCalledWith([ + 'foo', + 'baz', + 'version-manager', + 'resolve', + 'latest', + ]); + }); + }); + describe('a custom generator is provided', () => { beforeEach(async () => { process.argv = [ diff --git a/apps/generator-cli/src/app/app.module.ts b/apps/generator-cli/src/app/app.module.ts index 5806dfbc8b9..c574aababb1 100644 --- a/apps/generator-cli/src/app/app.module.ts +++ b/apps/generator-cli/src/app/app.module.ts @@ -5,7 +5,10 @@ import { ProxyAgent } from 'proxy-agent'; import { COMMANDER_PROGRAM, LOGGER } from './constants'; import { VersionManagerController } from './controllers/version-manager.controller'; -import { ConfigService } from './services/config.service'; +import { + ConfigService, + extractGlobalConfigArgs, +} from './services/config.service'; import { GeneratorService, PassThroughService, @@ -50,11 +53,20 @@ if (hasHttpsProxyEnvs) { '--openapitools ', 'Use the specified openapi-generator-cli configuration file', ) - .option( - '-c, --config ', - 'Use the specified openapi-generator-cli configuration file (alternative to --openapitools, also available via the ' + + // The global `-c, --config ` option is intentionally NOT + // registered with commander: commander would consume it from the whole + // argv — including after the command name — which would swallow the + // Java generator's own `generate -c, --config ` option. + // Instead, it is only honored BEFORE the command name by ConfigService + // (see extractGlobalConfigArgs) and stripped from the argv handed to + // commander below. It is listed here so it still shows up in the help. + .addHelpText( + 'after', + '\nGlobal config option (must be placed before the command name):\n\n' + + ' -c, --config \n' + + ' Use the specified openapi-generator-cli configuration file (alternative to --openapitools, also available via the ' + ConfigService.envConfigVariable + - ' environment variable)', + ' environment variable)\n', ), }, { provide: LOGGER, useValue: console }, @@ -92,7 +104,13 @@ export class AppModule implements OnApplicationBootstrap { if (!isVersionManagerResolveCommand) { await this.passThroughService.init(); } - this.program.parse(process.argv); + // Remove the global -c/--config options from the argv handed to commander: + // they are not registered (see above), and an unrecognized option before + // the command name would make commander treat the command itself as an + // unknown argument and skip its dispatch. + this.program.parse( + extractGlobalConfigArgs(process.argv).argvWithoutGlobalConfigArgs, + ); }; private isVersionManagerResolveCommand() { diff --git a/apps/generator-cli/src/app/services/config.service.spec.ts b/apps/generator-cli/src/app/services/config.service.spec.ts index fd29de62a52..c4fb1fc88e8 100644 --- a/apps/generator-cli/src/app/services/config.service.spec.ts +++ b/apps/generator-cli/src/app/services/config.service.spec.ts @@ -15,7 +15,15 @@ describe('ConfigService', () => { const log = jest.fn(); const error = jest.fn(); + let originalArgv: string[]; + beforeEach(async () => { + // the config file is resolved from process.argv, pin it to a plain cli + // invocation so the runner's own arguments (e.g. jest's --config) are not + // mistaken for cli options + originalArgv = process.argv; + process.argv = ['node', 'openapi-generator-cli']; + program = createCommand(); jest.spyOn(program, 'helpInformation'); @@ -33,6 +41,10 @@ describe('ConfigService', () => { fs.ensureFileSync.mockReset(); }); + afterEach(() => { + process.argv = originalArgv; + }); + describe('API', () => { describe('get()', () => { describe.each([ @@ -199,10 +211,13 @@ describe('ConfigService', () => { let originalEnv: NodeJS.ProcessEnv; const createFixture = async ( - opts: Record = {}, + argv: string[] = ['node', 'openapi-generator-cli'], ): Promise => { - program = createCommand(); - Object.assign(program.opts(), opts); + process.argv = argv; + program = createCommand().option( + '--openapitools ', + 'Use the specified openapi-generator-cli configuration file', + ); fs.writeJSONSync.mockReset(); fs.readJSONSync.mockReset(); @@ -221,6 +236,7 @@ describe('ConfigService', () => { beforeEach(() => { fs.existsSync.mockReset(); + error.mockClear(); exitSpy = jest .spyOn(process, 'exit') .mockImplementation((() => undefined) as never); @@ -231,36 +247,108 @@ describe('ConfigService', () => { afterEach(() => { exitSpy.mockRestore(); process.env = { ...originalEnv }; + process.argv = originalArgv; }); describe('--openapitools set', () => { it('returns path set at cli, if openapitools argument provided', async () => { fs.existsSync.mockReturnValue(true); - const fixture = await createFixture({ - openapitools: '/tmp/myopenapitools.json', - }); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + '--openapitools', + '/tmp/myopenapitools.json', + ]); expect(fixture.configFile).toEqual('/tmp/myopenapitools.json'); }); }); - describe('-c/--config set', () => { - it('returns the path provided via --config', async () => { - fs.existsSync.mockReturnValue(true); - const fixture = await createFixture({ - config: '/tmp/my-other-openapitools.json', - }); - expect(fixture.configFile).toEqual('/tmp/my-other-openapitools.json'); - }); + describe('-c/--config set before the command', () => { + it.each([ + [ + 'the separate -c option value', + ['-c', '/tmp/my-other-openapitools.json'], + ], + [ + 'the separate --config option value', + ['--config', '/tmp/my-other-openapitools.json'], + ], + [ + 'the inline --config option value', + ['--config=/tmp/my-other-openapitools.json'], + ], + [ + 'the attached -c option value', + ['-c/tmp/my-other-openapitools.json'], + ], + ])( + 'returns the path provided via %s', + async (_, configArgs) => { + fs.existsSync.mockReturnValue(true); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + ...configArgs, + 'version-manager', + 'resolve', + ]); + expect(fixture.configFile).toEqual( + '/tmp/my-other-openapitools.json', + ); + }, + ); it('resolves a relative path against the cwd', async () => { fs.existsSync.mockReturnValue(true); - const fixture = await createFixture({ config: 'conf/openapi.json' }); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + '-c', + 'conf/openapi.json', + 'version-manager', + 'resolve', + ]); expect(fixture.configFile).toEqual( path.resolve(fixture.cwd, 'conf/openapi.json'), ); }); }); + describe('the config options of the invoked command', () => { + it.each([ + ['the separate -c option', ['generate', '-c', 'gen-cfg.yaml']], + [ + 'the separate --config option', + ['generate', '--config', 'gen-cfg.yaml'], + ], + [ + 'the inline --config option', + ['generate', '--config=gen-cfg.yaml'], + ], + ['the attached -c option', ['generate', '-cgen-cfg.yaml']], + [ + 'an option of another command', + ['version-manager', 'list', '-c', 'gen-cfg.yaml'], + ], + ])( + 'is left to the command: %s reaches the command and the default config is used', + async (_, commandArgs) => { + // even a not existing path must not be mistaken for the cli config + fs.existsSync.mockReturnValue(false); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + ...commandArgs, + ]); + expect(fixture.configFile).toEqual( + expect.stringMatching(/[/\\]openapitools\.json$/), + ); + expect(exitSpy).not.toHaveBeenCalled(); + expect(error).not.toHaveBeenCalled(); + }, + ); + }); + describe(ConfigService.envConfigVariable + ' set', () => { it('returns the path set via the environment variable', async () => { fs.existsSync.mockReturnValue(true); @@ -285,13 +373,35 @@ describe('ConfigService', () => { fs.existsSync.mockReturnValue(true); process.env[ConfigService.envConfigVariable] = '/tmp/env-openapitools.json'; - const fixture = await createFixture({ - config: '/tmp/cli-openapitools.json', - }); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + '-c', + '/tmp/cli-openapitools.json', + 'version-manager', + 'resolve', + ]); expect(fixture.configFile).toEqual('/tmp/cli-openapitools.json'); }); }); + describe('--openapitools and -c/--config are both set', () => { + it('--openapitools keeps precedence over -c/--config', async () => { + fs.existsSync.mockReturnValue(true); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + '--openapitools', + '/tmp/openapitools.json', + '-c', + '/tmp/config.json', + 'version-manager', + 'resolve', + ]); + expect(fixture.configFile).toEqual('/tmp/openapitools.json'); + }); + }); + describe('--openapitools not set', () => { it('returns default path, if openapitools argument not provided', async () => { const fixture = await createFixture(); @@ -304,9 +414,14 @@ describe('ConfigService', () => { describe('the explicit config file does not exist', () => { it('logs an error and exits, if the path was set via a cli option', async () => { fs.existsSync.mockReturnValue(false); - const fixture = await createFixture({ - config: '/tmp/not-existing-openapitools.json', - }); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + '-c', + '/tmp/not-existing-openapitools.json', + 'version-manager', + 'resolve', + ]); expect(fixture.configFile).toEqual('/tmp/not-existing-openapitools.json'); expect(error).toHaveBeenCalledWith( expect.stringContaining('/tmp/not-existing-openapitools.json'), diff --git a/apps/generator-cli/src/app/services/config.service.ts b/apps/generator-cli/src/app/services/config.service.ts index 44e54950fd6..7c883532c14 100644 --- a/apps/generator-cli/src/app/services/config.service.ts +++ b/apps/generator-cli/src/app/services/config.service.ts @@ -4,6 +4,97 @@ import { COMMANDER_PROGRAM, LOGGER } from '../constants'; import * as fs from 'fs-extra'; import { Command } from 'commander'; +/** + * Result of scanning argv for the global config options. + */ +export interface GlobalConfigArgs { + /** + * The value of the global `-c/--config ` option, if it was used before + * the command name; `undefined` otherwise. + */ + configPath: string | undefined; + /** + * The given argv without the global `-c/--config ` options, so they are + * not seen (and not passed on) by the invoked command. + */ + argvWithoutGlobalConfigArgs: string[]; +} + +/** + * Extracts the global `-c/--config ` occurrences from `argv`. + * + * Only occurrences placed BEFORE the first positional argument (the command + * name) belong to the CLI itself. Afterwards, `-c` and `--config` are options + * of the invoked command — e.g. the Java generator defines its own + * `generate -c, --config ` — so the scan stops there and those + * arguments are returned untouched. + */ +export function extractGlobalConfigArgs(argv: string[]): GlobalConfigArgs { + const args = argv.slice(2); // skip the node executable and the script path + const occurrences: Array<{ index: number; hasSeparateValue: boolean }> = []; + + let index = 0; + while (index < args.length) { + const arg = args[index]; + + if (arg === '--openapitools') { + // global option with a separate value, skip it and its value + index += 2; + continue; + } + if (arg.startsWith('--openapitools=')) { + index += 1; + continue; + } + if (arg === '-c' || arg === '--config') { + occurrences.push({ index, hasSeparateValue: true }); + index += 2; + continue; + } + if (arg.startsWith('--config=')) { + occurrences.push({ index, hasSeparateValue: false }); + index += 1; + continue; + } + if (arg.startsWith('-c') && arg.length > 2) { + // attached value, e.g. -cfoo.json + occurrences.push({ index, hasSeparateValue: false }); + index += 1; + continue; + } + + // the command name or an option of the invoked command: + // everything from here on belongs to the command + break; + } + + if (occurrences.length === 0) { + return { configPath: undefined, argvWithoutGlobalConfigArgs: argv }; + } + + const configArgs = new Set(); + let configPath: string | undefined; + for (const { index, hasSeparateValue } of occurrences) { + configArgs.add(index); + if (hasSeparateValue) { + configArgs.add(index + 1); + configPath = args[index + 1]; + } else if (args[index].startsWith('--config=')) { + configPath = args[index].slice('--config='.length); + } else { + configPath = args[index].slice(2); + } + } + + return { + configPath, + argvWithoutGlobalConfigArgs: [ + ...argv.slice(0, 2), + ...args.filter((_, i) => !configArgs.has(i)), + ], + }; +} + @Injectable() export class ConfigService { /** @@ -19,9 +110,10 @@ export class ConfigService { private configFileOrDefault() { this.program.parseOptions(process.argv); const opts = this.program.opts(); + const globalConfigArgs = extractGlobalConfigArgs(process.argv); const explicitConfigPath = opts.openapitools || - opts.config || + globalConfigArgs.configPath || process.env[ConfigService.envConfigVariable]; if (!explicitConfigPath) { @@ -35,7 +127,7 @@ export class ConfigService { if (!fs.existsSync(configPath)) { this.logger.error( `Config file not found at "${configPath}". It was set via ${ - opts.openapitools || opts.config + opts.openapitools || globalConfigArgs.configPath ? 'the -c/--config or --openapitools option' : `the ${ConfigService.envConfigVariable} environment variable` }. Please make sure the file exists, or unset it to use the default ./openapitools.json.`, From 31864a8720d28f52ec3128fc73ce346979c88dfb Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Wed, 11 Nov 2026 16:08:18 +0300 Subject: [PATCH 3/3] fix(config): address review remarks on global -c/--config scanning - the scan now skips root options that take their own value (--custom-generator, in both separate and inline forms) together with their values, so --custom-generator ./gen.jar -c my.json resolve does not silently lose the config path - a trailing -c/--config without a value is rejected with a clear error instead of silently falling back to the environment variable or the default ./openapitools.json - the spec suite clears OPENAPI_GENERATOR_CLI_CONFIG in the top-level beforeEach, so a host shell exporting it cannot push every ConfigService construction in the suite onto the not-found path --- .../src/app/services/config.service.spec.ts | 33 +++++++++++++++++++ .../src/app/services/config.service.ts | 18 ++++++++++ 2 files changed, 51 insertions(+) diff --git a/apps/generator-cli/src/app/services/config.service.spec.ts b/apps/generator-cli/src/app/services/config.service.spec.ts index c4fb1fc88e8..5a7fcb428a4 100644 --- a/apps/generator-cli/src/app/services/config.service.spec.ts +++ b/apps/generator-cli/src/app/services/config.service.spec.ts @@ -23,6 +23,10 @@ describe('ConfigService', () => { // mistaken for cli options originalArgv = process.argv; process.argv = ['node', 'openapi-generator-cli']; + // keep the host environment from leaking into the resolution: an exported + // OPENAPI_GENERATOR_CLI_CONFIG would send every ConfigService construction + // in this suite down the config-not-found path + delete process.env[ConfigService.envConfigVariable]; program = createCommand(); jest.spyOn(program, 'helpInformation'); @@ -312,6 +316,35 @@ describe('ConfigService', () => { path.resolve(fixture.cwd, 'conf/openapi.json'), ); }); + + it('skips root options with a value, e.g. --custom-generator, before the command', async () => { + fs.existsSync.mockReturnValue(true); + const fixture = await createFixture([ + 'node', + 'openapi-generator-cli', + '--custom-generator', + './my-generator.jar', + '-c', + '/tmp/my-other-openapitools.json', + 'version-manager', + 'resolve', + ]); + expect(fixture.configFile).toEqual( + '/tmp/my-other-openapitools.json', + ); + }); + + it('rejects a trailing -c without a value', async () => { + await expect( + createFixture(['node', 'openapi-generator-cli', '-c']), + ).rejects.toThrow('The -c/--config option requires a value'); + }); + + it('rejects a trailing --config without a value', async () => { + await expect( + createFixture(['node', 'openapi-generator-cli', '--config']), + ).rejects.toThrow('The -c/--config option requires a value'); + }); }); describe('the config options of the invoked command', () => { diff --git a/apps/generator-cli/src/app/services/config.service.ts b/apps/generator-cli/src/app/services/config.service.ts index 7c883532c14..2adc070f6e9 100644 --- a/apps/generator-cli/src/app/services/config.service.ts +++ b/apps/generator-cli/src/app/services/config.service.ts @@ -28,6 +28,10 @@ export interface GlobalConfigArgs { * of the invoked command — e.g. the Java generator defines its own * `generate -c, --config ` — so the scan stops there and those * arguments are returned untouched. + * + * Root options that take their own value are skipped together with the value + * (e.g. `--openapitools`, `--custom-generator`), so a value that looks like a + * positional argument is not mistaken for the command name. */ export function extractGlobalConfigArgs(argv: string[]): GlobalConfigArgs { const args = argv.slice(2); // skip the node executable and the script path @@ -46,6 +50,15 @@ export function extractGlobalConfigArgs(argv: string[]): GlobalConfigArgs { index += 1; continue; } + if (arg === '--custom-generator') { + // root option with a separate value, skip it and its value + index += 2; + continue; + } + if (arg.startsWith('--custom-generator=')) { + index += 1; + continue; + } if (arg === '-c' || arg === '--config') { occurrences.push({ index, hasSeparateValue: true }); index += 2; @@ -77,6 +90,11 @@ export function extractGlobalConfigArgs(argv: string[]): GlobalConfigArgs { for (const { index, hasSeparateValue } of occurrences) { configArgs.add(index); if (hasSeparateValue) { + if (args[index + 1] === undefined) { + throw new Error( + 'The -c/--config option requires a value (a path to an openapitools.json file)', + ); + } configArgs.add(index + 1); configPath = args[index + 1]; } else if (args[index].startsWith('--config=')) {