Skip to content

Commit

Permalink
Merge branch 'master' into feature/new_intent_api
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Mar 30, 2021
2 parents 4307b76 + ac8def6 commit 5ac333d
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 121 deletions.
29 changes: 29 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@
"contributions": [
"test"
]
},
{
"login": "KhudaDad414",
"name": "Khuda Dad Nomani",
"avatar_url": "https://avatars.githubusercontent.com/u/32505158?v=4",
"profile": "https://github.com/KhudaDad414",
"contributions": [
"code",
"bug"
]
},
{
"login": "aayushmau5",
"name": "Aayush Kumar Sahu",
"avatar_url": "https://avatars.githubusercontent.com/u/54525741?v=4",
"profile": "https://github.com/aayushmau5",
"contributions": [
"test"
]
},
{
"login": "JQrdan",
"name": "Jordan Tucker",
"avatar_url": "https://avatars.githubusercontent.com/u/25624685?v=4",
"profile": "https://github.com/JQrdan",
"contributions": [
"test",
"code"
]
}
],
"contributorsPerLine": 4,
Expand Down
58 changes: 58 additions & 0 deletions .github/workflows/if-go-pr-testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#This action is centrally managed in https://github.com/asyncapi/.github/
#Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo
#It does magic only if there is go.mod file in the root of the project
name: PR testing - if Go project

on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]

jobs:
lint:
if: github.event.pull_request.draft == false
name: lint
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Check if Go project and has go.mod
id: gomod
run: test -e ./go.mod && echo "::set-output name=exists::true" || echo "::set-output name=exists::false"
shell: bash
- if: steps.gomod.outputs.exists == 'true'
name: Setup Go
uses: actions/setup-go@v2.1.3
with:
go-version: 1.16
- if: steps.gomod.outputs.exists == 'true'
name: golangci-lint
uses: golangci/golangci-lint-action@v2 # golangci-lint version extracted from go.mod. `latest` if missing.
with:
skip-go-installation: true # we wanna control the version of Go in use

test:
if: github.event.pull_request.draft == false
name: ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Check if Go project and has go.mod
id: gomod
run: test -e ./go.mod && echo "::set-output name=exists::true" || echo "::set-output name=exists::false"
shell: bash
- if: steps.gomod.outputs.exists == 'true'
name: Setup Go
uses: actions/setup-go@v2.1.3
with:
go-version: 1.16
- if: steps.gomod.outputs.exists == 'true'
name: Build
run: go build -v ./...
- if: steps.gomod.outputs.exists == 'true'
name: Test
run: go test -v ./...

45 changes: 17 additions & 28 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
<dt><a href="#SchemaTypesToIterate">SchemaTypesToIterate</a></dt>
<dd><p>The different types of schemas you can iterate</p>
</dd>
<dt><a href="#TraverseSchemas">TraverseSchemas</a> ⇒ <code>boolean</code></dt>
<dd><p>Callback used when crawling a schema.</p>
</dd>
</dl>

<a name="module_@asyncapi/parser"></a>
Expand Down Expand Up @@ -398,6 +395,8 @@
* [.extension(key)](#module_@asyncapi/parser+Tag+extension) ⇒ <code>any</code>
* [.hasExt(key)](#module_@asyncapi/parser+Tag+hasExt) ⇒ <code>boolean</code>
* [.ext(key)](#module_@asyncapi/parser+Tag+ext) ⇒ <code>any</code>
* _static_
* [.TraverseSchemas](#module_@asyncapi/parser.TraverseSchemas) ⇒ <code>boolean</code>
* _inner_
* [~ParserError](#module_@asyncapi/parser+ParserError) ⇐ <code>Error</code>
* [new ParserError(definition)](#new_module_@asyncapi/parser+ParserError_new)
Expand Down Expand Up @@ -2976,6 +2975,20 @@ Implements functions to deal with a Tag object.
| --- | --- | --- |
| key | <code>string</code> | Extension key. |

<a name="module_@asyncapi/parser.TraverseSchemas"></a>

### @asyncapi/parser.TraverseSchemas ⇒ <code>boolean</code>
Callback used when crawling a schema.

**Kind**: static typedef of [<code>@asyncapi/parser</code>](#module_@asyncapi/parser)
**Returns**: <code>boolean</code> - should the crawler continue crawling the schema?

| Param | Type | Description |
| --- | --- | --- |
| schema | <code>Schema</code> | which is being crawled |
| propName | <code>String</code> | if the schema is from a property get the name of such |
| callbackType | [<code>SchemaIteratorCallbackType</code>](#SchemaIteratorCallbackType) | is the schema a new one or is the crawler finishing one. |

<a name="module_@asyncapi/parser+ParserError"></a>

### @asyncapi/parser~ParserError ⇐ <code>Error</code>
Expand Down Expand Up @@ -3187,7 +3200,7 @@ By default all schemas are iterated

| Param | Type |
| --- | --- |
| callback | [<code>TraverseSchemas</code>](#TraverseSchemas) |
| callback | <code>TraverseSchemas</code> |
| schemaTypesToIterate | [<code>Array.&lt;SchemaTypesToIterate&gt;</code>](#SchemaTypesToIterate) |

<a name="module_@asyncapi/parser+AsyncAPIDocument+hasTags"></a>
Expand Down Expand Up @@ -3649,16 +3662,6 @@ Implements functions to deal with the Tags object.

<a name="SchemaIteratorCallbackType"></a>

## SchemaIteratorCallbackType : <code>enum</code>
**Kind**: global enum
**Read only**: true
<a name="SchemaTypesToIterate"></a>

## SchemaTypesToIterate : <code>enum</code>
**Kind**: global enum
**Read only**: true
<a name="SchemaIteratorCallbackType"></a>

## SchemaIteratorCallbackType
The different kind of stages when crawling a schema.

Expand Down Expand Up @@ -3690,17 +3693,3 @@ The different types of schemas you can iterate
| allOfs | <code>string</code> | Crawl all schemas in allOf's |
| anyOfs | <code>string</code> | Crawl all schemas in anyOf's |

<a name="TraverseSchemas"></a>

## TraverseSchemas ⇒ <code>boolean</code>
Callback used when crawling a schema.

**Kind**: global typedef
**Returns**: <code>boolean</code> - should the crawler continue crawling the schema?

| Param | Type | Description |
| --- | --- | --- |
| schema | <code>Schema</code> | which is being crawled |
| propName | <code>String</code> | if the schema is from a property get the name of such |
| callbackType | [<code>SchemaIteratorCallbackType</code>](#SchemaIteratorCallbackType) | is the schema a new one or is the crawler finishing one. |

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ This package throws a bunch of different error types. All errors contain a `type
|`validation-errors`|`parsedJSON`, `validationErrors`|The AsyncAPI document contains errors. See `validationErrors` for more information.
|`impossible-to-register-parser`| None | Registration of custom message parser failed.
|`schema-validation-errors`| `parsedJSON`, `validationErrors` | Schema of the payload provided in the AsyncAPI document is not valid with AsyncAPI schema format.
|`fetch-url-error`| None | The URL provided for fetching AsynAPI document is invalid.
For more information about the `ParserError` class, [check out the documentation](./API.md#new_ParserError_new).
Expand Down Expand Up @@ -192,6 +193,11 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center"><a href="https://www.linkedin.com/in/jbreitenbaumer/"><img src="https://avatars3.githubusercontent.com/u/683438?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jürgen B.</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=juergenbr" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/aeworxet"><img src="https://avatars.githubusercontent.com/u/16149591?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Viacheslav Turovskyi</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=aeworxet" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/KhudaDad414"><img src="https://avatars.githubusercontent.com/u/32505158?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Khuda Dad Nomani</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=KhudaDad414" title="Code">💻</a> <a href="https://github.com/asyncapi/parser-js/issues?q=author%3AKhudaDad414" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://github.com/aayushmau5"><img src="https://avatars.githubusercontent.com/u/54525741?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aayush Kumar Sahu</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=aayushmau5" title="Tests">⚠️</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/JQrdan"><img src="https://avatars.githubusercontent.com/u/25624685?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jordan Tucker</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=JQrdan" title="Tests">⚠️</a> <a href="https://github.com/asyncapi/parser-js/commits?author=JQrdan" title="Code">💻</a></td>
</tr>
</table>

Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion lib/asyncapiSchemaFormatParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Ajv = require('ajv');
const ParserError = require('./errors/parser-error');
const asyncapi = require('@asyncapi/specs');
const { improveAjvErrors } = require('./utils');
const cloneDeep = require('lodash.clonedeep');

module.exports = {
parse,
Expand All @@ -11,10 +12,13 @@ module.exports = {
/**
* @private
*/
async function parse({ message, originalAsyncAPIDocument, fileFormat, parsedAsyncAPIDocument, pathToPayload }) {
async function parse({ message, originalAsyncAPIDocument, fileFormat, parsedAsyncAPIDocument, pathToPayload, defaultSchemaFormat }) {
const payload = message.payload;
if (!payload) return;

message['x-parser-original-schema-format'] = message.schemaFormat || defaultSchemaFormat;
message['x-parser-original-payload'] = cloneDeep(message.payload);

const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
Expand Down
24 changes: 12 additions & 12 deletions lib/iterators.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
/**
* @readonly
* @enum {SchemaIteratorCallbackType}
*/

/**
* The different kind of stages when crawling a schema.
*
* @typedef SchemaIteratorCallbackType
* @property {string} NEW_SCHEMA The crawler just started crawling a schema.
* @property {string} END_SCHEMA The crawler just finished crawling a schema.
*/

/**
* @readonly
* @enum {SchemaIteratorCallbackType}
*/
const SchemaIteratorCallbackType = Object.freeze({
NEW_SCHEMA: 'NEW_SCHEMA',
END_SCHEMA: 'END_SCHEMA'
});

/**
*
* @readonly
* @enum {SchemaTypesToIterate}
*/

/**
* The different types of schemas you can iterate
*
Expand All @@ -29,12 +35,6 @@ const SchemaIteratorCallbackType = Object.freeze({
* @property {string} allOfs Crawl all schemas in allOf's
* @property {string} anyOfs Crawl all schemas in anyOf's
*/

/**
*
* @readonly
* @enum {SchemaTypesToIterate}
*/
const SchemaTypesToIterate = Object.freeze({
parameters: 'parameters',
payloads: 'payloads',
Expand Down Expand Up @@ -231,4 +231,4 @@ module.exports = {
traverseMessage,
recursiveSchemaObject,
recursiveSchemaArray
};
};
9 changes: 3 additions & 6 deletions lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const Info = require('./info');
const Server = require('./server');
const Channel = require('./channel');
const Components = require('./components');

const MixinExternalDocs = require('../mixins/external-docs');
const MixinTags = require('../mixins/tags');
const MixinSpecificationExtensions = require('../mixins/specification-extensions');
Expand Down Expand Up @@ -207,19 +206,17 @@ class AsyncAPIDocument extends Base {

/**
* Callback used when crawling a schema.
* @callback TraverseSchemas
* @param {Schema} schema which is being crawled
* @callback module:@asyncapi/parser.TraverseSchemas
* @param {Schema} schema which is being crawled
* @param {String} propName if the schema is from a property get the name of such
* @param {SchemaIteratorCallbackType} callbackType is the schema a new one or is the crawler finishing one.
* @returns {boolean} should the crawler continue crawling the schema?
*
*/

/**
* Traverse schemas in the document and select which types of schemas to include.
* By default all schemas are iterated
*
* @param {TraverseSchemas} callback
* @param {TraverseSchemas} callback
* @param {SchemaTypesToIterate[]} schemaTypesToIterate
*/
traverseSchemas(callback, schemaTypesToIterate) {
Expand Down
8 changes: 7 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ function parseFromUrl(url, fetchOptions, options) {
.then(res => res.text())
.then(doc => parse(doc, options))
.then(result => resolve(result))
.catch(reject);
.catch(e => {
if (e instanceof ParserError) return reject(e);
return reject(new ParserError({
type: 'fetch-url-error',
title: e.message,
}));
});
});
}

Expand Down
Loading

0 comments on commit 5ac333d

Please sign in to comment.