Skip to content

Commit

Permalink
json schema mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 24, 2019
1 parent 8da3c03 commit ddb0efe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/framework/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class OpenAPIFramework {
visitor: OpenAPIFrameworkVisitor,
): Promise<OpenAPIFrameworkInit> {
const args = this.args;
const apiDoc = await this.copy(await this.loadSpec(args.apiDoc, args.$refParser));
const apiDoc = await this.copy(
await this.loadSpec(args.apiDoc, args.$refParser),
);
const basePathObs = this.getBasePathsFromServers(apiDoc.servers);
const basePaths = Array.from(
basePathObs.reduce((acc, bp) => {
Expand Down Expand Up @@ -73,7 +75,8 @@ export class OpenAPIFramework {

private loadSpec(
filePath: string | object,
$refParser: { mode: 'bundle' | 'dereference' } = { mode: 'bundle' }): Promise<OpenAPIV3.Document> {
$refParser: { mode: 'bundle' | 'dereference' } = { mode: 'bundle' },
): Promise<OpenAPIV3.Document> {
// Because of this issue ( https://github.com/APIDevTools/json-schema-ref-parser/issues/101#issuecomment-421755168 )
// We need this workaround ( use '$RefParser.dereference' instead of '$RefParser.bundle' ) if asked by user
if (typeof filePath === 'string') {
Expand All @@ -88,7 +91,9 @@ export class OpenAPIFramework {
fs.readFileSync(absolutePath, 'utf8'),
{ json: true },
);
return ($refParser.mode === 'bundle') ? $RefParser.bundle(docWithRefs) : $RefParser.dereference(docWithRefs);
return $refParser.mode === 'dereference'
? $RefParser.dereference(docWithRefs)
: $RefParser.bundle(docWithRefs);
} finally {
process.chdir(origCwd);
}
Expand All @@ -98,7 +103,9 @@ export class OpenAPIFramework {
);
}
}
return ($refParser.mode === 'bundle') ? $RefParser.bundle(filePath) : $RefParser.dereference(filePath);
return $refParser.mode === 'dereference'
? $RefParser.dereference(filePath)
: $RefParser.bundle(filePath);
}

private copy<T>(obj: T): T {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class OpenApiValidator {
if (options.validateRequests == null) options.validateRequests = true;
if (options.validateResponses == null) options.validateResponses = false;
if (options.validateSecurity == null) options.validateSecurity = true;
if (options.$refParser == null) options.$refParser = {mode: 'bundle'};
if (options.$refParser == null) options.$refParser = { mode: 'bundle' };

if (options.validateResponses === true) {
options.validateResponses = {
Expand Down

0 comments on commit ddb0efe

Please sign in to comment.