Skip to content

Commit

Permalink
chore: update next-spec branch with master changes (asyncapi#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Sep 15, 2022
1 parent d1353bb commit a7a5423
Show file tree
Hide file tree
Showing 19 changed files with 358 additions and 141 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ env:
node: true
es6: true
mocha: true
browser: true

plugins:
- sonarjs
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/if-nodejs-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ jobs:
- if: steps.packagejson.outputs.exists == 'true'
name: Run test
run: npm test
- if: failure() # Only, on failure, send a message on the 94_bot-failing-ci slack channel
name: Report workflow run status to Slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,action,workflow
text: 'Release workflow failed in testing job'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_CI_FAIL_NOTIFY }}

release:
needs: [test-nodejs]
Expand Down Expand Up @@ -84,3 +93,12 @@ jobs:
GIT_COMMITTER_NAME: asyncapi-bot
GIT_COMMITTER_EMAIL: [email protected]
run: npm run release
- if: failure() # Only, on failure, send a message on the 94_bot-failing-ci slack channel
name: Report workflow run status to Slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,action,workflow
text: 'Release workflow failed in release job'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_CI_FAIL_NOTIFY }}
18 changes: 17 additions & 1 deletion .github/workflows/if-nodejs-version-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ jobs:
- name: Check if Node.js project and has package.json
id: packagejson
run: test -e ./package.json && echo "::set-output name=exists::true" || echo "::set-output name=exists::false"
- if: steps.packagejson.outputs.exists == 'true'
name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
- if: steps.packagejson.outputs.exists == 'true'
name: Install dependencies
run: npm install
Expand All @@ -46,4 +53,13 @@ jobs:
author: asyncapi-bot <[email protected]>
title: 'chore(release): ${{github.event.release.tag_name}}'
body: 'Version bump in package.json for release [${{github.event.release.tag_name}}](${{github.event.release.html_url}})'
branch: version-bump/${{github.event.release.tag_name}}
branch: version-bump/${{github.event.release.tag_name}}
- if: failure() # Only, on failure, send a message on the 94_bot-failing-ci slack channel
name: Report workflow run status to Slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,action,workflow
text: 'Unable to bump the version in package.json after the release'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_CI_FAIL_NOTIFY }}
2 changes: 1 addition & 1 deletion .github/workflows/link-check-cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
fields: repo,action,workflow
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_CI_FAIL_NOTIFY }}
if: failure() # Only, on failure, send a message on the Slack Docs Channel (if there are broken links)
if: failure() # Only, on failure, send a message on the 94_bot-failing-ci slack channel
5 changes: 3 additions & 2 deletions .github/workflows/stale-issues-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Mark issue or PR as stale
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v4.0.0
- uses: actions/stale@v5.1.0
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: |
Expand Down Expand Up @@ -41,4 +41,5 @@ jobs:
stale-issue-label: stale
stale-pr-label: stale
exempt-issue-labels: keep-open
exempt-pr-labels: keep-open
exempt-pr-labels: keep-open
close-issue-reason: not_planned
17 changes: 12 additions & 5 deletions lib/asyncapiSchemaFormatParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const cloneDeep = require('lodash.clonedeep');
const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: 'id',
schemaId: 'auto',
logger: false,
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
Expand Down Expand Up @@ -68,7 +68,8 @@ function getMimeTypes() {
function getValidator(version) {
let validate = ajv.getSchema(version);
if (!validate) {
ajv.addSchema(preparePayloadSchema(asyncapi[String(version)]), version);
const payloadSchema = preparePayloadSchema(asyncapi[String(version)], version);
ajv.addSchema(payloadSchema, version);
validate = ajv.getSchema(version);
}
return validate;
Expand All @@ -80,12 +81,18 @@ function getValidator(version) {
*
* @private
* @param {Object} asyncapiSchema AsyncAPI specification JSON Schema
* @param {Object} version AsyncAPI version.
* @returns {Object} valid JSON Schema document describing format of AsyncAPI-valid schema for message payload
*/
function preparePayloadSchema(asyncapiSchema) {
function preparePayloadSchema(asyncapiSchema, version) {
const payloadSchema = `http://asyncapi.com/definitions/${version}/schema.json`;
const definitions = asyncapiSchema.definitions;
// Remove the meta schemas because it is already present within Ajv, and it's not possible to add duplicate schemas.
delete definitions['http://json-schema.org/draft-07/schema'];
delete definitions['http://json-schema.org/draft-04/schema'];
return {
$ref: '#/definitions/schema',
definitions: asyncapiSchema.definitions
$ref: payloadSchema,
definitions
};
}

Expand Down
30 changes: 25 additions & 5 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ const $RefParser = require('@apidevtools/json-schema-ref-parser');
const mergePatch = require('tiny-merge-patch').apply;
const ParserError = require('./errors/parser-error');
const { validateChannels, validateTags, validateServerVariables, validateOperationId, validateServerSecurity, validateMessageId } = require('./customValidators.js');
const { toJS, findRefs, getLocationOf, improveAjvErrors, getDefaultSchemaFormat } = require('./utils');
const {
toJS,
findRefs,
getLocationOf,
improveAjvErrors,
getDefaultSchemaFormat,
getBaseUrl,
} = require('./utils');
const AsyncAPIDocument = require('./models/asyncapi');

const OPERATIONS = ['publish', 'subscribe'];
Expand All @@ -19,8 +26,9 @@ const xParserMessageParsed = 'x-parser-message-parsed';
const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: 'id',
schemaId: 'auto',
logger: false,
validateSchema: true,
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

Expand Down Expand Up @@ -55,7 +63,11 @@ async function parse(asyncapiYAMLorJSON, options = {}) {
let parsedJSON;
let initialFormat;

options.path = options.path || `${process.cwd()}${path.sep}`;
if (typeof window !== 'undefined' && !options.hasOwnProperty('path')) {
options.path = getBaseUrl(window.location.href);
} else {
options.path = options.path || `${process.cwd()}${path.sep}`;
}

try {
({ initialFormat, parsedJSON } = toJS(asyncapiYAMLorJSON));
Expand Down Expand Up @@ -127,12 +139,16 @@ async function parse(asyncapiYAMLorJSON, options = {}) {
* @param {ParserOptions=} [options] Configuration to pass to the {@link #asyncapiparserparseroptions--object|ParserOptions} method.
* @returns {Promise<AsyncAPIDocument>} The parsed AsyncAPI document.
*/
function parseFromUrl(url, fetchOptions, options) {
function parseFromUrl(url, fetchOptions, options = {}) {
//Why not just addinga default to the arguments list?
//All function parameters with default values should be declared after the function parameters without default values. Otherwise, it makes it impossible for callers to take advantage of defaults; they must re-specify the defaulted values or pass undefined in order to "get to" the non-default parameters.
//To not break the API by changing argument position and to silet the linter it is just better to move adding
if (!fetchOptions) fetchOptions = {};

if (!options.hasOwnProperty('path')) {
options = { ...options, path: getBaseUrl(url) };
}

return new Promise((resolve, reject) => {
fetch(url, fetchOptions)
.then(res => res.text())
Expand Down Expand Up @@ -186,7 +202,11 @@ async function handleCircularRefs(refParser, parsedJSON, initialFormat, asyncapi
function getValidator(version) {
let validate = ajv.getSchema(version);
if (!validate) {
ajv.addSchema(asyncapi[String(version)], version);
const asyncapiSchema = asyncapi[String(version)];
// Remove the meta schemas because it is already present within Ajv, and it's not possible to add duplicate schemas.
delete asyncapiSchema.definitions['http://json-schema.org/draft-07/schema'];
delete asyncapiSchema.definitions['http://json-schema.org/draft-04/schema'];
ajv.addSchema(asyncapiSchema, version);
validate = ajv.getSchema(version);
}
return validate;
Expand Down
17 changes: 17 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ utils.parseUrlQueryParameters = str => {
return str.match(/\?((.*=.*)(&?))/g);
};

/**
* Returns base URL parsed from location of AsyncAPI document
*
* @function getBaseUrl
* @private
* @param {String} url URL of AsyncAPI document
*/
utils.getBaseUrl = url => {
url = typeof url !== 'string' ? String(url) : url;
//URL validation is not performed because 'node-fetch' performs its own
//validation at fetch time, so no repetition of this task is made.
//Only ensuring that 'url' has type of 'string' and letting 'node-fetch' deal
//with the rest.

return url.substring(0, url.lastIndexOf('/') + 1);
};

/**
* Returns an array of not existing properties in provided object with names specified in provided array
* @function getMissingProps
Expand Down
Loading

0 comments on commit a7a5423

Please sign in to comment.