diff --git a/package-lock.json b/package-lock.json index 6075b889f31..72667fdfe01 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21100,9 +21100,9 @@ } }, "swagger-client": { - "version": "3.9.3", - "resolved": "https://registry.npmjs.org/swagger-client/-/swagger-client-3.9.3.tgz", - "integrity": "sha512-MkSI3oi9fBdpmihgY5Eo3XFzhnE/mryYA9siIykTd1y3xtSMXTxoQjXo3hUEUjp3vtdEW/yLMexUsq7fAjrP7Q==", + "version": "3.9.4", + "resolved": "https://registry.npmjs.org/swagger-client/-/swagger-client-3.9.4.tgz", + "integrity": "sha512-Rd4BrAUQeVIFYqzg7lkJMpd1P/oMVzcXEaKKaD+hfF8x4ZTTElNClCikD2yjFvmRCx0J2eIWs908kQOPkwc63w==", "requires": { "@babel/runtime-corejs2": "^7.0.0", "@kyleshockey/object-assign-deep": "^0.4.0", @@ -21125,9 +21125,9 @@ }, "dependencies": { "buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.2.1.tgz", - "integrity": "sha512-c+Ko0loDaFfuPWiL02ls9Xd3GO3cPVmUobQ6t3rXNUk304u6hGq+8N/kFi+QEIKhzK3uwolVhLzszmfLmMLnqg==", + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.4.2.tgz", + "integrity": "sha512-iy9koArjAFCzGnx3ZvNA6Z0clIbbFgbdWQ0mKD3hO0krOrZh8UgA6qMKcZvwLJxS+D6iVR76+5/pV56yMNYTag==", "requires": { "base64-js": "^1.0.2", "ieee754": "^1.1.4" diff --git a/package.json b/package.json index 6593d799910..4d17a512487 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "remarkable": "^1.7.4", "reselect": "^2.5.4", "serialize-error": "^2.1.0", - "swagger-client": "^3.9.3", + "swagger-client": "^3.9.4", "url-parse": "^1.4.7", "xml-but-prettier": "^1.0.1", "zenscroll": "^4.0.2" diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index 5c4fd67b59b..806833520dc 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -3,7 +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 { getSampleSchema, getExtensions, getCommonExtensions, numberToString, stringify } from "core/utils" +import getParameterSchema from "../../helpers/get-parameter-schema.js" 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 @@ -95,30 +96,68 @@ 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() + + 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 newValue + let initialValue + + //// Find an initial value if (specSelectors.isSwagger2()) { - newValue = paramWithMeta.get("x-example") - || paramWithMeta.getIn(["default"]) + 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()) - newValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"]) + 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") } - if(newValue !== undefined) { + + //// 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 + && !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(newValue) ? newValue : stringify(newValue) + List.isList(generatedSampleValue) ? ( + generatedSampleValue + ) : ( + stringify(generatedSampleValue) + ) ) } } @@ -171,7 +210,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 +324,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/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 {