diff --git a/src/core/plugins/spec/actions.js b/src/core/plugins/spec/actions.js index 1f43ca99..7465574c 100644 --- a/src/core/plugins/spec/actions.js +++ b/src/core/plugins/spec/actions.js @@ -1,6 +1,7 @@ import YAML from "js-yaml" import parseUrl from "url-parse" import serializeError from "serialize-error" +import { isJSONObject } from "core/utils" // Actions conform to FSA (flux-standard-actions) // {type: string,payload: Any|Error, meta: obj, error: bool} @@ -216,8 +217,14 @@ export const executeRequest = (req) => // OAS3 request feature support req.server = oas3Selectors.selectedServer() req.serverVariables = oas3Selectors.serverVariables(req.server).toJS() - req.requestBody = oas3Selectors.requestBodyValue(pathName, method) req.requestContentType = oas3Selectors.requestContentType(pathName, method) + const requestBody = oas3Selectors.requestBodyValue(pathName, method) + + if(isJSONObject(requestBody)) { + req.requestBody = JSON.parse(requestBody) + } else { + req.requestBody = requestBody + } } let parsedRequest = Object.assign({}, req) diff --git a/src/core/utils.js b/src/core/utils.js index 9a04329e..743c4749 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -13,6 +13,25 @@ const DEFAULT_REPONSE_KEY = "default" export const isImmutable = (maybe) => Im.Iterable.isIterable(maybe) +export function isJSONObject (str) { + try { + var o = JSON.parse(str) + + // Handle non-exception-throwing cases: + // Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking, + // but... JSON.parse(null) returns null, and typeof null === "object", + // so we must check for that, too. Thankfully, null is falsey, so this suffices: + if (o && typeof o === "object") { + return o + } + } + catch (e) { + // do nothing + } + + return false +} + export function objectify (thing) { if(!isObject(thing)) return {}