diff --git a/src/core/plugins/oas3/components/callbacks.jsx b/src/core/plugins/oas3/components/callbacks.jsx index 9a9eeeb4eb5..8e96a0dce65 100644 --- a/src/core/plugins/oas3/components/callbacks.jsx +++ b/src/core/plugins/oas3/components/callbacks.jsx @@ -1,54 +1,49 @@ +/** + * @prettier + */ import React from "react" import PropTypes from "prop-types" import ImPropTypes from "react-immutable-proptypes" -import { fromJS } from "immutable" -const Callbacks = (props) => { - let { callbacks, getComponent, specPath } = props - // const Markdown = getComponent("Markdown", true) +const Callbacks = ({ callbacks, specPath, specSelectors, getComponent }) => { + const operationDTOs = specSelectors.callbacksOperations({ + callbacks, + specPath, + }) + const callbackNames = Object.keys(operationDTOs) + const OperationContainer = getComponent("OperationContainer", true) - if(!callbacks) { - return No callbacks - } + if (callbackNames.length === 0) return No callbacks - let callbackElements = callbacks.entrySeq().map(([callbackName, callback]) => { - return
-

{callbackName}

- { callback.entrySeq().map(([pathItemName, pathItem]) => { - if(pathItemName === "$$ref") { - return null - } - return
- { pathItem.entrySeq().map(([method, operation]) => { - if(method === "$$ref") { - return null - } - let op = fromJS({ - operation - }) - return + {callbackNames.map((callbackName) => ( +
+

{callbackName}

+ + {operationDTOs[callbackName].map((operationDTO) => ( + - }) } + /> + ))}
- }) } + ))}
- }) - return
- {callbackElements} -
+ ) } Callbacks.propTypes = { getComponent: PropTypes.func.isRequired, + specSelectors: PropTypes.shape({ + callbacksOperations: PropTypes.func.isRequired, + }).isRequired, callbacks: ImPropTypes.iterable.isRequired, specPath: ImPropTypes.list.isRequired, } diff --git a/src/core/plugins/oas3/spec-extensions/selectors.js b/src/core/plugins/oas3/spec-extensions/selectors.js index 3a0ce81ae6b..67c6b2f39b6 100644 --- a/src/core/plugins/oas3/spec-extensions/selectors.js +++ b/src/core/plugins/oas3/spec-extensions/selectors.js @@ -1,6 +1,12 @@ -import { Map } from "immutable" -import { isSwagger2 as isSwagger2Helper, isOAS30 as isOAS30Helper } from "../helpers" +/** + * @prettier + */ +import { List, Map } from "immutable" +import { + isSwagger2 as isSwagger2Helper, + isOAS30 as isOAS30Helper, +} from "../helpers" /** * Helpers @@ -23,18 +29,54 @@ export const isOAS3 = () => (system) => { } function onlyOAS3(selector) { - return () => (system, ...args) => { - const spec = system.getSystem().specSelectors.specJson() - if(system.specSelectors.isOAS3(spec)) { - const result = selector(...args) - return typeof result === "function" ? result(system, ...args) : result - } else { - return null + return (state, ...args) => + (system) => { + if (system.specSelectors.isOAS3()) { + const selectedValue = selector(state, ...args) + return typeof selectedValue === "function" + ? selectedValue(system) + : selectedValue + } else { + return null + } } - } } export const servers = onlyOAS3(() => (system) => { const spec = system.specSelectors.specJson() return spec.get("servers", map) }) + +export const callbacksOperations = onlyOAS3( + (state, { callbacks, specPath }) => + (system) => { + const validOperationMethods = system.specSelectors.validOperationMethods() + + if (!Map.isMap(callbacks)) return {} + + return callbacks + .reduce((allOperations, callback, callbackName) => { + if (!Map.isMap(callback)) return allOperations + + return callback.reduce((callbackOperations, pathItem, expression) => { + if (!Map.isMap(pathItem)) return callbackOperations + + const pathItemOperations = pathItem + .entrySeq() + .filter(([key]) => validOperationMethods.includes(key)) + .map(([method, operation]) => ({ + operation: Map({ operation }), + method, + path: expression, + callbackName, + specPath: specPath.concat([callbackName, expression, method]), + })) + + return callbackOperations.concat(pathItemOperations) + }, List()) + }, List()) + .groupBy((operationDTO) => operationDTO.callbackName) + .map((operations) => operations.toArray()) + .toObject() + } +) diff --git a/src/core/plugins/oas31/spec-extensions/selectors.js b/src/core/plugins/oas31/spec-extensions/selectors.js index 401967a73ec..226f4bfea82 100644 --- a/src/core/plugins/oas31/spec-extensions/selectors.js +++ b/src/core/plugins/oas31/spec-extensions/selectors.js @@ -27,9 +27,13 @@ export const selectWebhooksOperations = createSelector( (state, system) => system.specSelectors.webhooks(), (state, system) => system.specSelectors.validOperationMethods(), (state, system) => system.specSelectors.specResolvedSubtree(["webhooks"]), - (webhooks, validOperationMethods) => - webhooks + (webhooks, validOperationMethods) => { + if (!Map.isMap(webhooks)) return {} + + return webhooks .reduce((allOperations, pathItem, pathItemName) => { + if (!Map.isMap(pathItem)) return allOperations + const pathItemOperations = pathItem .entrySeq() .filter(([key]) => validOperationMethods.includes(key)) @@ -42,9 +46,10 @@ export const selectWebhooksOperations = createSelector( return allOperations.concat(pathItemOperations) }, List()) - .groupBy((operation) => operation.path) + .groupBy((operationDTO) => operationDTO.path) .map((operations) => operations.toArray()) .toObject() + } ) export const license = () => (system) => {