From 5d4e27856e1fac85f7863794ff7cdbe225379fb5 Mon Sep 17 00:00:00 2001 From: Sergio Date: Fri, 6 Mar 2026 12:29:14 -0800 Subject: [PATCH] fix(cli-validator): allow empty-string JSON keys during duplicate-key check Signed-off-by: Sergio --- .../src/cli-validator/run-validator.js | 6 +++++- .../mock-files/oas3/empty-string-key.json | 20 +++++++++++++++++++ .../tests/error-handling.test.js | 16 +++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 packages/validator/test/cli-validator/mock-files/oas3/empty-string-key.json diff --git a/packages/validator/src/cli-validator/run-validator.js b/packages/validator/src/cli-validator/run-validator.js index d4c923e96..58718c4d9 100644 --- a/packages/validator/src/cli-validator/run-validator.js +++ b/packages/validator/src/cli-validator/run-validator.js @@ -212,7 +212,11 @@ async function runValidator(cliArgs, parseOptions = {}) { // jsonValidator looks through the originalFile for duplicate JSON keys // this is checked for by default in readYaml const duplicateKeysError = jsonValidator.validate(originalFile); - if (fileExtension === 'json' && duplicateKeysError) { + if ( + fileExtension === 'json' && + duplicateKeysError && + !duplicateKeysError.startsWith('Syntax error: empty string near') + ) { throw duplicateKeysError; } diff --git a/packages/validator/test/cli-validator/mock-files/oas3/empty-string-key.json b/packages/validator/test/cli-validator/mock-files/oas3/empty-string-key.json new file mode 100644 index 000000000..821f97ad1 --- /dev/null +++ b/packages/validator/test/cli-validator/mock-files/oas3/empty-string-key.json @@ -0,0 +1,20 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Empty string key test", + "version": "1.0.0" + }, + "paths": {}, + "components": { + "schemas": { + "Sample": { + "type": "object", + "properties": { + "": { + "type": "string" + } + } + } + } + } +} diff --git a/packages/validator/test/cli-validator/tests/error-handling.test.js b/packages/validator/test/cli-validator/tests/error-handling.test.js index b3f85eb3d..0507a8a5f 100644 --- a/packages/validator/test/cli-validator/tests/error-handling.test.js +++ b/packages/validator/test/cli-validator/tests/error-handling.test.js @@ -158,6 +158,22 @@ describe('cli tool - test error handling', function () { ); }); + it('should not reject valid JSON that uses an empty string key', async function () { + let exitCode; + try { + exitCode = await testValidator([ + './test/cli-validator/mock-files/oas3/empty-string-key.json', + ]); + } catch (err) { + exitCode = err; + } + + const capturedText = getCapturedText(consoleSpy.mock.calls); + + expect(exitCode).toEqual(0); + expect(capturedText.join('\n')).not.toContain('Syntax error: empty string near'); + }); + it('should return an error when a JSON document contains a trailing comma', async function () { let exitCode; try {