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: remove forceful normalization of YAML to JSON #1044

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
5 changes: 5 additions & 0 deletions .changeset/smooth-pumas-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/parser": patch
---

fix: remove forceful normalization of YAML to JSON
25 changes: 11 additions & 14 deletions packages/parser/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,21 @@ const defaultOptions: ParseOptions = {
validateOptions: {},
__unstable: {},
};
import yaml from 'js-yaml';

export async function parse(parser: Parser, spectral: Spectral, asyncapi: Input, options: ParseOptions = {}): Promise<ParseOutput> {
let spectralDocument: Document | undefined;

try {
options = mergePatch<ParseOptions>(defaultOptions, options);
// Normalize input to always be JSON
let loadedObj;
if (typeof asyncapi === 'string') {
try {
loadedObj = yaml.load(asyncapi);
} catch (e) {
loadedObj = JSON.parse(asyncapi);
}
} else {
loadedObj = asyncapi;
}
const { validated, diagnostics, extras } = await validate(parser, spectral, loadedObj, { ...options.validateOptions, source: options.source, __unstable: options.__unstable });

// `./src/validate.ts` enforces 'string' type on both YAML and JSON later in
// code, and parses them both using the same `@stoplight/yaml`, so forceful
// normalization of YAML to JSON here has no practical application. It only
// causes `range` to be reported incorrectly in `diagnostics` by misleading
// `Parser` into thinking it's dealing with JSON instead of YAML, creating
// the bug described in https://github.com/asyncapi/parser-js/issues/936

const { validated, diagnostics, extras } = await validate(parser, spectral, asyncapi, { ...options.validateOptions, source: options.source, __unstable: options.__unstable });
if (validated === undefined) {
return {
document: undefined,
Expand All @@ -72,7 +69,7 @@ export async function parse(parser: Parser, spectral: Spectral, asyncapi: Input,

// Apply unique ids which are used as part of iterating between channels <-> operations <-> messages
applyUniqueIds(validatedDoc);
const detailed = createDetailedAsyncAPI(validatedDoc, loadedObj as DetailedAsyncAPI['input'], options.source);
const detailed = createDetailedAsyncAPI(validatedDoc, asyncapi as DetailedAsyncAPI['input'], options.source);
const document = createAsyncAPIDocument(detailed);
setExtension(xParserSpecParsed, true, document);
setExtension(xParserApiVersion, ParserAPIVersion, document);
Expand Down
Loading
Loading