Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: resolve $refs relative to document #179

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class Document {
* @param {Object[]} parsedJSONList
* @param {Object} base
*/
constructor(parsedJSONList: AsyncAPIObject[], base: AsyncAPIObject) {
constructor(parsedJSONList: AsyncAPIObject[], base?: AsyncAPIObject) {
for (const resolvedJSON of parsedJSONList) {
this._doc = merge(this._doc, resolvedJSON);
}

if (typeof base !== 'undefined') {
if (base) {
this._doc = merge(this._doc, base);
}
}
Expand Down
27 changes: 18 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Document } from './document';
import { parse } from './parser';

import type { AsyncAPIObject } from './spec-types';
import type { AsyncAPIDocumentFromFileSystem } from './util';

// remember the directory where execution of the program started
const originDir = String(process.cwd());
Expand Down Expand Up @@ -96,20 +97,28 @@ export default async function bundle(
process.chdir(path.resolve(originDir, String(options.baseDir[0]))); // guard against passing an array
}

const readFiles = files.map(file => readFileSync(file, 'utf-8')); // eslint-disable-line
const parsedJsons = files.map((file): AsyncAPIDocumentFromFileSystem => {
const readFile = readFileSync(file, 'utf-8'); // eslint-disable-line
return {asyncapi: toJS(readFile) as AsyncAPIObject, path: file};
});

const parsedJsons = readFiles.map(file => toJS(file)) as AsyncAPIObject[];

const majorVersion = versionCheck(parsedJsons);
const majorVersion = versionCheck(parsedJsons.map((parsedJson) => parsedJson.asyncapi));

let parsedBaseFile: AsyncAPIObject | undefined;
if (typeof options.base !== 'undefined') {
let baseFile = '';
let baseFilePath = '';

if (typeof options.base === 'string') {
options.base = readFileSync(options.base, 'utf-8'); // eslint-disable-line
baseFilePath = options.base;
baseFile = readFileSync(baseFilePath, 'utf-8'); // eslint-disable-line
} else if (Array.isArray(options.base)) {
options.base = readFileSync(String(options.base[0]), 'utf-8'); // eslint-disable-line
baseFilePath = String(options.base[0]);
baseFile = readFileSync(baseFilePath, 'utf-8'); // eslint-disable-line
}
options.base = toJS(options.base);
await parse(options.base, majorVersion, options);

parsedBaseFile = toJS(baseFile) as AsyncAPIObject;
await parse(parsedBaseFile, majorVersion, path.dirname(baseFilePath), options);
}

const resolvedJsons: AsyncAPIObject[] = await resolve(
Expand All @@ -123,7 +132,7 @@ export default async function bundle(
process.chdir(originDir);
}

return new Document(resolvedJsons, options.base);
return new Document(resolvedJsons, parsedBaseFile);
}

// 'module.exports' is added to maintain backward compatibility with Node.js
Expand Down
14 changes: 14 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Parser } from '@asyncapi/parser';
import type { ParserOptions as $RefParserOptions } from '@apidevtools/json-schema-ref-parser';
import type { AsyncAPIObject } from 'spec-types';

import path from 'path';

const parser = new Parser();

let RefParserOptions: $RefParserOptions;
Expand All @@ -12,12 +14,14 @@ let RefParserOptions: $RefParserOptions;
* Function fully dereferences the provided AsyncAPI Document.
* @param {Object[]} JSONSchema
* @param {number} specVersion
* @param {string} filePath
* @param {Object} options
* @private
*/
export async function parse(
JSONSchema: AsyncAPIObject,
specVersion: number,
filePath: string,
options: any = {}
) {
let validationResult: any[] = [];
Expand Down Expand Up @@ -74,6 +78,12 @@ export async function parse(
);
}

let previousDir: string | null = null;
if (!options.baseDir) {
previousDir = process.cwd();
process.chdir(path.dirname(filePath));
}

const dereferencedJSONSchema = await $RefParser.dereference(
JSONSchema,
RefParserOptions
Expand Down Expand Up @@ -104,5 +114,9 @@ export async function parse(
throw new Error();
}

if (previousDir) {
process.chdir(previousDir);
}

return dereferencedJSONSchema;
}
11 changes: 8 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { ParserError } from './errors';

import type { AsyncAPIObject } from './spec-types';

export interface AsyncAPIDocumentFromFileSystem {
asyncapi: AsyncAPIObject;
path: string;
}

/**
* @private
*/
Expand Down Expand Up @@ -48,15 +53,15 @@ export const toJS = (asyncapiYAMLorJSON: string | object) => {
* @private
*/
export const resolve = async (
asyncapiDocuments: AsyncAPIObject[],
asyncapiDocuments: AsyncAPIDocumentFromFileSystem[],
specVersion: number,
options: any
) => {
const docs = [];

for (const asyncapiDocument of asyncapiDocuments) {
await parse(asyncapiDocument, specVersion, options);
docs.push(asyncapiDocument);
await parse(asyncapiDocument.asyncapi, specVersion, asyncapiDocument.path, options);
docs.push(asyncapiDocument.asyncapi);
}

return docs;
Expand Down
2 changes: 1 addition & 1 deletion tests/base-option/camera.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ channels:
camera/click:
subcribe:
message:
$ref: './tests/base-option/messages.yaml#/messages/clickPhoto'
$ref: './messages.yaml#/messages/clickPhoto'
2 changes: 1 addition & 1 deletion tests/base-option/lights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ channels:
lights/On:
subcribe:
message:
$ref: './tests/base-option/messages.yaml#/messages/LightsOn'
$ref: './messages.yaml#/messages/LightsOn'
Loading