Skip to content

Commit

Permalink
Merge branch 'master' into node_fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil authored Aug 15, 2024
2 parents 67f6e2a + 6794729 commit 78a1625
Show file tree
Hide file tree
Showing 5 changed files with 1,092 additions and 92 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This is a Monorepo managed using [Turborepo](https://turbo.build/) and contains the following package:

1. [Parser-js](packages/parser): Use this package to validate and parse AsyncAPI documents —either YAML or JSON— in your Node.js or browser application.
2. [Multi-Parser](packages/multi-parser): This tool allows the parsing of AsyncAPI documents producing a desired interface based on a given Parser-API version. Useful if your tool needs to support several AsyncAPI Spec versions.

Validation is powered by [Spectral](https:/stoplightio/spectral).
Updated bundle for the browser is always attached to the GitHub Release.
Expand Down
6 changes: 6 additions & 0 deletions packages/parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @asyncapi/parser

## 3.2.2

### Patch Changes

- b700a65: fix: remove forceful normalization of YAML to JSON

## 3.2.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/parser",
"version": "3.2.1",
"version": "3.2.2",
"description": "JavaScript AsyncAPI parser.",
"private": false,
"bugs": {
Expand Down
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:/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

0 comments on commit 78a1625

Please sign in to comment.