fix: respect OAS3 parameter default values (#4561)
* add test cases * refactor default setters into own function * reach into `schema` for default value in OAS3 * remove exclusive test
This commit is contained in:
@@ -22,20 +22,7 @@ export default class ParameterRow extends Component {
|
|||||||
constructor(props, context) {
|
constructor(props, context) {
|
||||||
super(props, context)
|
super(props, context)
|
||||||
|
|
||||||
let { specSelectors, pathMethod, param } = props
|
this.setDefaultValue()
|
||||||
let defaultValue = param.get("default")
|
|
||||||
let xExampleValue = param.get("x-example")
|
|
||||||
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
|
|
||||||
let value = parameter ? parameter.get("value") : ""
|
|
||||||
|
|
||||||
if( param.get("in") !== "body" ) {
|
|
||||||
if ( xExampleValue !== undefined && value === undefined && specSelectors.isSwagger2() ) {
|
|
||||||
this.onChangeWrapper(xExampleValue)
|
|
||||||
} else if ( defaultValue !== undefined && value === undefined ) {
|
|
||||||
this.onChangeWrapper(defaultValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(props) {
|
componentWillReceiveProps(props) {
|
||||||
@@ -43,7 +30,6 @@ export default class ParameterRow extends Component {
|
|||||||
let { isOAS3 } = specSelectors
|
let { isOAS3 } = specSelectors
|
||||||
|
|
||||||
let example = param.get("example")
|
let example = param.get("example")
|
||||||
let defaultValue = param.get("default")
|
|
||||||
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
|
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
|
||||||
let enumValue
|
let enumValue
|
||||||
|
|
||||||
@@ -61,8 +47,6 @@ export default class ParameterRow extends Component {
|
|||||||
value = paramValue
|
value = paramValue
|
||||||
} else if ( example !== undefined ) {
|
} else if ( example !== undefined ) {
|
||||||
value = example
|
value = example
|
||||||
} else if ( defaultValue !== undefined) {
|
|
||||||
value = defaultValue
|
|
||||||
} else if ( param.get("required") && enumValue && enumValue.size ) {
|
} else if ( param.get("required") && enumValue && enumValue.size ) {
|
||||||
value = enumValue.first()
|
value = enumValue.first()
|
||||||
}
|
}
|
||||||
@@ -77,6 +61,29 @@ export default class ParameterRow extends Component {
|
|||||||
return onChange(param, value)
|
return onChange(param, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setDefaultValue = () => {
|
||||||
|
let { specSelectors, pathMethod, param } = this.props
|
||||||
|
|
||||||
|
if (param.get("value") !== undefined) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let schema = specSelectors.isOAS3() ? param.get("schema", Map({})) : param
|
||||||
|
|
||||||
|
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 value = parameter ? parameter.get("value") : ""
|
||||||
|
|
||||||
|
if( param.get("in") !== "body" ) {
|
||||||
|
if ( xExampleValue !== undefined && value === undefined && specSelectors.isSwagger2() ) {
|
||||||
|
this.onChangeWrapper(xExampleValue)
|
||||||
|
} else if ( defaultValue !== undefined && value === undefined ) {
|
||||||
|
this.onChangeWrapper(defaultValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let {param, onChange, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath} = this.props
|
let {param, onChange, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath} = this.props
|
||||||
|
|
||||||
|
|||||||
67
test/bugs/4557-default-parameter-values.js
Normal file
67
test/bugs/4557-default-parameter-values.js
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
import React from "react"
|
||||||
|
import { List, fromJS } from "immutable"
|
||||||
|
import expect, { createSpy } from "expect"
|
||||||
|
import { render } from "enzyme"
|
||||||
|
import ParameterRow from "components/parameter-row"
|
||||||
|
|
||||||
|
describe("bug #4557: default parameter values", function(){
|
||||||
|
it("should apply a Swagger 2.0 default value", function(){
|
||||||
|
|
||||||
|
const paramValue = fromJS({
|
||||||
|
description: "a pet",
|
||||||
|
type: "string",
|
||||||
|
default: "MyDefaultValue"
|
||||||
|
})
|
||||||
|
|
||||||
|
let props = {
|
||||||
|
getComponent: ()=> "div",
|
||||||
|
specSelectors: {
|
||||||
|
security(){},
|
||||||
|
parameterWithMeta(){ return paramValue },
|
||||||
|
isOAS3(){ return false }
|
||||||
|
},
|
||||||
|
operation: {get: ()=>{}},
|
||||||
|
onChange: createSpy(),
|
||||||
|
param: paramValue,
|
||||||
|
onChangeConsumes: () => {},
|
||||||
|
pathMethod: [],
|
||||||
|
getConfigs: () => { return {} },
|
||||||
|
specPath: List([])
|
||||||
|
}
|
||||||
|
|
||||||
|
render(<ParameterRow {...props}/>)
|
||||||
|
|
||||||
|
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue")
|
||||||
|
})
|
||||||
|
it("should apply an OpenAPI 3.0 default value", function(){
|
||||||
|
|
||||||
|
const paramValue = fromJS({
|
||||||
|
description: "a pet",
|
||||||
|
schema: {
|
||||||
|
type: "string",
|
||||||
|
default: "MyDefaultValue"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
let props = {
|
||||||
|
getComponent: ()=> "div",
|
||||||
|
specSelectors: {
|
||||||
|
security(){},
|
||||||
|
parameterWithMeta(){ return paramValue },
|
||||||
|
isOAS3(){ return true }
|
||||||
|
},
|
||||||
|
operation: {get: ()=>{}},
|
||||||
|
onChange: createSpy(),
|
||||||
|
param: paramValue,
|
||||||
|
onChangeConsumes: () => {},
|
||||||
|
pathMethod: [],
|
||||||
|
getConfigs: () => { return {} },
|
||||||
|
specPath: List([])
|
||||||
|
}
|
||||||
|
|
||||||
|
render(<ParameterRow {...props}/>)
|
||||||
|
|
||||||
|
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue")
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user