Skip to content

Commit

Permalink
fix(oas3): expand Callback operation without browser error (#8509)
Browse files Browse the repository at this point in the history
Refs #8508
  • Loading branch information
char0n authored Mar 23, 2023
1 parent 3d3fea0 commit 4dc83b9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 74 deletions.
113 changes: 62 additions & 51 deletions src/core/plugins/oas3/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import { stringify } from "../../utils"

// Helpers

function onlyOAS3(selector) {
return (...args) =>
(system) => {
if (system.getSystem().specSelectors.isOAS3()) {
return selector(...args)
} else {
return null
}
const onlyOAS3 =
(selector) =>
(state, ...args) =>
(system) => {
if (system.getSystem().specSelectors.isOAS3()) {
const selectedValue = selector(state, ...args)
return typeof selectedValue === "function"
? selectedValue(system)
: selectedValue
} else {
return null
}
}
}

function validateRequestBodyIsRequired(selector) {
return (...args) =>
Expand Down Expand Up @@ -98,51 +101,59 @@ export const selectDefaultRequestBodyValue =
return null
}

export const hasUserEditedBody = (state, path, method) => (system) => {
const { oas3Selectors, specSelectors } = system.getSystem()

if (specSelectors.isOAS3()) {
let userHasEditedBody = false
const currentMediaType = oas3Selectors.requestContentType(path, method)
let userEditedRequestBody = oas3Selectors.requestBodyValue(path, method)
if (Map.isMap(userEditedRequestBody)) {
// context is not application/json media-type
userEditedRequestBody = stringify(
userEditedRequestBody
.mapEntries((kv) =>
Map.isMap(kv[1]) ? [kv[0], kv[1].get("value")] : kv
)
.toJS()
)
}
if (List.isList(userEditedRequestBody)) {
userEditedRequestBody = stringify(userEditedRequestBody)
}
if (currentMediaType) {
const currentMediaTypeDefaultBodyValue = getDefaultRequestBodyValue(
specSelectors.specResolvedSubtree([
"paths",
path,
method,
"requestBody",
]),
currentMediaType,
oas3Selectors.activeExamplesMember(
path,
method,
"requestBody",
"requestBody"
export const hasUserEditedBody = onlyOAS3((state, path, method) => (system) => {
const { oas3Selectors, specSelectors } = system

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(
userEditedRequestBody
.mapEntries((kv) =>
Map.isMap(kv[1]) ? [kv[0], kv[1].get("value")] : kv
)
.toJS()
)
}
if (List.isList(userEditedRequestBody)) {
userEditedRequestBody = stringify(userEditedRequestBody)
}

if (currentMediaType) {
const currentMediaTypeDefaultBodyValue = getDefaultRequestBodyValue(
requestBody,
currentMediaType,
oas3Selectors.activeExamplesMember(
path,
method,
"requestBody",
"requestBody"
)
userHasEditedBody =
!!userEditedRequestBody &&
userEditedRequestBody !== currentMediaTypeDefaultBodyValue
}
return userHasEditedBody
} else {
return null
)
userHasEditedBody =
!!userEditedRequestBody &&
userEditedRequestBody !== currentMediaTypeDefaultBodyValue
}
}
return userHasEditedBody
})

export const requestBodyInclusionSetting = onlyOAS3((state, path, method) => {
return state.getIn(["requestData", path, method, "bodyInclusion"]) || Map()
Expand Down
6 changes: 0 additions & 6 deletions src/core/plugins/oas31/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -112,11 +111,6 @@ const OAS31Plugin = ({ fn }) => {
selectLicenseUrl: selectLicenseUrlWrapper,
},
},
oas3: {
wrapSelectors: {
hasUserEditedBody: hasUserEditedBodySelectorWrapper,
},
},
oas31: {
selectors: {
selectLicenseUrl: createOnlyOAS31Selector(createSystemSelector(selectOAS31LicenseUrl)), // prettier-ignore
Expand Down
17 changes: 0 additions & 17 deletions src/core/plugins/oas31/oas3-extensions/wrap-selectors.js

This file was deleted.

0 comments on commit 4dc83b9

Please sign in to comment.