Skip to content

Commit

Permalink
fix: use correct working dir when resolving multi-file definitions
Browse files Browse the repository at this point in the history
this fixes a bug caused by running the validator on a multi-file definition from any directory other than that of the root file
  • Loading branch information
dpopp07 committed Jun 18, 2019
1 parent f388518 commit 43a22b8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/cli-validator/runValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/cli-validator/mockFiles/multi-file-spec/main.yaml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions test/cli-validator/mockFiles/multi-file-spec/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
openapi: 3.0.0
paths: {}
components:
schemas:
SchemaDef:
type: object
35 changes: 30 additions & 5 deletions test/cli-validator/tests/expectedOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.'
);
});
});

0 comments on commit 43a22b8

Please sign in to comment.