fix(oas3): expand Callback operation without browser error (#8509)

Refs #8508
This commit is contained in:
Vladimír Gorej
2023-03-23 15:12:17 +01:00
committed by GitHub
parent 3d3fea09c6
commit 4dc83b900d
3 changed files with 63 additions and 75 deletions

View File

@@ -9,16 +9,19 @@ import { stringify } from "../../utils"
// Helpers
function onlyOAS3(selector) {
return (...args) =>
const onlyOAS3 =
(selector) =>
(state, ...args) =>
(system) => {
if (system.getSystem().specSelectors.isOAS3()) {
return selector(...args)
const selectedValue = selector(state, ...args)
return typeof selectedValue === "function"
? selectedValue(system)
: selectedValue
} else {
return null
}
}
}
function validateRequestBodyIsRequired(selector) {
return (...args) =>
@@ -98,13 +101,28 @@ export const selectDefaultRequestBodyValue =
return null
}
export const hasUserEditedBody = (state, path, method) => (system) => {
const { oas3Selectors, specSelectors } = system.getSystem()
export const hasUserEditedBody = onlyOAS3((state, path, method) => (system) => {
const { oas3Selectors, specSelectors } = system
if (specSelectors.isOAS3()) {
let userHasEditedBody = false
const currentMediaType = oas3Selectors.requestContentType(path, method)
let userEditedRequestBody = oas3Selectors.requestBodyValue(path, method)
const requestBody = specSelectors.specResolvedSubtree([
"paths",
path,
method,
"requestBody",
])
/**
* The only request body that can currently be edited is for Path Items that are direct values of OpenAPI.paths.
* Path Item contained within the Callback Object or OpenAPI.webhooks (OpenAPI 3.1.0) have `Try it out`
* disabled and thus body cannot be edited.
*/
if (!requestBody) {
return false
}
if (Map.isMap(userEditedRequestBody)) {
// context is not application/json media-type
userEditedRequestBody = stringify(
@@ -118,14 +136,10 @@ export const hasUserEditedBody = (state, path, method) => (system) => {
if (List.isList(userEditedRequestBody)) {
userEditedRequestBody = stringify(userEditedRequestBody)
}
if (currentMediaType) {
const currentMediaTypeDefaultBodyValue = getDefaultRequestBodyValue(
specSelectors.specResolvedSubtree([
"paths",
path,
method,
"requestBody",
]),
requestBody,
currentMediaType,
oas3Selectors.activeExamplesMember(
path,
@@ -139,10 +153,7 @@ export const hasUserEditedBody = (state, path, method) => (system) => {
userEditedRequestBody !== currentMediaTypeDefaultBodyValue
}
return userHasEditedBody
} else {
return null
}
}
})
export const requestBodyInclusionSetting = onlyOAS3((state, path, method) => {
return state.getIn(["requestData", path, method, "bodyInclusion"]) || Map()

View File

@@ -41,7 +41,6 @@ import {
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,
@@ -112,11 +111,6 @@ const OAS31Plugin = ({ fn }) => {
selectLicenseUrl: selectLicenseUrlWrapper,
},
},
oas3: {
wrapSelectors: {
hasUserEditedBody: hasUserEditedBodySelectorWrapper,
},
},
oas31: {
selectors: {
selectLicenseUrl: createOnlyOAS31Selector(createSystemSelector(selectOAS31LicenseUrl)), // prettier-ignore

View File

@@ -1,17 +0,0 @@
/**
* @prettier
*/
import { createOnlyOAS31SelectorWrapper } from "../fn"
export const hasUserEditedBody = createOnlyOAS31SelectorWrapper(
(state, path, method) => (oriSelector, system) => {
const webhooks = system.specSelectors.webhooks()
if (webhooks.hasIn([path, method])) {
// try it out functionality is disabled for webhooks
return false
}
return oriSelector(path, method)
}
)