import React, { Component } from "react" import { Map } from "immutable" import PropTypes from "prop-types" import ImPropTypes from "react-immutable-proptypes" import win from "core/window" import { getExtensions, getCommonExtensions, numberToString } from "core/utils" 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, onChangeConsumes: PropTypes.func.isRequired, specSelectors: PropTypes.object.isRequired, pathMethod: PropTypes.array.isRequired, getConfigs: PropTypes.func.isRequired, specPath: ImPropTypes.list.isRequired } constructor(props, context) { super(props, context) this.setDefaultValue() } componentWillReceiveProps(props) { let { specSelectors, pathMethod, rawParam } = props let { isOAS3 } = specSelectors let parameterWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) // fallback, if the meta lookup fails parameterWithMeta = parameterWithMeta.isEmpty() ? rawParam : parameterWithMeta let enumValue if(isOAS3()) { let schema = parameterWithMeta.get("schema") || Map() enumValue = schema.get("enum") } else { enumValue = parameterWithMeta ? parameterWithMeta.get("enum") : undefined } let paramValue = parameterWithMeta ? parameterWithMeta.get("value") : undefined let value if ( paramValue !== undefined ) { value = paramValue } else if ( rawParam.get("required") && enumValue && enumValue.size ) { value = enumValue.first() } if ( value !== undefined && value !== paramValue ) { this.onChangeWrapper(numberToString(value)) } this.setDefaultValue() } onChangeWrapper = (value, isXml = false) => { let { onChange, rawParam } = this.props return onChange(rawParam, value, isXml) } setDefaultValue = () => { let { specSelectors, pathMethod, rawParam } = this.props let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) if (paramWithMeta.get("value") !== undefined) { return } if( paramWithMeta.get("in") !== "body" ) { let newValue if (specSelectors.isSwagger2()) { newValue = paramWithMeta.get("x-example") || paramWithMeta.getIn(["default"]) || paramWithMeta.getIn(["schema", "example"]) || paramWithMeta.getIn(["schema", "default"]) } else if (specSelectors.isOAS3()) { newValue = paramWithMeta.get("example") || paramWithMeta.getIn(["schema", "example"]) || paramWithMeta.getIn(["schema", "default"]) } if(newValue !== undefined) { this.onChangeWrapper(numberToString(newValue)) } } } render() { let {param, rawParam, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath} = this.props let { isOAS3 } = specSelectors const { showExtensions, showCommonExtensions } = getConfigs() // const onChangeWrapper = (value) => onChange(param, value) const JsonSchemaForm = getComponent("JsonSchemaForm") const ParamBody = getComponent("ParamBody") let inType = param.get("in") let bodyParam = inType !== "body" ? null : const ModelExample = getComponent("modelExample") const Markdown = getComponent("Markdown") const ParameterExt = getComponent("ParameterExt") let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) let format = param.get("format") let schema = isOAS3 && isOAS3() ? param.get("schema") : param let type = schema.get("type") let isFormData = inType === "formData" let isFormDataSupported = "FormData" in win let required = param.get("required") let itemType = schema.getIn(["items", "type"]) let value = paramWithMeta ? paramWithMeta.get("value") : "" let commonExt = showCommonExtensions ? getCommonExtensions(param) : null let extensions = showExtensions ? getExtensions(param) : null let paramItems // undefined let paramEnum // undefined let paramDefaultValue // undefined let paramExample // undefined let isDisplayParamEnum = false if ( param !== undefined ) { paramItems = schema.get("items") } if (paramItems !== undefined) { paramEnum = paramItems.get("enum") paramDefaultValue = paramItems.get("default") } else { paramEnum = schema.get("enum") } if ( paramEnum !== undefined && paramEnum.size > 0) { isDisplayParamEnum = true } // Default and Example Value for readonly doc if ( param !== undefined ) { paramDefaultValue = schema.get("default") paramExample = param.get("example") if (paramExample === undefined) { paramExample = param.get("x-example") } } return (
{ param.get("name") } { !required ? null :  * }
{ type } { itemType && `[${itemType}]` } { format && (${format})}
{ isOAS3 && isOAS3() && param.get("deprecated") ? "deprecated": null }
({ param.get("in") })
{ !showCommonExtensions || !commonExt.size ? null : commonExt.map((v, key) => )} { !showExtensions || !extensions.size ? null : extensions.map((v, key) => )} { param.get("description") ? : null } { (bodyParam || !isExecute) && isDisplayParamEnum ? Available values : " + paramEnum.map(function(item) { return item }).toArray().join(", ")}/> : null } { (bodyParam || !isExecute) && paramDefaultValue !== undefined ? Default value : " + paramDefaultValue}/> : null } {(isFormData && !isFormDataSupported) &&
Error: your browser does not support FormData
} { bodyParam || !isExecute ? null : } { bodyParam && schema ? : null } ) } }