Skip to content

Commit

Permalink
fix(oas3): expand Callback operation without browser error (#8510)
Browse files Browse the repository at this point in the history
Refs #7465
  • Loading branch information
char0n committed Mar 23, 2023
1 parent b0ab561 commit cb15dbb
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions src/core/plugins/oas3/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import { stringify } from "../../utils"

// Helpers

function onlyOAS3(selector) {
return (...args) => (system) => {
const onlyOAS3 =
(selector) =>
(state, ...args) =>
(system) => {
const spec = system.getSystem().specSelectors.specJson()
if(isOAS3Helper(spec)) {
return selector(...args)

if (isOAS3Helper(spec)) {
const selectedValue = selector(state, ...args)
return typeof selectedValue === "function"
? selectedValue(system)
: selectedValue
} else {
return null
}
}
}


function validateRequestBodyIsRequired(selector) {
return (...args) => (system) => {
Expand Down Expand Up @@ -81,37 +87,50 @@ export const selectDefaultRequestBodyValue = (state, path, method) => (system) =
return null
}

export const hasUserEditedBody = (state, path, method) => (system) => {
export const hasUserEditedBody = onlyOAS3((state, path, method) => (system) => {
const {oas3Selectors, specSelectors} = system.getSystem()
const spec = specSelectors.specJson()
if(isOAS3Helper(spec)) {
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",
)

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

0 comments on commit cb15dbb

Please sign in to comment.