From dc9129bc25ba8ba4b004be671b8aa4801f1c1c55 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Thu, 23 Mar 2023 10:08:04 +0100 Subject: [PATCH] feat(oas31): add support for OpenAPI 3.1.0 badge Refs #8501 --- src/core/plugins/oas3/helpers.jsx | 15 +++++++++++++ .../oas3/wrap-components/version-stamp.jsx | 22 ++++++++++++------- src/core/plugins/oas31/index.js | 2 ++ .../oas31/wrap-components/version-stamp.jsx | 21 ++++++++++++++++++ test/e2e-cypress/tests/bugs/4865.js | 4 ++-- test/e2e-cypress/tests/features/oas3-badge.js | 15 +++++++++++++ 6 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/core/plugins/oas31/wrap-components/version-stamp.jsx create mode 100644 test/e2e-cypress/tests/features/oas3-badge.js diff --git a/src/core/plugins/oas3/helpers.jsx b/src/core/plugins/oas3/helpers.jsx index 79b95722ad8..9d46eef5435 100644 --- a/src/core/plugins/oas3/helpers.jsx +++ b/src/core/plugins/oas3/helpers.jsx @@ -32,3 +32,18 @@ export function OAS3ComponentWrapFactory(Component) { } } } + +export function OAS30ComponentWrapFactory(Component) { + return (Ori, system) => (props) => { + if (typeof system.specSelectors?.isOAS30 === "function") { + if (system.specSelectors.isOAS30()) { + return + } else { + return + } + } else { + console.warn("OAS30 wrapper: couldn't get spec") + return null + } + } +} diff --git a/src/core/plugins/oas3/wrap-components/version-stamp.jsx b/src/core/plugins/oas3/wrap-components/version-stamp.jsx index d0d188bde88..9ec70f1b5fc 100644 --- a/src/core/plugins/oas3/wrap-components/version-stamp.jsx +++ b/src/core/plugins/oas3/wrap-components/version-stamp.jsx @@ -1,13 +1,19 @@ +/** + * @prettier + */ import React from "react" -import { OAS3ComponentWrapFactory } from "../helpers" -export default OAS3ComponentWrapFactory((props) => { +import { OAS30ComponentWrapFactory } from "../helpers" + +export default OAS30ComponentWrapFactory((props) => { const { Ori } = props - return - - -
OAS3
-
-
+ return ( + + + +
OAS 3.0
+
+
+ ) }) diff --git a/src/core/plugins/oas31/index.js b/src/core/plugins/oas31/index.js index 11c60c1bcc7..855123f7621 100644 --- a/src/core/plugins/oas31/index.js +++ b/src/core/plugins/oas31/index.js @@ -11,6 +11,7 @@ import LicenseWrapper from "./wrap-components/license" import ContactWrapper from "./wrap-components/contact" import InfoWrapper from "./wrap-components/info" import VersionPragmaFilterWrapper from "./wrap-components/version-pragma-filter" +import VersionStampWrapper from "./wrap-components/version-stamp" import { license as selectLicense, contact as selectContact, @@ -71,6 +72,7 @@ const OAS31Plugin = ({ fn }) => { License: LicenseWrapper, Contact: ContactWrapper, VersionPragmaFilter: VersionPragmaFilterWrapper, + VersionStamp: VersionStampWrapper, }, statePlugins: { spec: { diff --git a/src/core/plugins/oas31/wrap-components/version-stamp.jsx b/src/core/plugins/oas31/wrap-components/version-stamp.jsx new file mode 100644 index 00000000000..6af4017eb23 --- /dev/null +++ b/src/core/plugins/oas31/wrap-components/version-stamp.jsx @@ -0,0 +1,21 @@ +/** + * @prettier + */ +import React from "react" + +const VersionStampWrapper = (Original, system) => (props) => { + if (system.specSelectors.isOAS31()) { + return ( + + + +
OAS 3.1
+
+
+ ) + } + + return +} + +export default VersionStampWrapper diff --git a/test/e2e-cypress/tests/bugs/4865.js b/test/e2e-cypress/tests/bugs/4865.js index 85573ef7555..22db26dc6a6 100644 --- a/test/e2e-cypress/tests/bugs/4865.js +++ b/test/e2e-cypress/tests/bugs/4865.js @@ -5,7 +5,7 @@ describe("#4865: multiple invocations + OAS3 plugin", () => { cy.visit("/?url=/documents/petstore-expanded.openapi.yaml") .get("#swagger-ui") .get("pre.version") - .contains("OAS3") + .contains("OAS 3.0") }) it("test: should render the OAS3 badge correctly after re-initializing the UI", () => { @@ -14,6 +14,6 @@ describe("#4865: multiple invocations + OAS3 plugin", () => { .then(win => win.onload()) // re-initializes Swagger UI .get("#swagger-ui") .get("pre.version") - .contains("OAS3") + .contains("OAS 3.0") }) }) diff --git a/test/e2e-cypress/tests/features/oas3-badge.js b/test/e2e-cypress/tests/features/oas3-badge.js new file mode 100644 index 00000000000..1db2ae5f642 --- /dev/null +++ b/test/e2e-cypress/tests/features/oas3-badge.js @@ -0,0 +1,15 @@ +describe("OpenAPI 3.x.y Badge", () => { + it("should display light green badge with version indicator for OpenAPI 3.0.x", () => { + cy.visit("/?url=/documents/petstore-expanded.openapi.yaml") + .get("#swagger-ui") + .get("pre.version") + .contains("OAS 3.0") + }) + + it("should display light green badge with version indicator for OpenAPI 3.1.0", () => { + cy.visit("/?url=/documents/features/info-openAPI31.yaml") + .get("#swagger-ui") + .get("pre.version") + .contains("OAS 3.1") + }) +})