Skip to content

Commit

Permalink
Merge branch 'master' into issue-907-sidebar-broken
Browse files Browse the repository at this point in the history
  • Loading branch information
AceTheCreator authored Oct 14, 2024
2 parents 2a22197 + 2c031c9 commit 1538237
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/react-component",
"version": "2.2.4",
"version": "2.3.0",
"private": false,
"description": "A React component for AsyncAPI specification.",
"repository": {
Expand Down
90 changes: 55 additions & 35 deletions library/src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -26,41 +31,18 @@ export class Parser {
parserOptions?: any,
): Promise<ParserReturn> {
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);
}
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion playground/components/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Tab extends Component<TabProps> {
<TabLink
onClick={(event) => {
event.preventDefault();
if (parentCallback && tabIndex) {
if (parentCallback && tabIndex != undefined) {
parentCallback(tabIndex);
}
}}
Expand Down
4 changes: 2 additions & 2 deletions web-component/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/web-component",
"version": "2.2.4",
"version": "2.3.0",
"private": false,
"description": "A web component for AsyncAPI specification. Based on @asyncapi/react-component.",
"repository": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"install:reactcomp": "chmod +x ./bump-react-comp.sh && ./bump-react-comp.sh"
},
"dependencies": {
"@asyncapi/react-component": "^2.2.4",
"@asyncapi/react-component": "^2.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"web-react-components": "^1.4.2"
Expand Down

0 comments on commit 1538237

Please sign in to comment.