From 62bfa597f6b30140070be864ef964722038d2bdc Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 15 Aug 2019 22:22:26 -0700 Subject: [PATCH 01/20] add `getParameterSchema` OAS helper --- src/core/components/parameter-row.jsx | 7 ++-- src/core/openapi-helpers.js | 56 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/core/openapi-helpers.js diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index 5c4fd67b59b..9e86c37afce 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -4,6 +4,7 @@ import PropTypes from "prop-types" import ImPropTypes from "react-immutable-proptypes" import win from "core/window" import { getExtensions, getCommonExtensions, numberToString, stringify } from "core/utils" +import { getParameterSchema } from "core/openapi-helpers" export default class ParameterRow extends Component { static propTypes = { @@ -40,7 +41,7 @@ export default class ParameterRow extends Component { let enumValue if(isOAS3) { - let schema = parameterWithMeta.get("schema") || Map() + let schema = getParameterSchema(parameterWithMeta, { isOAS3 }) enumValue = schema.get("enum") } else { enumValue = parameterWithMeta ? parameterWithMeta.get("enum") : undefined @@ -171,7 +172,7 @@ export default class ParameterRow extends Component { let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map() let format = param.get("format") - let schema = isOAS3 ? param.get("schema") : param + let schema = getParameterSchema(param, { isOAS3 }) let type = schema.get("type") let isFormData = inType === "formData" let isFormDataSupported = "FormData" in win @@ -285,7 +286,7 @@ export default class ParameterRow extends Component { getConfigs={ getConfigs } isExecute={ isExecute } specSelectors={ specSelectors } - schema={ param.get("schema") } + schema={ schema } example={ bodyParam }/> : null } diff --git a/src/core/openapi-helpers.js b/src/core/openapi-helpers.js new file mode 100644 index 00000000000..bfd854943ff --- /dev/null +++ b/src/core/openapi-helpers.js @@ -0,0 +1,56 @@ +/** + * @prettier + */ + +import Im from "immutable" + +const swagger2SchemaKeys = Im.Set( + "type", + "format", + "items", + "default", + "maximum", + "exclusiveMaximum", + "minimum", + "exclusiveMinimum", + "maxLength", + "minLength", + "pattern", + "maxItems", + "minItems", + "uniqueItems", + "enum", + "multipleOf" +) + +/** + * Get the effective schema value for a parameter, or an empty Immutable.Map if + * no suitable schema can be found. + * + * Supports OpenAPI 3.0 `Parameter.content` priority -- since a Parameter Object + * cannot have both `schema` and `content`, this function ignores `schema` when + * `content` is present. + * + * @param {Immutable.Map} parameter The parameter to identify a schema for + * @param {object} config + * @param {boolean} config.isOAS3 Whether the parameter is from an OpenAPI 2.0 + * or OpenAPI 3.0 definition + * @return {Immutable.Map} The desired schema + */ +export function getParameterSchema(parameter, { isOAS3 }) { + // Return empty Map if `parameter` isn't a Map + if (!Im.Map.isMap(parameter)) return Im.Map() + + if (!isOAS3) { + // Swagger 2.0 + if (parameter.get("type") === "body") { + return parameter.get("schema", Im.Map()) + } else { + return parameter.filter((v, k) => swagger2SchemaKeys.includes(k)) + } + } + + // If we've reached here, the parameter is OpenAPI 3.0 + + return parameter.get("schema", Im.Map()) +} From bc4835787bfbc157b4efe3cfb31110f2c0aeba30 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 15 Aug 2019 22:23:00 -0700 Subject: [PATCH 02/20] use `Parameter.content.[firstKey].schema` as schema value when present --- src/core/openapi-helpers.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/core/openapi-helpers.js b/src/core/openapi-helpers.js index bfd854943ff..35ca27c25b2 100644 --- a/src/core/openapi-helpers.js +++ b/src/core/openapi-helpers.js @@ -52,5 +52,16 @@ export function getParameterSchema(parameter, { isOAS3 }) { // If we've reached here, the parameter is OpenAPI 3.0 + if (parameter.get("content")) { + const parameterContentMediaTypes = parameter + .get("content", Im.Map()) + .keySeq() + + return parameter.getIn( + ["content", parameterContentMediaTypes.first(), "schema"], + Im.Map() + ) + } + return parameter.get("schema", Im.Map()) } From ff4c8d4b10aeaade420b193e34bf60ccc00031e9 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 20 Aug 2019 16:21:20 -0700 Subject: [PATCH 03/20] `newValue` -> `initialValue` --- src/core/components/parameter-row.jsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index 9e86c37afce..e9b90f05966 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -103,23 +103,22 @@ export default class ParameterRow extends Component { } if( paramWithMeta.get("in") !== "body" ) { - let newValue + let initialValue if (specSelectors.isSwagger2()) { - newValue = paramWithMeta.get("x-example") - || paramWithMeta.getIn(["default"]) + initialValue = paramWithMeta.get("x-example") || paramWithMeta.getIn(["schema", "example"]) || paramWithMeta.getIn(["schema", "default"]) } else if (specSelectors.isOAS3()) { const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey()) - newValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"]) + initialValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"]) || paramWithMeta.get("example") || paramWithMeta.getIn(["schema", "example"]) || paramWithMeta.getIn(["schema", "default"]) } - if(newValue !== undefined) { + if(initialValue !== undefined) { this.onChangeWrapper( - List.isList(newValue) ? newValue : stringify(newValue) + List.isList(initialValue) ? initialValue : stringify(initialValue) ) } } From 55198ae246df3a6802cc91459422d750c3f94550 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 20 Aug 2019 16:21:50 -0700 Subject: [PATCH 04/20] make `paramWithMeta` a const --- src/core/components/parameter-row.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index e9b90f05966..d5b44adc61b 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -96,7 +96,7 @@ export default class ParameterRow extends Component { setDefaultValue = () => { let { specSelectors, pathMethod, rawParam, oas3Selectors } = this.props - let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map() + const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map() if (!paramWithMeta || paramWithMeta.get("value") !== undefined) { return From 74aec47cdde2322d837ac22101d7c66d501fa5ad Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 20 Aug 2019 16:22:11 -0700 Subject: [PATCH 05/20] add trailing comma to `swagger2SchemaKeys` --- src/core/openapi-helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/openapi-helpers.js b/src/core/openapi-helpers.js index 35ca27c25b2..8faf9ebb276 100644 --- a/src/core/openapi-helpers.js +++ b/src/core/openapi-helpers.js @@ -20,7 +20,7 @@ const swagger2SchemaKeys = Im.Set( "minItems", "uniqueItems", "enum", - "multipleOf" + "multipleOf", ) /** From 210008931b3ed8260985caf5ffdde40d47e0144d Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 20 Aug 2019 16:33:50 -0700 Subject: [PATCH 06/20] refactor `helpers` to a folder --- src/core/components/parameter-row.jsx | 2 +- .../openapi-helpers.js => helpers/get-parameter-schema.js} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/{core/openapi-helpers.js => helpers/get-parameter-schema.js} (94%) diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index d5b44adc61b..7422b0600bf 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -4,7 +4,7 @@ import PropTypes from "prop-types" import ImPropTypes from "react-immutable-proptypes" import win from "core/window" import { getExtensions, getCommonExtensions, numberToString, stringify } from "core/utils" -import { getParameterSchema } from "core/openapi-helpers" +import getParameterSchema from "core/helpers/get-parameter-schema" export default class ParameterRow extends Component { static propTypes = { diff --git a/src/core/openapi-helpers.js b/src/helpers/get-parameter-schema.js similarity index 94% rename from src/core/openapi-helpers.js rename to src/helpers/get-parameter-schema.js index 8faf9ebb276..9226f5fc4bb 100644 --- a/src/core/openapi-helpers.js +++ b/src/helpers/get-parameter-schema.js @@ -20,7 +20,7 @@ const swagger2SchemaKeys = Im.Set( "minItems", "uniqueItems", "enum", - "multipleOf", + "multipleOf" ) /** @@ -37,7 +37,7 @@ const swagger2SchemaKeys = Im.Set( * or OpenAPI 3.0 definition * @return {Immutable.Map} The desired schema */ -export function getParameterSchema(parameter, { isOAS3 }) { +export default function getParameterSchema(parameter, { isOAS3 }) { // Return empty Map if `parameter` isn't a Map if (!Im.Map.isMap(parameter)) return Im.Map() From 8759dc5096d1a05a7c43d2d1725c59b9bb82f8a8 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 20 Aug 2019 16:36:43 -0700 Subject: [PATCH 07/20] deprecate `src/core/utils.js` in favor of `src/core/helpers/` --- src/core/utils.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/core/utils.js b/src/core/utils.js index ffbb2948a23..dda53f5a40d 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -1,3 +1,15 @@ +/* + ATTENTION! This file (but not the functions within) is deprecated. + + You should probably add a new file to `./helpers/` instead of adding a new + function here. + + One-function-per-file is a better pattern than what we have here. + + If you're refactoring something in here, feel free to break it out to a file + in `./helpers` if you have the time. +*/ + import Im from "immutable" import { sanitizeUrl as braintreeSanitizeUrl } from "@braintree/sanitize-url" import camelCase from "lodash/camelCase" From 1c417e2ad9b79216dfcb42037e2424db975aac95 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 27 Aug 2019 16:45:01 -0700 Subject: [PATCH 08/20] support `Parameter.content.[mediaType].schema` in validateParam --- src/core/utils.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/utils.js b/src/core/utils.js index dda53f5a40d..6bdefe4399a 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -21,6 +21,7 @@ import eq from "lodash/eq" import { memoizedSampleFromSchema, memoizedCreateXMLExample } from "core/plugins/samples/fn" import win from "./window" import cssEscape from "css.escape" +import getParameterSchema from "../helpers/get-parameter-schema"; const DEFAULT_RESPONSE_KEY = "default" @@ -500,7 +501,9 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec let errors = [] let required = param.get("required") - let paramDetails = isOAS3 ? param.get("schema") : param + let paramDetails = getParameterSchema(param, { isOAS3 }) + + debugger if(!paramDetails) return errors From e05f93af8a468d9df0c60eb7a9b2d035e5ce4d1f Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 27 Aug 2019 16:45:15 -0700 Subject: [PATCH 09/20] reject `null` as an OAS3 object value --- src/core/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/utils.js b/src/core/utils.js index 6bdefe4399a..640259cea04 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -533,7 +533,7 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec let oas3ObjectCheck = false if(false || isOAS3 && type === "object") { - if(typeof value === "object") { + if(typeof value === "object" && value !== null) { oas3ObjectCheck = true } else if(typeof value === "string") { try { From d95aaf6d85a548ba674bccb612a47ae8dd49b218 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 27 Aug 2019 18:06:54 -0700 Subject: [PATCH 10/20] expose Fetch errors in the browser console --- src/core/plugins/spec/actions.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/core/plugins/spec/actions.js b/src/core/plugins/spec/actions.js index a93b1ebe7df..b9814e5a0af 100644 --- a/src/core/plugins/spec/actions.js +++ b/src/core/plugins/spec/actions.js @@ -436,9 +436,12 @@ export const executeRequest = (req) => specActions.setResponse(req.pathName, req.method, res) } ) .catch( - err => specActions.setResponse(req.pathName, req.method, { - error: true, err: serializeError(err) - }) + err => { + console.error(err) + specActions.setResponse(req.pathName, req.method, { + error: true, err: serializeError(err) + }) + } ) } From 9106e395c386d55115d11a26283239269ff63a44 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 27 Aug 2019 18:07:20 -0700 Subject: [PATCH 11/20] generate ParameterRow default values based on `content` values --- src/core/components/parameter-row.jsx | 47 +++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index 7422b0600bf..ef82058066a 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -3,8 +3,8 @@ import { Map, List } from "immutable" import PropTypes from "prop-types" import ImPropTypes from "react-immutable-proptypes" import win from "core/window" -import { getExtensions, getCommonExtensions, numberToString, stringify } from "core/utils" -import getParameterSchema from "core/helpers/get-parameter-schema" +import { getSampleSchema, getExtensions, getCommonExtensions, numberToString, stringify } from "core/utils" +import getParameterSchema from "../../helpers/get-parameter-schema.js" export default class ParameterRow extends Component { static propTypes = { @@ -98,27 +98,62 @@ export default class ParameterRow extends Component { const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map() + const schema = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() }) + + const parameterMediaType = paramWithMeta + .get("content", Map()) + .keySeq() + .first() + + const generatedSampleValue = getSampleSchema(schema.toJS(), parameterMediaType, { + includeWriteOnly: true + }) + if (!paramWithMeta || paramWithMeta.get("value") !== undefined) { return } if( paramWithMeta.get("in") !== "body" ) { let initialValue + + debugger + + //// Find an initial value if (specSelectors.isSwagger2()) { initialValue = paramWithMeta.get("x-example") || paramWithMeta.getIn(["schema", "example"]) - || paramWithMeta.getIn(["schema", "default"]) + || schema.getIn(["default"]) } else if (specSelectors.isOAS3()) { const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey()) initialValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"]) + || paramWithMeta.getIn(["content", parameterMediaType, "example"]) || paramWithMeta.get("example") - || paramWithMeta.getIn(["schema", "example"]) - || paramWithMeta.getIn(["schema", "default"]) + || schema.get("example") + || schema.get("default") } + + //// Process the initial value + + if(initialValue !== undefined && !List.isList(initialValue)) { + // Stringify if it isn't a List + initialValue = stringify(initialValue) + } + + //// Dispatch the initial value + if(initialValue !== undefined) { + this.onChangeWrapper(initialValue) + } else if(schema.get("type") === "object" && generatedSampleValue) { + // Object parameters get special treatment.. if the user doesn't set any + // default or example values, we'll provide initial values generated from + // the schema. this.onChangeWrapper( - List.isList(initialValue) ? initialValue : stringify(initialValue) + List.isList(generatedSampleValue) ? ( + generatedSampleValue + ) : ( + stringify(generatedSampleValue) + ) ) } } From 9a4ff63349fb8339198827957063b2a261b3a0ed Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 27 Aug 2019 20:52:08 -0700 Subject: [PATCH 12/20] add tests for `getParameterSchema` --- src/helpers/get-parameter-schema.js | 8 +- test/core/helpers/get-parameter-schema.js | 142 ++++++++++++++++++++++ 2 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 test/core/helpers/get-parameter-schema.js diff --git a/src/helpers/get-parameter-schema.js b/src/helpers/get-parameter-schema.js index 9226f5fc4bb..05b59fa2a6f 100644 --- a/src/helpers/get-parameter-schema.js +++ b/src/helpers/get-parameter-schema.js @@ -4,7 +4,7 @@ import Im from "immutable" -const swagger2SchemaKeys = Im.Set( +const swagger2SchemaKeys = Im.Set.of( "type", "format", "items", @@ -37,13 +37,13 @@ const swagger2SchemaKeys = Im.Set( * or OpenAPI 3.0 definition * @return {Immutable.Map} The desired schema */ -export default function getParameterSchema(parameter, { isOAS3 }) { +export default function getParameterSchema(parameter, { isOAS3 } = {}) { // Return empty Map if `parameter` isn't a Map if (!Im.Map.isMap(parameter)) return Im.Map() if (!isOAS3) { // Swagger 2.0 - if (parameter.get("type") === "body") { + if (parameter.get("in") === "body") { return parameter.get("schema", Im.Map()) } else { return parameter.filter((v, k) => swagger2SchemaKeys.includes(k)) @@ -54,7 +54,7 @@ export default function getParameterSchema(parameter, { isOAS3 }) { if (parameter.get("content")) { const parameterContentMediaTypes = parameter - .get("content", Im.Map()) + .get("content", Im.Map({})) .keySeq() return parameter.getIn( diff --git a/test/core/helpers/get-parameter-schema.js b/test/core/helpers/get-parameter-schema.js new file mode 100644 index 00000000000..9775ffccaf9 --- /dev/null +++ b/test/core/helpers/get-parameter-schema.js @@ -0,0 +1,142 @@ +/** + * @prettier + */ + +import expect from "expect" +import Im, { fromJS } from "immutable" +import getParameterSchema from "../../../src/helpers/get-parameter-schema" + +describe("getParameterSchema", () => { + it("should return an empty Map when given no parameters", () => { + const result = getParameterSchema() + + expect(result).toEqual(fromJS({})) + }) + + it("should return an empty Map when given an empty Map", () => { + const result = getParameterSchema(fromJS({})) + + expect(result).toEqual(fromJS({})) + }) + + it("should return a schema for a Swagger 2.0 query parameter", () => { + const result = getParameterSchema( + fromJS({ + name: "id", + in: "query", + description: "ID of the object to fetch", + required: false, + type: "array", + items: { + type: "string", + }, + collectionFormat: "multi", + }) + ) + + expect(result.toJS()).toEqual({ + type: "array", + items: { + type: "string", + }, + }) + }) + + it("should return a schema for a Swagger 2.0 body parameter", () => { + const result = getParameterSchema( + fromJS({ + name: "user", + in: "body", + description: "user to add to the system", + required: true, + schema: { + type: "array", + items: { + type: "string", + }, + }, + }) + ) + + expect(result.toJS()).toEqual({ + type: "array", + items: { + type: "string", + }, + }) + }) + + it("should return a schema for an OpenAPI 3.0 query parameter", () => { + const result = getParameterSchema( + fromJS({ + name: "id", + in: "query", + description: "ID of the object to fetch", + required: false, + schema: { + type: "array", + items: { + type: "string", + }, + }, + style: "form", + explode: true, + }), + { + isOAS3: true, + } + ) + + expect(result.toJS()).toEqual({ + type: "array", + items: { + type: "string", + }, + }) + }) + + it("should return a schema for an OpenAPI 3.0 query parameter with `content`", () => { + const result = getParameterSchema( + fromJS({ + in: "query", + name: "coordinates", + content: { + "application/json": { + schema: { + type: "object", + required: ["lat", "long"], + properties: { + lat: { + type: "number", + }, + long: { + type: "number", + }, + }, + }, + "should-ignore/the-second-media-type": { + type: "string", + default: "this shouldn't be returned", + }, + }, + }, + }), + { + isOAS3: true, + } + ) + + expect(result.toJS()).toEqual({ + type: "object", + required: ["lat", "long"], + properties: { + lat: { + type: "number", + }, + long: { + type: "number", + }, + }, + }) + }) +}) From 4adbbd7d4d72159d33b9aa743bf624388b8ea865 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 27 Aug 2019 20:52:14 -0700 Subject: [PATCH 13/20] remove debugger statement --- src/core/utils.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/utils.js b/src/core/utils.js index 640259cea04..0d5dfb0d729 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -503,8 +503,6 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec let paramDetails = getParameterSchema(param, { isOAS3 }) - debugger - if(!paramDetails) return errors let maximum = paramDetails.get("maximum") From 07aa04b51b19fcda94455796cb2cf15afc61288b Mon Sep 17 00:00:00 2001 From: kyle Date: Tue, 27 Aug 2019 20:58:51 -0700 Subject: [PATCH 14/20] remove debugger statement --- src/core/components/parameter-row.jsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index ef82058066a..7477a3449d6 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -115,8 +115,6 @@ export default class ParameterRow extends Component { if( paramWithMeta.get("in") !== "body" ) { let initialValue - - debugger //// Find an initial value From 2bc97f8f173d1d3ad5ade14b78f64504833c9592 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 27 Aug 2019 21:26:52 -0700 Subject: [PATCH 15/20] don't apply `generatedSampleValue`s to parameters with `examples` --- src/core/components/parameter-row.jsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index 7477a3449d6..806833520dc 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -142,10 +142,16 @@ export default class ParameterRow extends Component { if(initialValue !== undefined) { this.onChangeWrapper(initialValue) - } else if(schema.get("type") === "object" && generatedSampleValue) { + } else if( + schema.get("type") === "object" + && generatedSampleValue + && !paramWithMeta.get("examples") + ) { // Object parameters get special treatment.. if the user doesn't set any // default or example values, we'll provide initial values generated from // the schema. + // However, if `examples` exist for the parameter, we won't do anything, + // so that the appropriate `examples` logic can take over. this.onChangeWrapper( List.isList(generatedSampleValue) ? ( generatedSampleValue From 327ec856c37d72f4fad972bfbc3b92dd9305a83d Mon Sep 17 00:00:00 2001 From: kyle Date: Wed, 28 Aug 2019 12:22:26 -0700 Subject: [PATCH 16/20] remove extra semi --- src/core/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/utils.js b/src/core/utils.js index 0d5dfb0d729..0e0a39f4f7b 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -21,7 +21,7 @@ import eq from "lodash/eq" import { memoizedSampleFromSchema, memoizedCreateXMLExample } from "core/plugins/samples/fn" import win from "./window" import cssEscape from "css.escape" -import getParameterSchema from "../helpers/get-parameter-schema"; +import getParameterSchema from "../helpers/get-parameter-schema" const DEFAULT_RESPONSE_KEY = "default" From 609388692baed60385f29e675bbc251cbf1bd78b Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Wed, 28 Aug 2019 18:01:36 -0700 Subject: [PATCH 17/20] disable JSON check in parameter runtime validation --- src/core/utils.js | 21 +++++++++++++-------- test/core/utils.js | 34 +++++++++++++++++----------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/core/utils.js b/src/core/utils.js index 0e0a39f4f7b..767c81af89e 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -530,18 +530,23 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec let oas3ObjectCheck = false - if(false || isOAS3 && type === "object") { + if(isOAS3 && type === "object") { if(typeof value === "object" && value !== null) { oas3ObjectCheck = true } else if(typeof value === "string") { - try { - JSON.parse(value) - oas3ObjectCheck = true - } catch(e) { - errors.push("Parameter string value must be valid JSON") - return errors - } + oas3ObjectCheck = true } + // Disabled because `validateParam` doesn't consider the MediaType of the + // `Parameter.content` hint correctly. + // } else if(typeof value === "string") { + // try { + // JSON.parse(value) + // oas3ObjectCheck = true + // } catch(e) { + // errors.push("Parameter string value must be valid JSON") + // return errors + // } + // } } const allChecks = [ diff --git a/test/core/utils.js b/test/core/utils.js index b5af087060c..fa26a38f22b 100644 --- a/test/core/utils.js +++ b/test/core/utils.js @@ -412,15 +412,15 @@ describe("utils", function() { }) assertValidateOas3Param(param, value, []) - // invalid object-as-string - param = { - required: true, - schema: { - type: "object" - } - } - value = "{{}" - assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"]) + // // invalid object-as-string + // param = { + // required: true, + // schema: { + // type: "object" + // } + // } + // value = "{{}" + // assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"]) // missing when required param = { @@ -456,14 +456,14 @@ describe("utils", function() { }) assertValidateOas3Param(param, value, []) - // invalid object-as-string - param = { - schema: { - type: "object" - } - } - value = "{{}" - assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"]) + // // invalid object-as-string + // param = { + // schema: { + // type: "object" + // } + // } + // value = "{{}" + // assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"]) // missing when not required param = { From 597405f61484f88e18c7ae48af6f734278de8f9a Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Sat, 31 Aug 2019 14:34:49 -0700 Subject: [PATCH 18/20] stringify JsonSchema_object textarea values --- src/core/json-schema-components.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/json-schema-components.jsx b/src/core/json-schema-components.jsx index e349ee722d6..bf88ebea06b 100644 --- a/src/core/json-schema-components.jsx +++ b/src/core/json-schema-components.jsx @@ -4,6 +4,7 @@ import { List, fromJS } from "immutable" import cx from "classnames" import ImPropTypes from "react-immutable-proptypes" import DebounceInput from "react-debounce-input" +import { stringify } from "core/utils" //import "less/json-schema-form" const noop = ()=> {} @@ -269,7 +270,7 @@ export class JsonSchema_object extends PureComponent {