Skip to content

Commit 43a22b8

Browse files
authored
fix: use correct working dir when resolving multi-file definitions
this fixes a bug caused by running the validator on a multi-file definition from any directory other than that of the root file
1 parent f388518 commit 43a22b8

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

src/cli-validator/runValidator.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ const processInput = async function(program) {
197197
continue;
198198
}
199199

200+
// change working directory to location of root api definition
201+
// this will allow the parser in `buildSwaggerObject` to resolve external refs correctly
202+
const originalWorkingDirectory = process.cwd();
203+
process.chdir(path.dirname(validFile));
204+
200205
// validator requires the swagger object to follow a specific format
201206
let swagger;
202207
try {
@@ -207,6 +212,11 @@ const processInput = async function(program) {
207212
// console.log(err.stack);
208213
exitCode = 1;
209214
continue;
215+
} finally {
216+
// return the working directory to its original location so that
217+
// the rest of the program runs as expected. using finally block
218+
// because this must happen regardless of result in buildSwaggerObject
219+
process.chdir(originalWorkingDirectory);
210220
}
211221

212222
// run validator, print the results, and determine if validator passed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openapi: 3.0.0
2+
paths:
3+
/example:
4+
get:
5+
summary: Summary
6+
responses:
7+
"200":
8+
description: OK
9+
content:
10+
application/json:
11+
schema:
12+
$ref: "./schema.yaml#/components/schemas/SchemaDef"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openapi: 3.0.0
2+
paths: {}
3+
components:
4+
schemas:
5+
SchemaDef:
6+
type: object

test/cli-validator/tests/expectedOutput.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,35 @@ describe('test expected output - OpenAPI 3', function() {
226226
const allOutput = capturedText.join('');
227227

228228
expect(exitCode).toEqual(0);
229-
expect(
230-
allOutput.includes(
231-
'./test/cli-validator/mockFiles/oas3/clean.yml passed the validator'
232-
)
233-
).toEqual(true);
229+
expect(allOutput).toContain(
230+
'./test/cli-validator/mockFiles/oas3/clean.yml passed the validator'
231+
);
232+
});
233+
234+
it('should catch problems in a multi-file spec from an outside directory', async function() {
235+
const capturedText = [];
236+
237+
const unhookIntercept = intercept(function(txt) {
238+
capturedText.push(stripAnsiFrom(txt));
239+
return '';
240+
});
241+
242+
const program = {};
243+
program.args = ['./test/cli-validator/mockFiles/multi-file-spec/main.yaml'];
244+
program.default_mode = true;
245+
246+
const exitCode = await commandLineValidator(program);
247+
248+
unhookIntercept();
249+
250+
const allOutput = capturedText.join('');
251+
252+
expect(exitCode).toEqual(1);
253+
expect(allOutput).toContain('errors');
254+
expect(allOutput).toContain('API definition must have an `info` object');
255+
expect(allOutput).toContain('warnings');
256+
expect(allOutput).toContain(
257+
'Operations must have a non-empty `operationId`.'
258+
);
234259
});
235260
});

0 commit comments

Comments
 (0)