Skip to content

Commit b700a65

Browse files
aeworxetsmoya
andauthored
fix: remove forceful normalization of YAML to JSON (#1044)
Co-authored-by: Sergio Moya <[email protected]>
1 parent 6c215a8 commit b700a65

File tree

3 files changed

+1089
-91
lines changed

3 files changed

+1089
-91
lines changed

.changeset/smooth-pumas-deliver.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@asyncapi/parser": patch
3+
---
4+
5+
fix: remove forceful normalization of YAML to JSON

packages/parser/src/parse.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,21 @@ const defaultOptions: ParseOptions = {
3838
validateOptions: {},
3939
__unstable: {},
4040
};
41-
import yaml from 'js-yaml';
41+
4242
export async function parse(parser: Parser, spectral: Spectral, asyncapi: Input, options: ParseOptions = {}): Promise<ParseOutput> {
4343
let spectralDocument: Document | undefined;
4444

4545
try {
4646
options = mergePatch<ParseOptions>(defaultOptions, options);
47-
// Normalize input to always be JSON
48-
let loadedObj;
49-
if (typeof asyncapi === 'string') {
50-
try {
51-
loadedObj = yaml.load(asyncapi);
52-
} catch (e) {
53-
loadedObj = JSON.parse(asyncapi);
54-
}
55-
} else {
56-
loadedObj = asyncapi;
57-
}
58-
const { validated, diagnostics, extras } = await validate(parser, spectral, loadedObj, { ...options.validateOptions, source: options.source, __unstable: options.__unstable });
47+
48+
// `./src/validate.ts` enforces 'string' type on both YAML and JSON later in
49+
// code, and parses them both using the same `@stoplight/yaml`, so forceful
50+
// normalization of YAML to JSON here has no practical application. It only
51+
// causes `range` to be reported incorrectly in `diagnostics` by misleading
52+
// `Parser` into thinking it's dealing with JSON instead of YAML, creating
53+
// the bug described in https://github.com/asyncapi/parser-js/issues/936
54+
55+
const { validated, diagnostics, extras } = await validate(parser, spectral, asyncapi, { ...options.validateOptions, source: options.source, __unstable: options.__unstable });
5956
if (validated === undefined) {
6057
return {
6158
document: undefined,
@@ -72,7 +69,7 @@ export async function parse(parser: Parser, spectral: Spectral, asyncapi: Input,
7269

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

0 commit comments

Comments
 (0)