From ee2f62f675507947694d889425da47b35f1f88a7 Mon Sep 17 00:00:00 2001 From: Joseph DeChicchis Date: Thu, 8 Jan 2026 10:33:10 -0500 Subject: [PATCH 1/2] Fix config file boolean options being ignored Boolean CLI options with defaults (keepComments, sortComponentsProps, split) were not being respected when set in a config file. This happened because Commander.js always sets these options to their default values, and the subsequent Object.assign would overwrite config file values with CLI defaults. The fix applies the same pattern already used for lineWidth, sort, and bundle: explicitly check for config file values before the merge, using nullish coalescing to preserve CLI-specified values when present. Co-Authored-By: Claude Opus 4.5 --- bin/cli.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/cli.js b/bin/cli.js index 6414e9f..9ea4c86 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -90,9 +90,14 @@ async function run(oaFile, options) { // Merge .openapiformatrc and configOptions configOptions = Object.assign({}, defaultOptions, configOptions); + // Preserve config file values for options that have CLI defaults + // These must be set before Object.assign to prevent CLI defaults from overwriting config values options.lineWidth = configOptions?.lineWidth ?? options.lineWidth; options.sort = configOptions?.sort ?? options.sort; options.bundle = configOptions?.bundle ?? options.bundle; + options.keepComments = configOptions?.keepComments ?? options.keepComments; + options.sortComponentsProps = configOptions?.sortComponentsProps ?? options.sortComponentsProps; + options.split = configOptions?.split ?? options.split; // Merge configOptions and CLI options options = Object.assign({}, configOptions, options); From f04cd0e3ee2aea6abe57323c693c20b8a5c02b05 Mon Sep 17 00:00:00 2001 From: Joseph DeChicchis Date: Thu, 8 Jan 2026 11:00:56 -0500 Subject: [PATCH 2/2] Update tests --- bin/__snapshots__/cli.test.js.snap | 11 ++++++ bin/cli.test.js | 57 ++++++++++++++++++++++++++++ test/_cli-configfile/configFile.json | 5 ++- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/bin/__snapshots__/cli.test.js.snap b/bin/__snapshots__/cli.test.js.snap index 18b0c56..53049e0 100644 --- a/bin/__snapshots__/cli.test.js.snap +++ b/bin/__snapshots__/cli.test.js.snap @@ -132,6 +132,17 @@ Options: " `; +exports[`openapi-format CLI command should respect boolean options from .openapiformatrc 1`] = ` +"================================================================================ +OpenAPI-Format CLI settings: +- .openapiformatrc: /Users/joseph/oss/openapi-format/.openapiformatrc +- Input file: test/yaml-no-sort-keep-comments/input.yaml +================================================================================ +✅ OpenAPI formatted successfully +================================================================================ +" +`; + exports[`openapi-format CLI command should show unused components 1`] = ` "================================================================================ OpenAPI-Format CLI settings: diff --git a/bin/cli.test.js b/bin/cli.test.js index 6f4b2bd..3f1297d 100644 --- a/bin/cli.test.js +++ b/bin/cli.test.js @@ -146,6 +146,63 @@ describe('openapi-format CLI command', () => { fs.readFileSync.mockRestore(); }); + it('should respect boolean options from .openapiformatrc', async () => { + // Create a temporary .openapiformatrc file with boolean options + const defaultConfigPath = '.openapiformatrc'; + const configContent = { + keepComments: true, + sortComponentsProps: true, + split: false + }; + + // Write the config file + fs.writeFileSync(defaultConfigPath, JSON.stringify(configContent, null, 2)); + + try { + const inputFile = `test/yaml-no-sort-keep-comments/input.yaml`; + let result = await testUtils.cli([inputFile, '--no-sort'], '.'); + expect(result.code).toBe(0); + expect(result.stdout).toContain('formatted successfully'); + expect(result.stdout).toMatchSnapshot(); + // The output should preserve comments due to keepComments: true in config + expect(result.stderr).toContain('#'); + } finally { + // Clean up - remove the temporary config file + if (fs.existsSync(defaultConfigPath)) { + fs.unlinkSync(defaultConfigPath); + } + } + }); + + it('should use config file boolean options when set', async () => { + // Create a config file with boolean options set to non-default values + const defaultConfigPath = '.openapiformatrc'; + const configContent = { + keepComments: true, + sortComponentsProps: true, + split: false, + sort: false + }; + + // Write the config file + fs.writeFileSync(defaultConfigPath, JSON.stringify(configContent, null, 2)); + + try { + const inputFile = `test/yaml-no-sort-keep-comments/input.yaml`; + // Config file values should be applied + let result = await testUtils.cli([inputFile], '.'); + expect(result.code).toBe(0); + expect(result.stdout).toContain('formatted successfully'); + // The output should preserve comments due to config file setting + expect(result.stderr).toContain('#'); + } finally { + // Clean up - remove the temporary config file + if (fs.existsSync(defaultConfigPath)) { + fs.unlinkSync(defaultConfigPath); + } + } + }); + it('should use the casingFile', async () => { const path = `test/yaml-casing`; const inputFile = `${path}/input.yaml`; diff --git a/test/_cli-configfile/configFile.json b/test/_cli-configfile/configFile.json index 8ecc1fb..06362cc 100644 --- a/test/_cli-configfile/configFile.json +++ b/test/_cli-configfile/configFile.json @@ -22,5 +22,8 @@ }, "no-sort": false, "lineWidth": 80, - "rename": "Updated API" + "rename": "Updated API", + "keepComments": true, + "sortComponentsProps": true, + "split": false } \ No newline at end of file