From 7db197c7c2f36bf67f7b2210059ef1bc1d95fc38 Mon Sep 17 00:00:00 2001 From: Mohammed Mehdi <96487647+catosaurusrex2003@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:12:14 +0530 Subject: [PATCH] feat: fix of make @asyncapi/react component with vs asyncapi preview (#1082) --- library/src/helpers/parser.ts | 90 +++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/library/src/helpers/parser.ts b/library/src/helpers/parser.ts index 0ecb3cc28..83da10b6b 100644 --- a/library/src/helpers/parser.ts +++ b/library/src/helpers/parser.ts @@ -1,4 +1,9 @@ -import { Parser as AsyncapiParser, fromURL } from '@asyncapi/parser'; +import { + Parser as AsyncapiParser, + Diagnostic, + DiagnosticSeverity, + fromURL, +} from '@asyncapi/parser'; import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser'; import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser'; import { AvroSchemaParser } from '@asyncapi/avro-schema-parser'; @@ -26,41 +31,18 @@ export class Parser { parserOptions?: any, ): Promise { try { - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - const parseResult = await asyncapiParser.parse(content, parserOptions); - - const error: { - title: string | undefined; - validationErrors: ValidationError[] | undefined; - } = { - title: 'There are errors in your Asyncapi document', - validationErrors: [], - }; + const { document, diagnostics } = await asyncapiParser.parse( + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + content, + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + parserOptions, + ); - if (parseResult.document === undefined) { - parseResult.diagnostics.forEach((diagnostic) => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison - if (diagnostic.severity == 0) { - const tempObj: ValidationError = { - title: diagnostic.message, - location: { - jsonPointer: '/' + diagnostic.path.join('/'), - startLine: diagnostic.range.start.line, - startColumn: diagnostic.range.start.character, - // as of @asyncapi/parser 3.3.0 offset of 1 correctly shows the error line - startOffset: 1, - endLine: diagnostic.range.end.line, - endColumn: diagnostic.range.end.character, - endOffset: 0, - }, - }; - error.validationErrors?.push(tempObj); - } - }); - throw error; + if (document === undefined) { + throw this.convertDiagnosticToErrorObject(diagnostics, [0]); } - return { asyncapi: parseResult.document }; + return { asyncapi: document }; } catch (err) { return this.handleError(err as ErrorObject); } @@ -79,13 +61,51 @@ export class Parser { arg.requestOptions as any, ); // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - const { document } = await fromResult.parse(parserOptions); - return { asyncapi: document }; + const { document, diagnostics } = await fromResult.parse(parserOptions); + + if (document == undefined) { + // this means there are errors in the document. + // so we gather all the severity 0 diagnostics and throw them as errors + throw this.convertDiagnosticToErrorObject(diagnostics, [0]); + } + + return { asyncapi: document, error: undefined }; } catch (err) { return this.handleError(err as ErrorObject); } } + static readonly convertDiagnosticToErrorObject = ( + diagnostics: Diagnostic[], + severities: DiagnosticSeverity[], + ): ErrorObject => { + const error: ErrorObject = { + title: 'There are errors in your Asyncapi document', + type: 'VALIDATION_ERRORS_TYPE', + validationErrors: [], + }; + diagnostics.forEach((diagnostic) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison + if (severities.includes(diagnostic.severity)) { + const tempObj: ValidationError = { + title: diagnostic.message, + location: { + jsonPointer: '/' + diagnostic.path.join('/'), + startLine: diagnostic.range.start.line, + startColumn: diagnostic.range.start.character, + // as of @asyncapi/parser 3.3.0 offset of 1 correctly shows the error line + startOffset: 1, + endLine: diagnostic.range.end.line, + endColumn: diagnostic.range.end.character, + endOffset: 0, + }, + }; + error.validationErrors?.push(tempObj); + } + }); + return error; + }; + private static handleError = (err: ErrorObject): ParserReturn => { if (err.type === VALIDATION_ERRORS_TYPE) { return {