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

fix: informative Stubs when document is invalid #1068

Merged
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
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"dependencies": {
"@asyncapi/avro-schema-parser": "^3.0.24",
"@asyncapi/openapi-schema-parser": "^3.0.24",
"@asyncapi/parser": "^3.1.0",
"@asyncapi/parser": "^3.3.0",
"@asyncapi/protobuf-schema-parser": "^3.2.14",
"highlight.js": "^10.7.2",
"isomorphic-dompurify": "^2.14.0",
Expand Down
8 changes: 7 additions & 1 deletion library/src/containers/AsyncApi/Standalone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ class AsyncApiComponent extends Component<AsyncApiProps, AsyncAPIState> {
if (!error) {
return null;
}
return concatenatedConfig.show?.errors && <Error error={error} />;
return (
concatenatedConfig.show?.errors && (
<section className="aui-root">
<Error error={error} />
</section>
)
);
}

return (
Expand Down
6 changes: 3 additions & 3 deletions library/src/containers/Error/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const renderErrors = (errors: ValidationError[]): React.ReactNode => {

return errors
.map((singleError: ValidationError, index: number) => {
if (!singleError?.title || !singleError.location) {
if (!singleError?.title) {
return null;
}
return (
<div key={index} className="flex">
<span>{`${singleError.location.startLine}.`}</span>
<div key={index} className="flex gap-2">
<span>{`line ${singleError?.location?.startLine + singleError?.location?.startOffset}:`}</span>
<code className="whitespace-pre-wrap break-all ml-2">
{singleError.title}
</code>
Expand Down
43 changes: 40 additions & 3 deletions library/src/helpers/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser';
import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';

import { ErrorObject, ParserReturn, FetchingSchemaInterface } from '../types';
import {
ErrorObject,
ParserReturn,
FetchingSchemaInterface,
ValidationError,
} from '../types';

import { VALIDATION_ERRORS_TYPE } from '../constants';

Expand All @@ -22,8 +27,40 @@ export class Parser {
): Promise<ParserReturn> {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const { document } = await asyncapiParser.parse(content, parserOptions);
return { asyncapi: document };
const parseResult = await asyncapiParser.parse(content, parserOptions);

const error: {
title: string | undefined;
validationErrors: ValidationError[] | undefined;
} = {
title: 'There are errors in your Asyncapi document',
validationErrors: [],
};

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;
}

return { asyncapi: parseResult.document };
} catch (err) {
return this.handleError(err as ErrorObject);
}
Expand Down
40 changes: 23 additions & 17 deletions package-lock.json

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

Loading