From 681fcc1a449d89828605a30c299639c8f1293c0e Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Tue, 24 Dec 2024 07:51:19 +0100 Subject: [PATCH] feat(parser-adapter-openapi-json-3-0): add support for OpenAPI 3.0.4 Refs #4612 --- .../src/adapter.ts | 2 +- .../test/adapter.ts | 22 +++++++++++++-- .../test/fixtures/sample-api.json | 2 +- .../test/fixtures/sample-api.yaml | 2 +- .../test/media-types.ts | 28 +++++++++++++++++-- .../src/adapter.ts | 2 +- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/packages/apidom-parser-adapter-openapi-json-3-0/src/adapter.ts b/packages/apidom-parser-adapter-openapi-json-3-0/src/adapter.ts index a523a20d19..05b7deb595 100644 --- a/packages/apidom-parser-adapter-openapi-json-3-0/src/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-json-3-0/src/adapter.ts @@ -9,7 +9,7 @@ export { default as mediaTypes } from './media-types.ts'; /** * @public */ -export const detectionRegExp = /"openapi"\s*:\s*"(?3\.0\.([0123]))"/; +export const detectionRegExp = /"openapi"\s*:\s*"(?3\.0\.(?:[1-9]\d*|0))"/; /** * @public diff --git a/packages/apidom-parser-adapter-openapi-json-3-0/test/adapter.ts b/packages/apidom-parser-adapter-openapi-json-3-0/test/adapter.ts index f2b49cbf45..71d6234a95 100644 --- a/packages/apidom-parser-adapter-openapi-json-3-0/test/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-json-3-0/test/adapter.ts @@ -21,8 +21,8 @@ describe('adapter', function () { assert.isFalse(await adapter.detect('{"openapi": "3.1.0"}')); }); - specify('should not detect patch version bump', async function () { - assert.isFalse(await adapter.detect('{"openapi": "3.0.4"}')); + specify('should detect patch version bump', async function () { + assert.isTrue(await adapter.detect('{"openapi": "3.0.24"}')); }); specify('should not detect minor and patch version bump', async function () { @@ -75,8 +75,24 @@ describe('adapter', function () { }); context('detectionRegExp', function () { + specify('should detect version ranges in forward compatible way', function () { + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.0"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.1"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.2"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.3"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.4"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.5"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.6"')); + assert.isTrue(adapter.detectionRegExp.test('"openapi": "3.0.145"')); + }); + + specify('should reject rc version ranges', function () { + assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.0.0-rc2')); + assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.0.0-rc1"')); + assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.0.0-rc0"')); + }); + specify('should reject invalid version ranges', function () { - assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.0.4"')); assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.1.145"')); assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.1.0"')); assert.isFalse(adapter.detectionRegExp.test('"openapi": "3.01.0"')); diff --git a/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.json b/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.json index 579d14dad9..fd0bb7f5c9 100644 --- a/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.json +++ b/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.json @@ -1,5 +1,5 @@ { - "openapi": "3.0.3", + "openapi": "3.0.4", "info": { "title": "Sample Pet Store App", "description": "This is a sample server for a pet store.", diff --git a/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.yaml b/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.yaml index 33a5243301..bc41f09e67 100644 --- a/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.yaml +++ b/packages/apidom-parser-adapter-openapi-json-3-0/test/fixtures/sample-api.yaml @@ -1,5 +1,5 @@ --- -openapi: 3.0.3 +openapi: 3.0.4 info: title: Sample Pet Store App description: This is a sample server for a pet store. diff --git a/packages/apidom-parser-adapter-openapi-json-3-0/test/media-types.ts b/packages/apidom-parser-adapter-openapi-json-3-0/test/media-types.ts index d5640ef531..b91dba98b6 100644 --- a/packages/apidom-parser-adapter-openapi-json-3-0/test/media-types.ts +++ b/packages/apidom-parser-adapter-openapi-json-3-0/test/media-types.ts @@ -6,6 +6,14 @@ import * as openApiJsonAdapter from '../src/adapter.ts'; describe('given adapter is used in parser', function () { const parser = new ApiDOMParser().use(openApiJsonAdapter); + context('given OpenAPI 3.0.4 definition in JSON format', function () { + specify('should find appropriate media type', async function () { + const mediaType = await parser.findMediaType('{"openapi": "3.0.4"}'); + + assert.strictEqual(mediaType, 'application/vnd.oai.openapi+json;version=3.0.4'); + }); + }); + context('given OpenAPI 3.0.3 definition in JSON format', function () { specify('should find appropriate media type', async function () { const mediaType = await parser.findMediaType('{"openapi": "3.0.3"}'); @@ -38,9 +46,25 @@ describe('given adapter is used in parser', function () { }); }); - context('given OpenAPI 3.0.3-rc3 definition in JSON format', function () { + context('given OpenAPI 3.0.0-rc2 definition in JSON format', function () { + specify('should not find appropriate media type', async function () { + const mediaType = await parser.findMediaType('{"openapi": "3.0.0-rc2"}'); + + assert.strictEqual(mediaType, 'application/octet-stream'); + }); + }); + + context('given OpenAPI 3.0.0-rc1 definition in JSON format', function () { + specify('should not find appropriate media type', async function () { + const mediaType = await parser.findMediaType('{"openapi": "3.0.0-rc1"}'); + + assert.strictEqual(mediaType, 'application/octet-stream'); + }); + }); + + context('given OpenAPI 3.0.0-rc0 definition in JSON format', function () { specify('should not find appropriate media type', async function () { - const mediaType = await parser.findMediaType('{"openapi": "3.0.3-rc3"}'); + const mediaType = await parser.findMediaType('{"openapi": "3.0.0-rc1"}'); assert.strictEqual(mediaType, 'application/octet-stream'); }); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-0/src/adapter.ts b/packages/apidom-parser-adapter-openapi-yaml-3-0/src/adapter.ts index 86cc3396a7..5ad521b28d 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-0/src/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-yaml-3-0/src/adapter.ts @@ -13,7 +13,7 @@ export { default as mediaTypes } from './media-types.ts'; * @public */ export const detectionRegExp = - /(?^(["']?)openapi\2\s*:\s*(["']?)(?3\.0\.[0123](?:-rc[012])?)\3(?:\s+|$))|(?"openapi"\s*:\s*"(?3\.0\.[0123](?:-rc[012])?)")/m; + /(?^(["']?)openapi\2\s*:\s*(["']?)(?3\.0\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?"openapi"\s*:\s*"(?3\.0\.(?:[1-9]\d*|0))")/m; /** * @public