diff --git a/src/cli-validator/runValidator.js b/src/cli-validator/runValidator.js index 5e51cec1e..d33a9a512 100644 --- a/src/cli-validator/runValidator.js +++ b/src/cli-validator/runValidator.js @@ -197,6 +197,11 @@ const processInput = async function(program) { continue; } + // change working directory to location of root api definition + // this will allow the parser in `buildSwaggerObject` to resolve external refs correctly + const originalWorkingDirectory = process.cwd(); + process.chdir(path.dirname(validFile)); + // validator requires the swagger object to follow a specific format let swagger; try { @@ -207,6 +212,11 @@ const processInput = async function(program) { // console.log(err.stack); exitCode = 1; continue; + } finally { + // return the working directory to its original location so that + // the rest of the program runs as expected. using finally block + // because this must happen regardless of result in buildSwaggerObject + process.chdir(originalWorkingDirectory); } // run validator, print the results, and determine if validator passed diff --git a/test/cli-validator/mockFiles/multi-file-spec/main.yaml b/test/cli-validator/mockFiles/multi-file-spec/main.yaml new file mode 100644 index 000000000..fa4cea29a --- /dev/null +++ b/test/cli-validator/mockFiles/multi-file-spec/main.yaml @@ -0,0 +1,12 @@ +openapi: 3.0.0 +paths: + /example: + get: + summary: Summary + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "./schema.yaml#/components/schemas/SchemaDef" diff --git a/test/cli-validator/mockFiles/multi-file-spec/schema.yaml b/test/cli-validator/mockFiles/multi-file-spec/schema.yaml new file mode 100644 index 000000000..4a4f824c2 --- /dev/null +++ b/test/cli-validator/mockFiles/multi-file-spec/schema.yaml @@ -0,0 +1,6 @@ +openapi: 3.0.0 +paths: {} +components: + schemas: + SchemaDef: + type: object diff --git a/test/cli-validator/tests/expectedOutput.js b/test/cli-validator/tests/expectedOutput.js index 79448664e..9ce292f9e 100644 --- a/test/cli-validator/tests/expectedOutput.js +++ b/test/cli-validator/tests/expectedOutput.js @@ -226,10 +226,35 @@ describe('test expected output - OpenAPI 3', function() { const allOutput = capturedText.join(''); expect(exitCode).toEqual(0); - expect( - allOutput.includes( - './test/cli-validator/mockFiles/oas3/clean.yml passed the validator' - ) - ).toEqual(true); + expect(allOutput).toContain( + './test/cli-validator/mockFiles/oas3/clean.yml passed the validator' + ); + }); + + it('should catch problems in a multi-file spec from an outside directory', async function() { + const capturedText = []; + + const unhookIntercept = intercept(function(txt) { + capturedText.push(stripAnsiFrom(txt)); + return ''; + }); + + const program = {}; + program.args = ['./test/cli-validator/mockFiles/multi-file-spec/main.yaml']; + program.default_mode = true; + + const exitCode = await commandLineValidator(program); + + unhookIntercept(); + + const allOutput = capturedText.join(''); + + expect(exitCode).toEqual(1); + expect(allOutput).toContain('errors'); + expect(allOutput).toContain('API definition must have an `info` object'); + expect(allOutput).toContain('warnings'); + expect(allOutput).toContain( + 'Operations must have a non-empty `operationId`.' + ); }); });