Improvement: Hash-keyed Try-It-Out parameter value storage (#4670)
* allow param update by identity + hashed value storage * add specActions.changeParamByIdentity * add identity-based lookup support in spec selectors * migrate `changeParam` usage to `changeParamByIdentity` * migrate usage of `parameterWithMeta` to `parameterWithMetaByIdentity` * update invocations of `changeParamByIdentity` to match fn signature * use OrderedMap throughout hash-based selectors for consistency * normalize usage of ParameterRow `onChange` * migrate bug 4557 tests to reflect new ParameterRow interface * remove exclusive test blocks * linter fixes * copy Parameters changes into OAS3 wrapper * use rawParam for meta lookups in ParameterRow
This commit is contained in:
@@ -46,11 +46,10 @@ export default class ParamBody extends PureComponent {
|
||||
}
|
||||
|
||||
updateValues = (props) => {
|
||||
let { specSelectors, pathMethod, param, isExecute, consumesValue="" } = props
|
||||
let parameter = (specSelectors ? specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in")) : fromJS({})) || param
|
||||
let { param, isExecute, consumesValue="" } = props
|
||||
let isXml = /xml/i.test(consumesValue)
|
||||
let isJson = /json/i.test(consumesValue)
|
||||
let paramValue = isXml ? parameter.get("value_xml") : parameter.get("value")
|
||||
let paramValue = isXml ? param.get("value_xml") : param.get("value")
|
||||
|
||||
if ( paramValue !== undefined ) {
|
||||
let val = !paramValue && isJson ? "{}" : paramValue
|
||||
@@ -79,7 +78,7 @@ export default class ParamBody extends PureComponent {
|
||||
this._onChange(value, isXml)
|
||||
}
|
||||
|
||||
_onChange = (val, isXml) => { (this.props.onChange || NOOP)(this.props.param, val, isXml) }
|
||||
_onChange = (val, isXml) => { (this.props.onChange || NOOP)(val, isXml) }
|
||||
|
||||
handleOnChange = e => {
|
||||
const {consumesValue} = this.props
|
||||
@@ -107,7 +106,7 @@ export default class ParamBody extends PureComponent {
|
||||
const HighlightCode = getComponent("highlightCode")
|
||||
const ContentType = getComponent("contentType")
|
||||
// for domains where specSelectors not passed
|
||||
let parameter = specSelectors ? specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in")) : param
|
||||
let parameter = specSelectors ? specSelectors.parameterWithMetaByIdentity(pathMethod, param) : param
|
||||
let errors = parameter.get("errors", List())
|
||||
let consumesValue = specSelectors.contentTypeValues(pathMethod).get("requestContentType")
|
||||
let consumes = this.props.consumes && this.props.consumes.size ? this.props.consumes : ParamBody.defaultProp.consumes
|
||||
|
||||
@@ -9,6 +9,7 @@ export default class ParameterRow extends Component {
|
||||
static propTypes = {
|
||||
onChange: PropTypes.func.isRequired,
|
||||
param: PropTypes.object.isRequired,
|
||||
rawParam: PropTypes.object.isRequired,
|
||||
getComponent: PropTypes.func.isRequired,
|
||||
fn: PropTypes.object.isRequired,
|
||||
isExecute: PropTypes.bool,
|
||||
@@ -30,7 +31,7 @@ export default class ParameterRow extends Component {
|
||||
let { isOAS3 } = specSelectors
|
||||
|
||||
let example = param.get("example")
|
||||
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in")) || param
|
||||
let parameter = specSelectors.parameterWithMetaByIdentity(pathMethod, param) || param
|
||||
let enumValue
|
||||
|
||||
if(isOAS3()) {
|
||||
@@ -56,9 +57,9 @@ export default class ParameterRow extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
onChangeWrapper = (value) => {
|
||||
let { onChange, param } = this.props
|
||||
return onChange(param, value)
|
||||
onChangeWrapper = (value, isXml = false) => {
|
||||
let { onChange, rawParam } = this.props
|
||||
return onChange(rawParam, value, isXml)
|
||||
}
|
||||
|
||||
setDefaultValue = () => {
|
||||
@@ -72,7 +73,7 @@ export default class ParameterRow extends Component {
|
||||
|
||||
let defaultValue = schema.get("default")
|
||||
let xExampleValue = param.get("x-example") // Swagger 2 only
|
||||
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
|
||||
let parameter = specSelectors.parameterWithMetaByIdentity(pathMethod, param)
|
||||
let value = parameter ? parameter.get("value") : ""
|
||||
|
||||
if( param.get("in") !== "body" ) {
|
||||
@@ -85,7 +86,7 @@ export default class ParameterRow extends Component {
|
||||
}
|
||||
|
||||
render() {
|
||||
let {param, onChange, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath} = this.props
|
||||
let {param, rawParam, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath} = this.props
|
||||
|
||||
let { isOAS3 } = specSelectors
|
||||
|
||||
@@ -101,7 +102,7 @@ export default class ParameterRow extends Component {
|
||||
param={param}
|
||||
consumes={ specSelectors.operationConsumes(pathMethod) }
|
||||
consumesValue={ specSelectors.contentTypeValues(pathMethod).get("requestContentType") }
|
||||
onChange={onChange}
|
||||
onChange={this.onChangeWrapper}
|
||||
onChangeConsumes={onChangeConsumes}
|
||||
isExecute={ isExecute }
|
||||
specSelectors={ specSelectors }
|
||||
@@ -112,7 +113,7 @@ export default class ParameterRow extends Component {
|
||||
const Markdown = getComponent("Markdown")
|
||||
const ParameterExt = getComponent("ParameterExt")
|
||||
|
||||
let paramWithMeta = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
|
||||
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam)
|
||||
let format = param.get("format")
|
||||
let schema = isOAS3 && isOAS3() ? param.get("schema") : param
|
||||
let type = schema.get("type")
|
||||
|
||||
@@ -36,11 +36,11 @@ export default class Parameters extends Component {
|
||||
|
||||
onChange = ( param, value, isXml ) => {
|
||||
let {
|
||||
specActions: { changeParam },
|
||||
specActions: { changeParamByIdentity },
|
||||
onChangeKey,
|
||||
} = this.props
|
||||
|
||||
changeParam( onChangeKey, param.get("name"), param.get("in"), value, isXml)
|
||||
changeParamByIdentity(onChangeKey, param, value, isXml)
|
||||
}
|
||||
|
||||
onChangeConsumesWrapper = ( val ) => {
|
||||
@@ -101,7 +101,8 @@ export default class Parameters extends Component {
|
||||
specPath={specPath.push(i.toString())}
|
||||
getComponent={ getComponent }
|
||||
getConfigs={ getConfigs }
|
||||
param={ specSelectors.parameterWithMeta(pathMethod, parameter.get("name"), parameter.get("in")) }
|
||||
rawParam={ parameter }
|
||||
param={ specSelectors.parameterWithMetaByIdentity(pathMethod, parameter) }
|
||||
key={ `${parameter.get( "in" )}.${parameter.get("name")}` }
|
||||
onChange={ this.onChange }
|
||||
onChangeConsumes={this.onChangeConsumesWrapper}
|
||||
|
||||
@@ -47,11 +47,11 @@ class Parameters extends Component {
|
||||
|
||||
onChange = ( param, value, isXml ) => {
|
||||
let {
|
||||
specActions: { changeParam },
|
||||
specActions: { changeParamByIdentity },
|
||||
onChangeKey,
|
||||
} = this.props
|
||||
|
||||
changeParam( onChangeKey, param.get("name"), param.get("in"), value, isXml)
|
||||
changeParamByIdentity( onChangeKey, param, value, isXml)
|
||||
}
|
||||
|
||||
onChangeConsumesWrapper = ( val ) => {
|
||||
@@ -145,7 +145,8 @@ class Parameters extends Component {
|
||||
getComponent={ getComponent }
|
||||
specPath={specPath.push(i)}
|
||||
getConfigs={ getConfigs }
|
||||
param={ parameter }
|
||||
rawParam={ parameter }
|
||||
param={ specSelectors.parameterWithMetaByIdentity(pathMethod, parameter) }
|
||||
key={ parameter.get( "name" ) }
|
||||
onChange={ this.onChange }
|
||||
onChangeConsumes={this.onChangeConsumesWrapper}
|
||||
|
||||
@@ -234,6 +234,13 @@ export function changeParam( path, paramName, paramIn, value, isXml ){
|
||||
}
|
||||
}
|
||||
|
||||
export function changeParamByIdentity( pathMethod, param, value, isXml ){
|
||||
return {
|
||||
type: UPDATE_PARAM,
|
||||
payload:{ path: pathMethod, param, value, isXml }
|
||||
}
|
||||
}
|
||||
|
||||
export const updateResolvedSubtree = (path, value) => {
|
||||
return {
|
||||
type: UPDATE_RESOLVED_SUBTREE,
|
||||
|
||||
@@ -51,12 +51,21 @@ export default {
|
||||
},
|
||||
|
||||
[UPDATE_PARAM]: ( state, {payload} ) => {
|
||||
let { path: pathMethod, paramName, paramIn, value, isXml } = payload
|
||||
let { path: pathMethod, paramName, paramIn, param, value, isXml } = payload
|
||||
|
||||
let paramKey
|
||||
|
||||
// `hashCode` is an Immutable.js Map method
|
||||
if(param && param.hashCode && !paramIn && !paramName) {
|
||||
paramKey = `${param.get("name")}.${param.get("in")}.hash-${param.hashCode()}`
|
||||
} else {
|
||||
paramKey = `${paramName}.${paramIn}`
|
||||
}
|
||||
|
||||
const valueKey = isXml ? "value_xml" : "value"
|
||||
|
||||
return state.setIn(
|
||||
["meta", "paths", ...pathMethod, "parameters", `${paramName}.${paramIn}`, valueKey],
|
||||
["meta", "paths", ...pathMethod, "parameters", paramKey, valueKey],
|
||||
value
|
||||
)
|
||||
},
|
||||
|
||||
@@ -294,34 +294,42 @@ export const allowTryItOutFor = () => {
|
||||
return true
|
||||
}
|
||||
|
||||
export const operationWithMeta = (state, path, method) => {
|
||||
const op = specJsonWithResolvedSubtrees(state).getIn(["paths", path, method], Map())
|
||||
const meta = state.getIn(["meta", "paths", path, method], Map())
|
||||
export const parameterWithMetaByIdentity = (state, pathMethod, param) => {
|
||||
const opParams = specJsonWithResolvedSubtrees(state).getIn(["paths", ...pathMethod, "parameters"], OrderedMap())
|
||||
const metaParams = state.getIn(["meta", "paths", ...pathMethod, "parameters"], OrderedMap())
|
||||
|
||||
const mergedParams = op.get("parameters", List()).map((param) => {
|
||||
return Map().merge(
|
||||
param,
|
||||
meta.getIn(["parameters", `${param.get("name")}.${param.get("in")}`])
|
||||
const mergedParams = opParams.map((currentParam) => {
|
||||
const nameInKeyedMeta = metaParams.get(`${param.get("name")}.${param.get("in")}`)
|
||||
const hashKeyedMeta = metaParams.get(`${param.get("name")}.${param.get("in")}.hash-${param.hashCode()}`)
|
||||
return OrderedMap().merge(
|
||||
currentParam,
|
||||
nameInKeyedMeta,
|
||||
hashKeyedMeta
|
||||
)
|
||||
})
|
||||
|
||||
return Map()
|
||||
.merge(op, meta)
|
||||
.set("parameters", mergedParams)
|
||||
return mergedParams.find(curr => curr.get("in") === param.get("in") && curr.get("name") === param.get("name"), OrderedMap())
|
||||
}
|
||||
|
||||
export const parameterWithMeta = (state, pathMethod, paramName, paramIn) => {
|
||||
const opParams = specJsonWithResolvedSubtrees(state).getIn(["paths", ...pathMethod, "parameters"], Map())
|
||||
const metaParams = state.getIn(["meta", "paths", ...pathMethod, "parameters"], Map())
|
||||
|
||||
const mergedParams = opParams.map((param) => {
|
||||
return Map().merge(
|
||||
param,
|
||||
metaParams.get(`${param.get("name")}.${param.get("in")}`)
|
||||
)
|
||||
export const parameterWithMeta = (state, pathMethod, paramName, paramIn) => {
|
||||
const opParams = specJsonWithResolvedSubtrees(state).getIn(["paths", ...pathMethod, "parameters"], OrderedMap())
|
||||
const currentParam = opParams.find(param => param.get("in") === paramIn && param.get("name") === paramName, OrderedMap())
|
||||
|
||||
return parameterWithMetaByIdentity(state, pathMethod, currentParam)
|
||||
}
|
||||
|
||||
export const operationWithMeta = (state, path, method) => {
|
||||
const op = specJsonWithResolvedSubtrees(state).getIn(["paths", path, method], OrderedMap())
|
||||
const meta = state.getIn(["meta", "paths", path, method], OrderedMap())
|
||||
|
||||
const mergedParams = op.get("parameters", List()).map((param) => {
|
||||
return parameterWithMetaByIdentity(state, [path, method], param)
|
||||
})
|
||||
|
||||
return mergedParams.find(param => param.get("in") === paramIn && param.get("name") === paramName, Map())
|
||||
return OrderedMap()
|
||||
.merge(op, meta)
|
||||
.set("parameters", mergedParams)
|
||||
}
|
||||
|
||||
// Get the parameter value by parameter name
|
||||
|
||||
Reference in New Issue
Block a user