diff --git a/README.md b/README.md index 2d99a5945..2d14f1511 100644 --- a/README.md +++ b/README.md @@ -28,13 +28,15 @@ npm install asyncapi-parser [Check out the API page](./API.md). -##### Example +### Examples + +##### Example passing inline AsyncAPI ```js const parser = require('asyncapi-parser'); const doc = await parser.parse(` - asyncapi: '2.0.0-rc1' + asyncapi: '2.0.0' info: title: Example version: '0.1.0' @@ -58,7 +60,7 @@ console.log(doc.info().title()); // => Example ``` -##### Example +##### Example passing a URL ```js const parser = require('asyncapi-parser'); @@ -69,6 +71,14 @@ console.log(doc.info().title()); // => Example ``` +##### Example using OpenAPI schemas + +Head over to [asyncapi/openapi-schema-parser](https://www.github.com/asyncapi/openapi-schema-parser) for more information. + +##### Example using RAML data types + +Head over to [asyncapi/raml-dt-schema-parser](https://www.github.com/asyncapi/raml-dt-schema-parser) for more information. + ### Develop 1. Run tests with `npm test` diff --git a/lib/index.js b/lib/index.js index e43d15de2..4fa045760 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,4 @@ const parser = require('./parser'); -const openapiSchemaParser = require('./schema-parsers/openapi'); -const ramlDtParser = require('./schema-parsers/raml-datatype'); const noop = () => {}; // No operation @@ -13,14 +11,4 @@ parser.registerSchemaParser([ 'application/schema+yaml;version=draft-07', ], noop); -parser.registerSchemaParser([ - 'application/vnd.oai.openapi;version=3.0.0', - 'application/vnd.oai.openapi+json;version=3.0.0', - 'application/vnd.oai.openapi+yaml;version=3.0.0', -], openapiSchemaParser); - -parser.registerSchemaParser([ - 'application/raml+yaml;version=1.0', -], ramlDtParser); - module.exports = parser; diff --git a/lib/schema-parsers/openapi.js b/lib/schema-parsers/openapi.js deleted file mode 100644 index 5769e44e2..000000000 --- a/lib/schema-parsers/openapi.js +++ /dev/null @@ -1,58 +0,0 @@ -const toJsonSchema = require('@openapi-contrib/openapi-schema-to-json-schema'); - -module.exports = async ({ message, defaultSchemaFormat }) => { - const transformed = toJsonSchema(message.payload, { - cloneSchema: true, - keepNotSupported: [ - 'discriminator', - 'readOnly', - 'writeOnly', - 'deprecated', - 'xml', - 'example', - ], - }); - - iterateSchema(transformed); - - message['x-parser-original-schema-format'] = message.schemaFormat || defaultSchemaFormat; - message['x-parser-original-payload'] = message.payload; - message.payload = transformed; - delete message.schemaFormat; -}; - -function iterateSchema(schema) { - if (schema.example !== undefined) { - const examples = schema.examples || []; - examples.push(schema.example); - schema.examples = examples; - delete schema.example; - } - - if (schema.$schema !== undefined) { - delete schema.$schema; - } - - aliasProps(schema.properties); - aliasProps(schema.patternProperties); - aliasProps(schema.additionalProperties); - aliasProps(schema.items); - aliasProps(schema.additionalItems); - aliasProps(schema.oneOf); - aliasProps(schema.anyOf); - aliasProps(schema.allOf); - aliasProps(schema.not); -} - -function aliasProps(obj) { - for (let key in obj) { - const prop = obj[key]; - - if (prop.xml !== undefined) { - prop['x-xml'] = prop.xml; - delete prop.xml; - } - - iterateSchema(obj[key]); - } -} diff --git a/lib/schema-parsers/raml-datatype.js b/lib/schema-parsers/raml-datatype.js deleted file mode 100644 index 6bfe13e6c..000000000 --- a/lib/schema-parsers/raml-datatype.js +++ /dev/null @@ -1,23 +0,0 @@ -const yaml = require('js-yaml'); -const r2j = require('ramldt2jsonschema'); - -module.exports = async ({ message, defaultSchemaFormat }) => { - try { - let payload = message.payload; - if (typeof payload === 'object') { - payload = '#%RAML 1.0 Library\n' + - yaml.safeDump({types: {tmpType: payload}}); - } - - // Draft 6 is compatible with 7. - const jsonModel = await r2j.dt2js(payload, 'tmpType', {draft: '06'}); - const convertedType = jsonModel.definitions.tmpType; - - message['x-parser-original-schema-format'] = message.schemaFormat || defaultSchemaFormat; - message['x-parser-original-payload'] = payload; - message.payload = convertedType; - delete message.schemaFormat; - } catch (e) { - console.error(e); - } -}; diff --git a/package-lock.json b/package-lock.json index 04a0ae429..2132dcbf7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,14 +10,6 @@ "integrity": "sha512-0fKu/QqildpXmPVaRBoXOlyBb3MC+J0A66x97qEfLOMkn3u6nfY5esWogQwi/K0BjASYy4DbnsEWnpNL6qT5Mw==", "dev": true }, - "@openapi-contrib/openapi-schema-to-json-schema": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@openapi-contrib/openapi-schema-to-json-schema/-/openapi-schema-to-json-schema-3.0.0.tgz", - "integrity": "sha512-nM0Xn6lCwk1nt/fCWwiLBT1SbH4TlX099bWfz4h5lleW7yeu3SHGDP3knFBDHXPCLwywo5qqOOTVvjTTGf/7lA==", - "requires": { - "deep-equal": "^1.0.1" - } - }, "JSONStream": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.5.tgz", @@ -593,11 +585,6 @@ } } }, - "co": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", - "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=" - }, "collect-all": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/collect-all/-/collect-all-1.0.3.tgz", @@ -708,11 +695,6 @@ } } }, - "commander": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", - "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==" - }, "common-sequence": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/common-sequence/-/common-sequence-2.0.0.tgz", @@ -864,19 +846,6 @@ "type-detect": "^4.0.0" } }, - "deep-equal": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.1.1.tgz", - "integrity": "sha512-yd9c5AdiqVcR+JjcwUQb9DkhJc8ngNr0MahEBGvDiJw8puWab2yZlh+nkasOnZP+EGTAP6rRp2JzJhJZzvNF8g==", - "requires": { - "is-arguments": "^1.0.4", - "is-date-object": "^1.0.1", - "is-regex": "^1.0.4", - "object-is": "^1.0.1", - "object-keys": "^1.1.1", - "regexp.prototype.flags": "^1.2.0" - } - }, "deep-extend": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", @@ -887,6 +856,7 @@ "version": "1.1.3", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "dev": true, "requires": { "object-keys": "^1.0.12" } @@ -1027,6 +997,7 @@ "version": "1.17.4", "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.4.tgz", "integrity": "sha512-Ae3um/gb8F0mui/jPL+QiqmglkUsaQf7FwBEHYIFkztkneosu9imhqHpBzQ3h1vit8t5iQ74t6PEVvphBZiuiQ==", + "dev": true, "requires": { "es-to-primitive": "^1.2.1", "function-bind": "^1.1.1", @@ -1045,6 +1016,7 @@ "version": "1.2.1", "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz", "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==", + "dev": true, "requires": { "is-callable": "^1.1.4", "is-date-object": "^1.0.1", @@ -1274,7 +1246,8 @@ "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true }, "get-assigned-identifiers": { "version": "1.2.0", @@ -1373,6 +1346,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, "requires": { "function-bind": "^1.1.1" } @@ -1395,7 +1369,8 @@ "has-symbols": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz", - "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==" + "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==", + "dev": true }, "hash-base": { "version": "3.0.4", @@ -1495,11 +1470,6 @@ "xtend": "^4.0.0" } }, - "is-arguments": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.0.4.tgz", - "integrity": "sha512-xPh0Rmt8NE65sNzvyUmWgI1tz3mKq74lGA0mL8LYZcoIzKOzDh6HmrYm3d18k60nHerC8A9Km8kYu87zfSFnLA==" - }, "is-buffer": { "version": "1.1.6", "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", @@ -1509,12 +1479,14 @@ "is-callable": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.5.tgz", - "integrity": "sha512-ESKv5sMCJB2jnHTWZ3O5itG+O128Hsus4K4Qh1h2/cgn2vbgnLSVqfV46AeJA9D5EeeLa9w81KUXMtn34zhX+Q==" + "integrity": "sha512-ESKv5sMCJB2jnHTWZ3O5itG+O128Hsus4K4Qh1h2/cgn2vbgnLSVqfV46AeJA9D5EeeLa9w81KUXMtn34zhX+Q==", + "dev": true }, "is-date-object": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.2.tgz", - "integrity": "sha512-USlDT524woQ08aoZFzh3/Z6ch9Y/EWXEHQ/AaRN0SkKq4t2Jw2R2339tSXmwuVoY7LLlBCbOIlx2myP/L5zk0g==" + "integrity": "sha512-USlDT524woQ08aoZFzh3/Z6ch9Y/EWXEHQ/AaRN0SkKq4t2Jw2R2339tSXmwuVoY7LLlBCbOIlx2myP/L5zk0g==", + "dev": true }, "is-defined": { "version": "1.0.0", @@ -1532,6 +1504,7 @@ "version": "1.0.5", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.5.tgz", "integrity": "sha512-vlKW17SNq44owv5AQR3Cq0bQPEb8+kF3UKZ2fiZNOWtztYE5i0CzCZxFDwO58qAOWtxdBRVO/V5Qin1wjCqFYQ==", + "dev": true, "requires": { "has": "^1.0.3" } @@ -1540,6 +1513,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.3.tgz", "integrity": "sha512-OwijhaRSgqvhm/0ZdAcXNZt9lYdKFpcRDT5ULUuYXPoT794UNOdU+gpT6Rzo7b4V2HUl/op6GqY894AZwv9faQ==", + "dev": true, "requires": { "has-symbols": "^1.0.1" } @@ -1679,37 +1653,6 @@ "walk-back": "^4.0.0" } }, - "json-schema-migrate": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/json-schema-migrate/-/json-schema-migrate-0.2.0.tgz", - "integrity": "sha1-ukelsAcvxyOWRg4b1gtE1SF4u8Y=", - "requires": { - "ajv": "^5.0.0" - }, - "dependencies": { - "ajv": { - "version": "5.5.2", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz", - "integrity": "sha1-c7Xuyj+rZT49P5Qis0GtQiBdyWU=", - "requires": { - "co": "^4.6.0", - "fast-deep-equal": "^1.0.0", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.3.0" - } - }, - "fast-deep-equal": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz", - "integrity": "sha1-wFNHeBfIa1HaqFPIHgWbcz0CNhQ=" - }, - "json-schema-traverse": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz", - "integrity": "sha1-NJptRMU6Ud6JtAgFxdXlm0F9M0A=" - } - } - }, "json-schema-ref-parser": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/json-schema-ref-parser/-/json-schema-ref-parser-7.1.3.tgz", @@ -2064,17 +2007,14 @@ "object-inspect": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.7.0.tgz", - "integrity": "sha512-a7pEHdh1xKIAgTySUGgLMx/xwDZskN1Ud6egYYN3EdRW4ZMPNEDUTF+hwy2LUC+Bl+SyLXANnwz/jyh/qutKUw==" - }, - "object-is": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.0.2.tgz", - "integrity": "sha512-Epah+btZd5wrrfjkJZq1AOB9O6OxUQto45hzFd7lXGrpHPGE0W1k+426yrZV+k6NJOzLNNW/nVsmZdIWsAqoOQ==" + "integrity": "sha512-a7pEHdh1xKIAgTySUGgLMx/xwDZskN1Ud6egYYN3EdRW4ZMPNEDUTF+hwy2LUC+Bl+SyLXANnwz/jyh/qutKUw==", + "dev": true }, "object-keys": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", - "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==" + "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", + "dev": true }, "object-to-spawn-args": { "version": "1.1.1", @@ -2086,6 +2026,7 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz", "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==", + "dev": true, "requires": { "define-properties": "^1.1.2", "function-bind": "^1.1.1", @@ -2292,17 +2233,6 @@ "integrity": "sha1-nsYfeQSYdXB9aUFFlv2Qek1xHnM=", "dev": true }, - "ramldt2jsonschema": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/ramldt2jsonschema/-/ramldt2jsonschema-1.1.0.tgz", - "integrity": "sha512-JPGVxcuJhFAg2FRqNpiPQXak2jbTLVLfZRGv0biv9Gat/Zhomo2iUdnr4cQ1UJrVdGWzgGRr2nxXZoWj3+Bzcw==", - "requires": { - "commander": "^4.0.0", - "js-yaml": "^3.13.1", - "json-schema-migrate": "^0.2.0", - "webapi-parser": "^0.4.0" - } - }, "randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -2435,15 +2365,6 @@ } } }, - "regexp.prototype.flags": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.3.0.tgz", - "integrity": "sha512-2+Q0C5g951OlYlJz6yu5/M33IcsESLlLfsyIaLJaG4FA2r4yP8MvVMJUUP/fVBkSpbbbZlS5gynbEWLipiiXiQ==", - "requires": { - "define-properties": "^1.1.3", - "es-abstract": "^1.17.0-next.1" - } - }, "rename-function-calls": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/rename-function-calls/-/rename-function-calls-0.1.1.tgz", @@ -2728,6 +2649,7 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/string.prototype.trimleft/-/string.prototype.trimleft-2.1.1.tgz", "integrity": "sha512-iu2AGd3PuP5Rp7x2kEZCrB2Nf41ehzh+goo8TV7z8/XDBbsvc6HQIlUl9RjkZ4oyrW1XM5UwlGl1oVEaDjg6Ag==", + "dev": true, "requires": { "define-properties": "^1.1.3", "function-bind": "^1.1.1" @@ -2737,6 +2659,7 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/string.prototype.trimright/-/string.prototype.trimright-2.1.1.tgz", "integrity": "sha512-qFvWL3/+QIgZXVmJBfpHmxLB7xsUXz6HsUmP8+5dRaC3Q7oKUv9Vo6aMCRZC1smrtyECFsIT30PqBJ1gTjAs+g==", + "dev": true, "requires": { "define-properties": "^1.1.3", "function-bind": "^1.1.1" @@ -3061,32 +2984,6 @@ "integrity": "sha512-kudCA8PXVQfrqv2mFTG72vDBRi8BKWxGgFLwPpzHcpZnSwZk93WMwUDVcLHWNsnm+Y0AC4Vb6MUNRgaHfyV2DQ==", "dev": true }, - "webapi-parser": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/webapi-parser/-/webapi-parser-0.4.1.tgz", - "integrity": "sha512-F5oSV1ttCnm4H8tLyAiT6l564A1B0KLkVSBJs3jpyalDPkMn/qvpB+jniEWLNmhOroLCunV07xCCUKJTf98f7Q==", - "requires": { - "ajv": "6.5.2" - }, - "dependencies": { - "ajv": { - "version": "6.5.2", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.5.2.tgz", - "integrity": "sha512-hOs7GfvI6tUI1LfZddH82ky6mOMyTuY0mk7kE2pWpmhhUSkumzaTO5vbVwij39MdwPQWCV4Zv57Eo06NtL/GVA==", - "requires": { - "fast-deep-equal": "^2.0.1", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.4.1", - "uri-js": "^4.2.1" - } - }, - "fast-deep-equal": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", - "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" - } - } - }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/package.json b/package.json index f7ac56333..6a1f27c5d 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,6 @@ "js-yaml": "^3.13.1", "json-schema-ref-parser": "^7.1.0", "node-fetch": "^2.6.0", - "@openapi-contrib/openapi-schema-to-json-schema": "^3.0.0", - "ramldt2jsonschema": "^1.1.0", "tiny-merge-patch": "^0.1.2" }, "browserify": { diff --git a/test/asyncapi-openapi.yaml b/test/asyncapi-openapi.yaml deleted file mode 100644 index badaf8625..000000000 --- a/test/asyncapi-openapi.yaml +++ /dev/null @@ -1,44 +0,0 @@ -asyncapi: 2.0.0 -info: - title: My API - version: '1.0.0' -channels: - mychannel: - publish: - message: - $ref: '#/components/messages/testMessage' - -components: - messages: - testMessage: - schemaFormat: 'application/vnd.oai.openapi;version=3.0.0' - payload: - $ref: '#/components/schemas/testSchema' - schemas: - testSchema: - type: object - nullable: true - example: - name: Fran - properties: - name: - type: string - discriminatorTest: - discriminator: objectType - oneOf: - - type: object - properties: - objectType: - type: string - prop1: - type: string - - type: object - properties: - objectType: - type: string - prop2: - type: string - - test: - $ref: 'refs/refed.yaml' - \ No newline at end of file diff --git a/test/asyncapi-raml.yaml b/test/asyncapi-raml.yaml deleted file mode 100644 index 8b91c8ecc..000000000 --- a/test/asyncapi-raml.yaml +++ /dev/null @@ -1,32 +0,0 @@ -asyncapi: 2.0.0 -info: - title: Example using RAML data types - version: '1.0.0' -channels: - mychannel: - publish: - message: - $ref: '#/components/messages/testMessage' - otherchannel: - subscribe: - message: - schemaFormat: 'application/raml+yaml;version=1.0' - payload: - $ref: 'complex_cat.raml#/types/Cat' - -components: - messages: - testMessage: - schemaFormat: 'application/raml+yaml;version=1.0' - payload: - type: object - properties: - title: string - author: - type: string - examples: - anExample: Eva - examples: - exampleOne: - title: A book - author: An author diff --git a/test/complex_cat.raml b/test/complex_cat.raml deleted file mode 100644 index b0c18da3f..000000000 --- a/test/complex_cat.raml +++ /dev/null @@ -1,111 +0,0 @@ -#%RAML 1.0 Library - -types: - NameXML: - displayName: Name XML - type: string - xml: - attribute: false - wrapped: true - name: cat - prefix: animal - - CatName: - type: NameXML - default: "regular cat" - example: fluffy - displayName: Cat - description: "Cat name" - facets: - amazing: boolean - - CatBreed: - type: string - pattern: "\\w{3,}" - examples: - dogone: "CatOne" - dogtwo: "CatTwo" - enum: ["CatOne", "CatTwo", "CatThree"] - - CatPros: - type: array - uniqueItems: false - items: string - minItems: 1 - maxItems: 100 - - CatCons: - type: array - uniqueItems: true - items: - type: string - minLength: 1 - maxLength: 100 - - CatAge: - type: number - minimum: 1 - maximum: 50 - format: int32 - - CatWithCity: - additionalProperties: false - properties: - city: string - - CatWithAddress: - additionalProperties: false - properties: - address: string - - Cat: - type: [CatWithAddress, CatWithCity] - minProperties: 1 - maxProperties: 50 - additionalProperties: false - discriminator: breed - discriminatorValue: CatOne - properties: - proscons: - type: CatPros | CatCons - required: true - name: - type: CatName - amazing: true - breed: - type: CatBreed - age: CatAge - rating: - type: integer - multipleOf: 5 - example: - displayName: Cat's rating - description: Rating of cat's awesomeness - strict: false - value: 50 - year_of_birth: date-only - time_of_birth: time-only - dt_of_birth: - type: datetime-only - required: false - addition_date: - type: datetime - format: rfc2616 - removal_date: - type: datetime - photo: - type: file - fileTypes: ['image/jpeg', 'image/png'] - minLength: 1 - maxLength: 307200 - description: nil - habits?: string - character: nil | string - siblings: string[] - parents: CatName[] - ratingHistory: (integer | number)[] - additionalData: - type: - type: object - properties: - weight: number diff --git a/test/parse_test.js b/test/parse_test.js index 0b21ee021..344bd3bac 100644 --- a/test/parse_test.js +++ b/test/parse_test.js @@ -12,10 +12,6 @@ const invalidYAML = fs.readFileSync(path.resolve(__dirname, "./malformed-asyncap const inputYAML = fs.readFileSync(path.resolve(__dirname, "./asyncapi.yaml"), 'utf8'); const outputJSON = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"externalDocs":{"x-extension":true,"url":"https://company.com/docs"},"message":{"payload":{"type":"object","properties":{"name":{"type":"string"},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"},"x-some-extension":"some extension","x-parser-original-traits":[{"x-some-extension":"some extension"}],"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"},"x-parser-original-traits":[{"externalDocs":{"url":"https://company.com/docs"}}]}}},"components":{"messages":{"testMessage":{"payload":{"type":"object","properties":{"name":{"type":"string"},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"},"x-some-extension":"some extension","x-parser-original-traits":[{"x-some-extension":"some extension"}],"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"}},"schemas":{"testSchema":{"type":"object","properties":{"name":{"type":"string"},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"}},"messageTraits":{"extension":{"x-some-extension":"some extension"}},"operationTraits":{"docs":{"externalDocs":{"url":"https://company.com/docs"}}}}}'; const outputJsonNoApplyTraits = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"traits":[{"externalDocs":{"url":"https://company.com/docs"}}],"externalDocs":{"x-extension":true,"url":"https://irrelevant.com"},"message":{"traits":[{"x-some-extension":"some extension"}],"payload":{"type":"object","properties":{"name":{"type":"string"},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"}}}},"components":{"messages":{"testMessage":{"traits":[{"x-some-extension":"some extension"}],"payload":{"type":"object","properties":{"name":{"type":"string"},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"}},"schemas":{"testSchema":{"type":"object","properties":{"name":{"type":"string"},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"}},"messageTraits":{"extension":{"x-some-extension":"some extension"}},"operationTraits":{"docs":{"externalDocs":{"url":"https://company.com/docs"}}}}}'; -const inputWithOpenAPI = fs.readFileSync(path.resolve(__dirname, "./asyncapi-openapi.yaml"), 'utf8'); -const outputWithOpenAPI = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":["object","null"],"properties":{"name":{"type":"string"},"discriminatorTest":{"discriminator":"objectType","oneOf":[{"type":"object","properties":{"objectType":{"type":"string"},"prop1":{"type":"string"}}},{"type":"object","properties":{"objectType":{"type":"string"},"prop2":{"type":"string"}}}]},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"examples":[{"name":"Fran"}],"x-parser-schema-id":""},"x-parser-original-schema-format":"application/vnd.oai.openapi;version=3.0.0","x-parser-original-payload":{"type":"object","nullable":true,"example":{"name":"Fran"},"properties":{"name":{"type":"string"},"discriminatorTest":{"discriminator":"objectType","oneOf":[{"type":"object","properties":{"objectType":{"type":"string"},"prop1":{"type":"string"}}},{"type":"object","properties":{"objectType":{"type":"string"},"prop2":{"type":"string"}}}]},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"}}}},"components":{"messages":{"testMessage":{"payload":{"type":["object","null"],"properties":{"name":{"type":"string"},"discriminatorTest":{"discriminator":"objectType","oneOf":[{"type":"object","properties":{"objectType":{"type":"string"},"prop1":{"type":"string"}}},{"type":"object","properties":{"objectType":{"type":"string"},"prop2":{"type":"string"}}}]},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"examples":[{"name":"Fran"}],"x-parser-schema-id":""},"x-parser-original-schema-format":"application/vnd.oai.openapi;version=3.0.0","x-parser-original-payload":{"type":"object","nullable":true,"example":{"name":"Fran"},"properties":{"name":{"type":"string"},"discriminatorTest":{"discriminator":"objectType","oneOf":[{"type":"object","properties":{"objectType":{"type":"string"},"prop1":{"type":"string"}}},{"type":"object","properties":{"objectType":{"type":"string"},"prop2":{"type":"string"}}}]},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"},"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"}},"schemas":{"testSchema":{"type":"object","nullable":true,"example":{"name":"Fran"},"properties":{"name":{"type":"string"},"discriminatorTest":{"discriminator":"objectType","oneOf":[{"type":"object","properties":{"objectType":{"type":"string"},"prop1":{"type":"string"}}},{"type":"object","properties":{"objectType":{"type":"string"},"prop2":{"type":"string"}}}]},"test":{"type":"object","properties":{"testing":{"type":"string"}}}},"x-parser-schema-id":"testSchema"}}}}'; -const inputWithRAML = fs.readFileSync(path.resolve(__dirname, "./asyncapi-raml.yaml"), 'utf8'); -const outputWithRAML = JSON.stringify({"asyncapi":"2.0.0","info":{"title":"Example using RAML data types","version":"1.0.0"},"channels":{"mychannel":{"publish":{"message":{"payload":{"type":"object","examples":[{"title":"A book","author":"An author"}],"additionalProperties":true,"required":["title","author"],"properties":{"title":{"type":"string"},"author":{"type":"string","examples":["Eva"]}},"x-parser-schema-id":""},"x-parser-original-schema-format":"application/raml+yaml;version=1.0","x-parser-original-payload":"#%RAML 1.0 Library\ntypes:\n tmpType:\n type: object\n properties:\n title: string\n author:\n type: string\n examples:\n anExample: Eva\n examples:\n exampleOne:\n title: A book\n author: An author\n","schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"}}},"otherchannel":{"subscribe":{"message":{"payload":{"minProperties":1,"maxProperties":50,"additionalProperties":false,"discriminator":"breed","discriminatorValue":"CatOne","type":"object","required":["proscons","name","breed","age","rating","year_of_birth","time_of_birth","addition_date","removal_date","photo","description","character","siblings","parents","ratingHistory","additionalData"],"properties":{"proscons":{"anyOf":[true,true]},"name":true,"breed":true,"age":true,"rating":{"type":"integer","multipleOf":5,"example":{"displayName":"Cat's rating","description":"Rating of cat's awesomeness","strict":false,"value":50}},"year_of_birth":{"type":"string","format":"date"},"time_of_birth":{"type":"string","format":"time"},"dt_of_birth":{"type":"string","format":"date-time-only"},"addition_date":{"type":"string","format":"rfc2616"},"removal_date":{"type":"string","format":"date-time"},"photo":{"type":"string","minLength":1,"maxLength":307200},"description":{"type":"null"},"habits":{"type":"string"},"character":{"anyOf":[{"type":"null"},{"type":"string"}]},"siblings":{"type":"array","items":{"type":"string"}},"parents":{"type":"array","items":true},"ratingHistory":{"type":"array","items":{"anyOf":[{"type":"integer"},{"type":"number"}]}},"additionalData":{"type":"object","additionalProperties":true,"required":["weight"],"properties":{"weight":{"type":"number"}}}},"x-parser-schema-id":""},"x-parser-original-schema-format":"application/raml+yaml;version=1.0","x-parser-original-payload":"#%RAML 1.0 Library\ntypes:\n tmpType:\n type:\n - CatWithAddress\n - CatWithCity\n minProperties: 1\n maxProperties: 50\n additionalProperties: false\n discriminator: breed\n discriminatorValue: CatOne\n properties:\n proscons:\n type: CatPros | CatCons\n required: true\n name:\n type: CatName\n amazing: true\n breed:\n type: CatBreed\n age: CatAge\n rating:\n type: integer\n multipleOf: 5\n example:\n displayName: Cat's rating\n description: Rating of cat's awesomeness\n strict: false\n value: 50\n year_of_birth: date-only\n time_of_birth: time-only\n dt_of_birth:\n type: datetime-only\n required: false\n addition_date:\n type: datetime\n format: rfc2616\n removal_date:\n type: datetime\n photo:\n type: file\n fileTypes:\n - image/jpeg\n - image/png\n minLength: 1\n maxLength: 307200\n description: nil\n habits?: string\n character: nil | string\n siblings: 'string[]'\n parents: 'CatName[]'\n ratingHistory: '(integer | number)[]'\n additionalData:\n type:\n type: object\n properties:\n weight: number\n","schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":""}}}},"components":{"messages":{"testMessage":{"payload":{"type":"object","examples":[{"title":"A book","author":"An author"}],"additionalProperties":true,"required":["title","author"],"properties":{"title":{"type":"string"},"author":{"type":"string","examples":["Eva"]}},"x-parser-schema-id":""},"x-parser-original-schema-format":"application/raml+yaml;version=1.0","x-parser-original-payload":"#%RAML 1.0 Library\ntypes:\n tmpType:\n type: object\n properties:\n title: string\n author:\n type: string\n examples:\n anExample: Eva\n examples:\n exampleOne:\n title: A book\n author: An author\n","schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-name":"testMessage"}}}}); const invalidAsyncAPI = { "asyncapi": "2.0.0", "info": {} }; const errorsOfInvalidAsyncAPI = [{keyword: 'required',dataPath: '.info',schemaPath: '#/required',params: { missingProperty: 'title' },message: 'should have required property \'title\''},{keyword: 'required',dataPath: '.info',schemaPath: '#/required',params: { missingProperty: 'version' },message: 'should have required property \'version\''},{keyword: 'required',dataPath: '',schemaPath: '#/required',params: { missingProperty: 'channels' },message: 'should have required property \'channels\''}]; @@ -74,14 +70,4 @@ describe('parse()', function () { await expect(testFn()) .to.be.rejectedWith(ParserError) }); - - it('should parse OpenAPI schemas', async function () { - const result = await parser.parse(inputWithOpenAPI, { path: __filename }); - await expect(JSON.stringify(result.json())).to.equal(outputWithOpenAPI); - }); - - it('should parse RAML data types', async function () { - const result = await parser.parse(inputWithRAML, { path: __filename }); - await expect(JSON.stringify(result.json())).to.equal(outputWithRAML); - }); });