Skip to content

Commit 1ba6aad

Browse files
authored
feat: parser adapter for json in asyncapi3 (#5048)
1 parent 6fd34b6 commit 1ba6aad

File tree

33 files changed

+2711
-5
lines changed

33 files changed

+2711
-5
lines changed

package-lock.json

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/apidom-ls/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"@swagger-api/apidom-parser-adapter-api-design-systems-yaml": "^1.0.0-rc.3",
107107
"@swagger-api/apidom-parser-adapter-asyncapi-json-2": "^1.0.0-rc.3",
108108
"@swagger-api/apidom-parser-adapter-asyncapi-yaml-2": "^1.0.0-rc.3",
109+
"@swagger-api/apidom-parser-adapter-asyncapi-json-3": "^1.0.0-rc.3",
109110
"@swagger-api/apidom-parser-adapter-asyncapi-yaml-3": "^1.0.0-rc.3",
110111
"@swagger-api/apidom-parser-adapter-json": "^1.0.0-rc.3",
111112
"@swagger-api/apidom-parser-adapter-openapi-json-2": "^1.0.0-rc.3",

packages/apidom-ls/src/utils/utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as openapi31xAdapterJson from '@swagger-api/apidom-parser-adapter-opena
66
import * as openapi31xAdapterYaml from '@swagger-api/apidom-parser-adapter-openapi-yaml-3-1';
77
import * as asyncapi2AdapterJson from '@swagger-api/apidom-parser-adapter-asyncapi-json-2';
88
import * as asyncapi2AdapterYaml from '@swagger-api/apidom-parser-adapter-asyncapi-yaml-2';
9+
import * as asyncapi3AdapterJson from '@swagger-api/apidom-parser-adapter-asyncapi-json-3';
910
import * as asyncapi3AdapterYaml from '@swagger-api/apidom-parser-adapter-asyncapi-yaml-3';
1011
import * as adsAdapterJson from '@swagger-api/apidom-parser-adapter-api-design-systems-json';
1112
import * as adsAdapterYaml from '@swagger-api/apidom-parser-adapter-api-design-systems-yaml';
@@ -836,7 +837,6 @@ export async function findNamespace(
836837
mediaType: asyncapi2AdapterYaml.mediaTypes.findBy(version, 'yaml'),
837838
};
838839
}
839-
840840
if (await asyncapi3AdapterYaml.detect(text)) {
841841
const asyncapi3YamlMatch = text.match(asyncapi3AdapterYaml.detectionRegExp)!;
842842
const groups = asyncapi3YamlMatch.groups!;
@@ -850,6 +850,19 @@ export async function findNamespace(
850850
};
851851
}
852852

853+
if (await asyncapi3AdapterJson.detect(text)) {
854+
const asyncapi3JsonMatch = text.match(asyncapi3AdapterJson.detectionRegExp)!;
855+
const groups = asyncapi3JsonMatch.groups!;
856+
const version = groups.version_json;
857+
858+
return {
859+
namespace: 'asyncapi',
860+
version,
861+
format: 'JSON',
862+
mediaType: asyncapi3AdapterJson.mediaTypes.findBy(version, 'json'),
863+
};
864+
}
865+
853866
if (await openapi2AdapterJson.detect(text)) {
854867
const openapi2JsonMatch = text.match(openapi2AdapterJson.detectionRegExp)!;
855868
const groups = openapi2JsonMatch.groups!;

packages/apidom-ns-asyncapi-3/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swagger-api/apidom-ns-asyncapi-3",
3-
"version": "1.0.0-beta.0",
3+
"version": "1.0.0-rc.3",
44
"description": "AsyncAPI 3.x.y namespace for ApiDOM.",
55
"publishConfig": {
66
"access": "public",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**/*.js
2+
/**/*.mjs
3+
/**/*.cjs
4+
/dist
5+
/types
6+
/config
7+
/.nyc_output
8+
/node_modules
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/src/**/*.mjs
2+
/src/**/*.cjs
3+
/test/**/*.mjs
4+
/dist
5+
/types
6+
/NOTICE
7+
/swagger-api-apidom-parser-adapter-asyncapi-json-3-*.tgz
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extensions": ["ts"],
3+
"loader": "ts-node/esm",
4+
"recursive": true,
5+
"spec": "test/**/*.mjs",
6+
"file": ["test/mocha-bootstrap.ts"],
7+
"ignore": ["test/perf/**/*.ts"]
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
save-prefix="="
2+
save=false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# @swagger-api/apidom-parser-adapter-asyncapi-json-3
2+
3+
`@swagger-api/apidom-parser-adapter-asyncapi-json-3` is a parser adapter for following AsyncAPI specification versions defined in [JSON format](https://www.json.org/json-en.html):
4+
5+
- [AsyncAPI 3.0.0 specification](https://github.com/asyncapi/spec/blob/v3.0.0/spec/asyncapi.md)
6+
- [AsyncAPI 3.0.1 specification](https://github.com/asyncapi/spec/blob/v3.0.1/spec/asyncapi.md)
7+
8+
Under the hood this adapter uses [@swagger-api/apidom-parser-adapter-json](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-json)
9+
to parse a source string into generic ApiDOM in [base ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom-core#base-namespace)
10+
which is then refracted with [AsyncApi 3.x.y Refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-3#refractors).
11+
12+
## Installation
13+
14+
After [prerequisites](https://github.com/swagger-api/apidom/blob/main/README.md#prerequisites) for installing this package are satisfied, you can install it
15+
via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
16+
17+
```sh
18+
$ npm install @swagger-api/apidom-parser-adapter-asyncapi-json-3
19+
```
20+
21+
## Parser adapter API
22+
23+
This parser adapter is fully compatible with parser adapter interface required by [@swagger-api/apidom-parser](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser#mounting-parser-adapters)
24+
and implements all required properties.
25+
26+
### mediaTypes
27+
28+
Defines list of media types that this parser adapter recognizes.
29+
30+
```js
31+
[
32+
'application/vnd.aai.asyncapi;version=3.0.0',
33+
'application/vnd.aai.asyncapi+json;version=3.0.0',
34+
'application/vnd.aai.asyncapi;version=3.0.1',
35+
'application/vnd.aai.asyncapi+json;version=3.0.1',
36+
]
37+
```
38+
39+
### detect
40+
41+
[Detection](https://github.com/swagger-api/apidom/blob/main/packages/apidom-parser-adapter-asyncapi-json-3/src/adapter.ts#L18) is based on a regular expression matching required AsyncApi 3.0.0 specification symbols in JSON format.
42+
43+
### namespace
44+
45+
This adapter exposes an instance of [AsyncApi 3.x.y ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-3#asyncapi-3xy-namespace).
46+
47+
### parse
48+
49+
`parse` function consumes various options as a second argument. Here is a list of these options:
50+
51+
Option | Type | Default | Description
52+
--- | --- | --- | ---
53+
<a name="specObj"></a>`specObj` | `Object` | [Specification Object](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-asyncapi-3/src/refractor/specification.ts) | This specification object drives the JSON AST transformation to AsyncAPI 3.x ApiDOM namespace.
54+
<a name="sourceMap"></a>`sourceMap` | `Boolean` | `false` | Indicate whether to generate source maps.
55+
<a name="refractorOpts"></a>`refractorOpts` | `Object` | `{}` | Refractor options are [passed to refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-3#refractor-plugins) during refracting phase.
56+
57+
All unrecognized arbitrary options will be ignored.
58+
59+
## Usage
60+
61+
This parser adapter can be used directly or indirectly via [@swagger-api/apidom-parser](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser).
62+
63+
### Direct usage
64+
65+
During direct usage you don't need to provide `mediaType` as the `parse` function is already pre-bound
66+
with [supported media types](#mediatypes).
67+
68+
```js
69+
import { parse, detect } from '@swagger-api/apidom-parser-adapter-asyncapi-json-3';
70+
71+
// detecting
72+
await detect('{"asyncapi": "3.0.0"}'); // => true
73+
await detect('{"asyncapi": "3.0.1"}'); // => true
74+
await detect('test'); // => false
75+
76+
// parsing
77+
const parseResult = await parse('{"asyncapi": "3.0.0"}', { sourceMap: true });
78+
```
79+
80+
### Indirect usage
81+
82+
You can omit the `mediaType` option here, but please read [Word on detect vs mediaTypes](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser#word-on-detect-vs-mediatypes) before you do so.
83+
84+
```js
85+
import ApiDOMParser from '@swagger-api/apidom-parser';
86+
import * as asyncApiJsonAdapter from '@swagger-api/apidom-parser-adapter-asyncapi-json-3';
87+
88+
const parser = new ApiDOMParser();
89+
90+
parser.use(asyncApiJsonAdapter);
91+
92+
const parseResult = await parser.parse('{"asyncapi": "3.0.1"}', { mediaType: asyncApiJsonAdapter.mediaTypes.latest('json') });
93+
```

0 commit comments

Comments
 (0)