Skip to content

Commit

Permalink
fix: file path input for AsyncAPI (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Feb 12, 2024
1 parent 293d4f9 commit 9b218af
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 284 deletions.
148 changes: 113 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^11.1.0",
"@apidevtools/swagger-parser": "^10.0.3",
"@asyncapi/parser": "3.0.0-next-major-spec.8",
"@smoya/multi-parser": "^4.0.0",
"@asyncapi/parser": "^3.0.5",
"@smoya/multi-parser": "^5.0.1",
"@swc/core": "^1.3.5",
"@swc/jest": "^0.2.23",
"@types/node": "^20.3.3",
Expand Down
51 changes: 22 additions & 29 deletions src/processors/AsyncAPIInputProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
import Parser, {
import {
isAsyncAPIDocument,
isOldAsyncAPIDocument,
AsyncAPIDocumentInterface,
Expand All @@ -18,7 +18,10 @@ import { AsyncapiV2Schema } from '../models/AsyncapiV2Schema';
import { convertToMetaModel } from '../helpers';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { NewParser } from '@smoya/multi-parser';
import {
ConvertDocumentParserAPIVersion,
NewParser
} from '@smoya/multi-parser';
import { createDetailedAsyncAPI } from '@asyncapi/parser/cjs/utils';

/**
Expand Down Expand Up @@ -46,10 +49,8 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
input?: any,
options?: ProcessorOptions
): Promise<InputMetaModel> {
let rawInput = input;
if (this.isFileInput(input)) {
rawInput = await this.getParsedFileInput(input);
}
const rawInput = input;
let doc: AsyncAPIDocumentInterface | undefined;

if (!this.shouldProcess(rawInput)) {
throw new Error(
Expand All @@ -58,23 +59,33 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
}

Logger.debug('Processing input as an AsyncAPI document');
let doc: AsyncAPIDocumentInterface | undefined;
const inputModel = new InputMetaModel();
if (isOldAsyncAPIDocument(rawInput)) {
// Is from old parser
const parsedJSON = rawInput.json();
const detailed = createDetailedAsyncAPI(parsedJSON, parsedJSON);
doc = createAsyncAPIDocument(detailed);
} else if (AsyncAPIInputProcessor.isFromNewParser(rawInput)) {
doc = ConvertDocumentParserAPIVersion(rawInput, 2) as any;
} else {
const parserOptions = options?.asyncapi || {};
const parser = NewParser(2, {
parserOptions,
includeSchemaParsers: true
});
const { document, diagnostics } = await parser.parse(
rawInput,
parserOptions
);

let parserResult;
if (this.isFileInput(input)) {
const filePath = fileURLToPath(input);
/* eslint-disable-next-line security/detect-non-literal-fs-filename -- Safe as it just checks file existance */
if (!fs.existsSync(filePath)) {
throw new Error('File does not exists.');
}
parserResult = await fromFile(parser as any, filePath).parse();
} else {
parserResult = await parser.parse(rawInput, parserOptions);
}
const { document, diagnostics } = parserResult;
if (document) {
doc = document as unknown as AsyncAPIDocumentInterface;
} else {
Expand Down Expand Up @@ -419,22 +430,4 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
// prettier-ignore
return typeof input === 'string' && (/^file:\/\//g).test(input);
}

async getParsedFileInput(input: string): Promise<AsyncAPIDocumentInterface> {
const filePath = fileURLToPath(input);
/* eslint-disable-next-line security/detect-non-literal-fs-filename -- Safe as it just checks file existance */
if (!fs.existsSync(filePath)) {
throw new Error('File does not exists.');
}
const parser = new Parser();
const { document, diagnostics } = await fromFile(parser, filePath).parse();
if (!document) {
const err = new Error(
'Input is not an correct AsyncAPI document so it cannot be processed.'
);
(err as any).diagnostics = diagnostics;
throw err;
}
return document;
}
}
Loading

0 comments on commit 9b218af

Please sign in to comment.