From f7434039df2cd3ed3441928d7ebce8f3fd73e66d Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Mon, 8 Aug 2022 15:14:26 +0200 Subject: [PATCH] fix(adapters): force forward compatible dection to be valid as well This relates for following adapters: - asyncapi-json-2 - asyncapi-yaml-2 - openapi-json-3-1 - openapi-yaml-3-1 Refs #1741 --- .../src/adapter.ts | 3 ++- .../test/adapter.ts | 25 ++++++++++++++++++ .../src/adapter.ts | 2 +- .../test/adapter.ts | 26 +++++++++++++++++++ .../src/adapter.ts | 2 +- .../test/adapter.ts | 15 +++++++++++ .../src/adapter.ts | 2 +- .../test/adapter.ts | 15 +++++++++++ 8 files changed, 86 insertions(+), 4 deletions(-) diff --git a/packages/apidom-parser-adapter-asyncapi-json-2/src/adapter.ts b/packages/apidom-parser-adapter-asyncapi-json-2/src/adapter.ts index b4b3c2e26c..93ff670d14 100644 --- a/packages/apidom-parser-adapter-asyncapi-json-2/src/adapter.ts +++ b/packages/apidom-parser-adapter-asyncapi-json-2/src/adapter.ts @@ -6,7 +6,8 @@ import asyncApiNamespace, { AsyncApi2Element } from '@swagger-api/apidom-ns-asyn export { default as mediaTypes } from './media-types'; -export const detectionRegExp = /"asyncapi"\s*:\s*"(?2\.\d+\.\d+)"/; +export const detectionRegExp = + /"asyncapi"\s*:\s*"(?2\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))"/; export const detect = async (source: string): Promise => detectionRegExp.test(source) && (await detectJSON(source)); diff --git a/packages/apidom-parser-adapter-asyncapi-json-2/test/adapter.ts b/packages/apidom-parser-adapter-asyncapi-json-2/test/adapter.ts index da9de7d9bd..4dfb329ab3 100644 --- a/packages/apidom-parser-adapter-asyncapi-json-2/test/adapter.ts +++ b/packages/apidom-parser-adapter-asyncapi-json-2/test/adapter.ts @@ -71,4 +71,29 @@ describe('adapter', function () { assert.isTrue(parseResult.isEmpty); }); }); + + context('detectionRegExp', function () { + specify('should detect version ranges in forward compatible way', function () { + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.0.0"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.0.145"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.1.0"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.1.145"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.2.0"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.2.145"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.3.0"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.3.1"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.4.0"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.4.1"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.5.0"')); + assert.isTrue(adapter.detectionRegExp.test('"asyncapi": "2.5.1"')); + }); + + specify('should reject invalid version ranges', function () { + assert.isFalse(adapter.detectionRegExp.test('"asyncapi": "3.0.0"')); + assert.isFalse(adapter.detectionRegExp.test('"asyncapi": "3.1.0"')); + assert.isFalse(adapter.detectionRegExp.test('"asyncapi": "3.1.1"')); + assert.isFalse(adapter.detectionRegExp.test('"asyncapi": "2.01.0"')); + assert.isFalse(adapter.detectionRegExp.test('"asyncapi": "2.1.01"')); + }); + }); }); diff --git a/packages/apidom-parser-adapter-asyncapi-yaml-2/src/adapter.ts b/packages/apidom-parser-adapter-asyncapi-yaml-2/src/adapter.ts index 54ff84db70..76f267dc42 100644 --- a/packages/apidom-parser-adapter-asyncapi-yaml-2/src/adapter.ts +++ b/packages/apidom-parser-adapter-asyncapi-yaml-2/src/adapter.ts @@ -10,7 +10,7 @@ import asyncApiNamespace, { AsyncApi2Element } from '@swagger-api/apidom-ns-asyn export { default as mediaTypes } from './media-types'; export const detectionRegExp = - /(?^(["']?)asyncapi\2\s*:\s*(["']?)(?2\.\d+\.\d+)\3)|(?"asyncapi"\s*:\s*"(?2\.\d+\.\d+)")/m; + /(?^(["']?)asyncapi\2\s*:\s*(["']?)(?2\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?"asyncapi"\s*:\s*"(?2\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))")/m; export const detect = async (source: string): Promise => detectionRegExp.test(source) && (await detectYAML(source)); diff --git a/packages/apidom-parser-adapter-asyncapi-yaml-2/test/adapter.ts b/packages/apidom-parser-adapter-asyncapi-yaml-2/test/adapter.ts index bc9f25d672..d06284ec5b 100644 --- a/packages/apidom-parser-adapter-asyncapi-yaml-2/test/adapter.ts +++ b/packages/apidom-parser-adapter-asyncapi-yaml-2/test/adapter.ts @@ -92,4 +92,30 @@ describe('adapter', function () { assert.isTrue(positionEnd.equals(expectedEmptyPosition)); }); }); + + context('detectionRegExp', function () { + specify('should detect version ranges in forward compatible way', function () { + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.0.0')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.0.145')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.1.0')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.1.145')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.2.0')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.2.145')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.3.0')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.3.1')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.4.0')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.4.1')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.5.0')); + assert.isTrue(adapter.detectionRegExp.test('asyncapi: 2.5.1')); + }); + + specify('should reject invalid version ranges', function () { + assert.isFalse(adapter.detectionRegExp.test('asyncapi: 3.0.0')); + assert.isFalse(adapter.detectionRegExp.test('asyncapi: 3.1.0')); + assert.isFalse(adapter.detectionRegExp.test('asyncapi: 3.1.1')); + assert.isFalse(adapter.detectionRegExp.test('asyncapi: 2.01.0')); + assert.isFalse(adapter.detectionRegExp.test('asyncapi: 2.1.013')); + assert.isFalse(adapter.detectionRegExp.test('asyncapi: 2.1.013 ')); + }); + }); }); diff --git a/packages/apidom-parser-adapter-openapi-json-3-1/src/adapter.ts b/packages/apidom-parser-adapter-openapi-json-3-1/src/adapter.ts index a212b768c0..af292a523d 100644 --- a/packages/apidom-parser-adapter-openapi-json-3-1/src/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-json-3-1/src/adapter.ts @@ -6,7 +6,7 @@ import openApiNamespace, { OpenApi3_1Element } from '@swagger-api/apidom-ns-open export { default as mediaTypes } from './media-types'; -export const detectionRegExp = /"openapi"\s*:\s*"(?3\.1\.\d+)"/; +export const detectionRegExp = /"openapi"\s*:\s*"(?3\.1\.(?:[1-9]\d*|0))"/; export const detect = async (source: string): Promise => detectionRegExp.test(source) && (await detectJSON(source)); diff --git a/packages/apidom-parser-adapter-openapi-json-3-1/test/adapter.ts b/packages/apidom-parser-adapter-openapi-json-3-1/test/adapter.ts index b51f152b4c..fcc3e527ce 100644 --- a/packages/apidom-parser-adapter-openapi-json-3-1/test/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-json-3-1/test/adapter.ts @@ -71,4 +71,19 @@ describe('adapter', function () { assert.isTrue(parseResult.isEmpty); }); }); + + context('detectionRegExp', function () { + specify('should detect version ranges in forward compatible way', function () { + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.1.0"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.1.1"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.1.145"')); + }); + + specify('should reject invalid version ranges', function () { + assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.0.0"')); + assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.0.1"')); + assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.01.0"')); + assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.1.01"')); + }); + }); }); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/src/adapter.ts b/packages/apidom-parser-adapter-openapi-yaml-3-1/src/adapter.ts index 48ef8f15aa..547e82d394 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/src/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/src/adapter.ts @@ -10,7 +10,7 @@ import openApiNamespace, { OpenApi3_1Element } from '@swagger-api/apidom-ns-open export { default as mediaTypes } from './media-types'; export const detectionRegExp = - /(?^(["']?)openapi\2\s*:\s*(["']?)(?3\.1\.\d+)\3)|(?"openapi"\s*:\s*"(?3\.1\.\d+)")/m; + /(?^(["']?)openapi\2\s*:\s*(["']?)(?3\.1\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?"openapi"\s*:\s*"(?3\.1\.\d+)")/m; export const detect = async (source: string): Promise => detectionRegExp.test(source) && (await detectYAML(source)); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts index 32d9a90979..1df2f6c081 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts @@ -87,4 +87,19 @@ describe('adapter', function () { assert.instanceOf(subMappingValue.meta.get('sourceMap'), SourceMapElement); }); }); + + context('detectionRegExp', function () { + specify('should detect version ranges in forward compatible way', function () { + assert.isTrue(adapter.detectionRegExp.test('openapi: 3.1.0')); + assert.isTrue(adapter.detectionRegExp.test('openapi: 3.1.1')); + assert.isTrue(adapter.detectionRegExp.test('openapi: 3.1.15')); + }); + + specify('should reject invalid version ranges', function () { + assert.isFalse(adapter.detectionRegExp.test('openapi: 3.0.0')); + assert.isFalse(adapter.detectionRegExp.test('openapi: 3.0.1')); + assert.isFalse(adapter.detectionRegExp.test('openapi: 3.01.0')); + assert.isFalse(adapter.detectionRegExp.test('openapi: 3.1.01')); + }); + }); });