From d837b2b511594f434ecec79d3799e90a11a0428f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Mon, 20 Mar 2023 09:44:22 +0100 Subject: [PATCH] refactor(oas31): disable try it out mechanism explicitly for webhooks (#8485) Refs #8474 --- .../plugins/oas3/components/request-body.jsx | 10 ++++---- src/core/plugins/oas31/fn.js | 22 ++++++++++++++++- src/core/plugins/oas31/index.js | 10 ++++++-- .../oas31/oas3-extensions/wrap-selectors.js | 14 +++++++++++ .../oas31/spec-extensions/wrap-selectors.js | 24 +++++-------------- 5 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 src/core/plugins/oas31/oas3-extensions/wrap-selectors.js diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index 1f9905a7..df3159fb 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -6,13 +6,13 @@ import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "c import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse" export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey) => { - const mediaTypeValue = requestBody?.getIn(["content", mediaType]) - const schema = mediaTypeValue?.get("schema").toJS() + const mediaTypeValue = requestBody.getIn(["content", mediaType]) + const schema = mediaTypeValue.get("schema").toJS() - const hasExamplesKey = mediaTypeValue?.get("examples") !== undefined - const exampleSchema = mediaTypeValue?.get("example") + const hasExamplesKey = mediaTypeValue.get("examples") !== undefined + const exampleSchema = mediaTypeValue.get("example") const mediaTypeExample = hasExamplesKey - ? mediaTypeValue?.getIn([ + ? mediaTypeValue.getIn([ "examples", activeExamplesKey, "value" diff --git a/src/core/plugins/oas31/fn.js b/src/core/plugins/oas31/fn.js index 55597e5b..e3a5533e 100644 --- a/src/core/plugins/oas31/fn.js +++ b/src/core/plugins/oas31/fn.js @@ -11,7 +11,7 @@ export const isOAS31 = (jsSpec) => { } /** - * Creates selector that returns value of the original + * Creates selector that returns value of the passed * selector when spec is OpenAPI 3.1.0., null otherwise. * * @param selector @@ -31,6 +31,26 @@ export const createOnlyOAS31Selector = } } +/** + * Creates selector wrapper that returns value of the passed + * selector when spec is OpenAPI 3.1.0., calls original selector otherwise. + * + * + * @param selector + * @returns {function(*, *): function(*, ...[*]): (*)} + */ +export const createOnlyOAS31SelectorWrapper = + (selector) => + (oriSelector, system) => + (state, ...args) => { + if (system.getSystem().specSelectors.isOAS31()) { + const result = selector(state, ...args) + return typeof result === "function" ? result(system) : result + } else { + return oriSelector(...args) + } + } + /** * Creates selector that provides system as the * second argument. This allows to create memoized diff --git a/src/core/plugins/oas31/index.js b/src/core/plugins/oas31/index.js index 6c008988..d05b51ca 100644 --- a/src/core/plugins/oas31/index.js +++ b/src/core/plugins/oas31/index.js @@ -32,9 +32,10 @@ import { selectWebhooksOperations, } from "./spec-extensions/selectors" import { - isOAS3 as isOAS3Wrapper, + isOAS3 as isOAS3SelectorWrapper, selectLicenseUrl as selectLicenseUrlWrapper, } from "./spec-extensions/wrap-selectors" +import { hasUserEditedBody as hasUserEditedBodySelectorWrapper } from "./oas3-extensions/wrap-selectors" import { selectLicenseUrl as selectOAS31LicenseUrl } from "./selectors" import { isOAS31 as isOAS31Fn, @@ -94,10 +95,15 @@ const OAS31Plugin = ({ fn }) => { selectWebhooksOperations: createOnlyOAS31Selector(createSystemSelector(selectWebhooksOperations)), // prettier-ignore }, wrapSelectors: { - isOAS3: isOAS3Wrapper, + isOAS3: isOAS3SelectorWrapper, selectLicenseUrl: selectLicenseUrlWrapper, }, }, + oas3: { + wrapSelectors: { + hasUserEditedBody: hasUserEditedBodySelectorWrapper, + }, + }, oas31: { selectors: { selectLicenseUrl: createOnlyOAS31Selector(createSystemSelector(selectOAS31LicenseUrl)), // prettier-ignore diff --git a/src/core/plugins/oas31/oas3-extensions/wrap-selectors.js b/src/core/plugins/oas31/oas3-extensions/wrap-selectors.js new file mode 100644 index 00000000..65dde17d --- /dev/null +++ b/src/core/plugins/oas31/oas3-extensions/wrap-selectors.js @@ -0,0 +1,14 @@ +import { createOnlyOAS31SelectorWrapper } from "../fn" + +export const hasUserEditedBody = createOnlyOAS31SelectorWrapper( + (state, path, method) => (system) => { + const webhooks = system.specSelectors.webhooks() + + if (webhooks.hasIn([path, method])) { + // try it out functionality is disabled for webhooks + return false + } + + return system.oas3Selectors.hasUserEditedBody([path, method]) + } +) diff --git a/src/core/plugins/oas31/spec-extensions/wrap-selectors.js b/src/core/plugins/oas31/spec-extensions/wrap-selectors.js index 1411f291..048186ad 100644 --- a/src/core/plugins/oas31/spec-extensions/wrap-selectors.js +++ b/src/core/plugins/oas31/spec-extensions/wrap-selectors.js @@ -2,21 +2,7 @@ * @prettier */ -/** - * Selector wrapper maker the only wraps the passed selector - * when spec is of OpenAPI 3.1.0 version. - */ -const onlyOAS31Wrap = - (selector) => - (oriSelector, system) => - (state, ...args) => { - if (system.getSystem().specSelectors.isOAS31()) { - const result = selector(state, ...args) - return typeof result === "function" ? result(system) : result - } else { - return oriSelector(...args) - } - } +import { createOnlyOAS31SelectorWrapper } from "../fn" export const isOAS3 = (oriSelector, system) => @@ -25,6 +11,8 @@ export const isOAS3 = return isOAS31 || oriSelector(...args) } -export const selectLicenseUrl = onlyOAS31Wrap(() => (system) => { - return system.oas31Selectors.selectLicenseUrl() -}) +export const selectLicenseUrl = createOnlyOAS31SelectorWrapper( + () => (system) => { + return system.oas31Selectors.selectLicenseUrl() + } +)