@@ -38,24 +38,21 @@ const defaultOptions: ParseOptions = {
38
38
validateOptions : { } ,
39
39
__unstable : { } ,
40
40
} ;
41
- import yaml from 'js-yaml' ;
41
+
42
42
export async function parse ( parser : Parser , spectral : Spectral , asyncapi : Input , options : ParseOptions = { } ) : Promise < ParseOutput > {
43
43
let spectralDocument : Document | undefined ;
44
44
45
45
try {
46
46
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 } ) ;
59
56
if ( validated === undefined ) {
60
57
return {
61
58
document : undefined ,
@@ -72,7 +69,7 @@ export async function parse(parser: Parser, spectral: Spectral, asyncapi: Input,
72
69
73
70
// Apply unique ids which are used as part of iterating between channels <-> operations <-> messages
74
71
applyUniqueIds ( validatedDoc ) ;
75
- const detailed = createDetailedAsyncAPI ( validatedDoc , loadedObj as DetailedAsyncAPI [ 'input' ] , options . source ) ;
72
+ const detailed = createDetailedAsyncAPI ( validatedDoc , asyncapi as DetailedAsyncAPI [ 'input' ] , options . source ) ;
76
73
const document = createAsyncAPIDocument ( detailed ) ;
77
74
setExtension ( xParserSpecParsed , true , document ) ;
78
75
setExtension ( xParserApiVersion , ParserAPIVersion , document ) ;
0 commit comments