Skip to content

Commit c8480a8

Browse files
authored
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
1 parent 55fdeeb commit c8480a8

File tree

2 files changed

+91
-17
lines changed

2 files changed

+91
-17
lines changed

src/core/components/parameter-row.jsx

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,14 @@ export default class ParameterRow extends Component {
2222
constructor(props, context) {
2323
super(props, context)
2424

25-
let { specSelectors, pathMethod, param } = props
26-
let defaultValue = param.get("default")
27-
let xExampleValue = param.get("x-example")
28-
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
29-
let value = parameter ? parameter.get("value") : ""
30-
31-
if( param.get("in") !== "body" ) {
32-
if ( xExampleValue !== undefined && value === undefined && specSelectors.isSwagger2() ) {
33-
this.onChangeWrapper(xExampleValue)
34-
} else if ( defaultValue !== undefined && value === undefined ) {
35-
this.onChangeWrapper(defaultValue)
36-
}
37-
}
38-
25+
this.setDefaultValue()
3926
}
4027

4128
componentWillReceiveProps(props) {
4229
let { specSelectors, pathMethod, param } = props
4330
let { isOAS3 } = specSelectors
4431

4532
let example = param.get("example")
46-
let defaultValue = param.get("default")
4733
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
4834
let enumValue
4935

@@ -61,8 +47,6 @@ export default class ParameterRow extends Component {
6147
value = paramValue
6248
} else if ( example !== undefined ) {
6349
value = example
64-
} else if ( defaultValue !== undefined) {
65-
value = defaultValue
6650
} else if ( param.get("required") && enumValue && enumValue.size ) {
6751
value = enumValue.first()
6852
}
@@ -77,6 +61,29 @@ export default class ParameterRow extends Component {
7761
return onChange(param, value)
7862
}
7963

64+
setDefaultValue = () => {
65+
let { specSelectors, pathMethod, param } = this.props
66+
67+
if (param.get("value") !== undefined) {
68+
return
69+
}
70+
71+
let schema = specSelectors.isOAS3() ? param.get("schema", Map({})) : param
72+
73+
let defaultValue = schema.get("default")
74+
let xExampleValue = param.get("x-example") // Swagger 2 only
75+
let parameter = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
76+
let value = parameter ? parameter.get("value") : ""
77+
78+
if( param.get("in") !== "body" ) {
79+
if ( xExampleValue !== undefined && value === undefined && specSelectors.isSwagger2() ) {
80+
this.onChangeWrapper(xExampleValue)
81+
} else if ( defaultValue !== undefined && value === undefined ) {
82+
this.onChangeWrapper(defaultValue)
83+
}
84+
}
85+
}
86+
8087
render() {
8188
let {param, onChange, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath} = this.props
8289

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-env mocha */
2+
import React from "react"
3+
import { List, fromJS } from "immutable"
4+
import expect, { createSpy } from "expect"
5+
import { render } from "enzyme"
6+
import ParameterRow from "components/parameter-row"
7+
8+
describe("bug #4557: default parameter values", function(){
9+
it("should apply a Swagger 2.0 default value", function(){
10+
11+
const paramValue = fromJS({
12+
description: "a pet",
13+
type: "string",
14+
default: "MyDefaultValue"
15+
})
16+
17+
let props = {
18+
getComponent: ()=> "div",
19+
specSelectors: {
20+
security(){},
21+
parameterWithMeta(){ return paramValue },
22+
isOAS3(){ return false }
23+
},
24+
operation: {get: ()=>{}},
25+
onChange: createSpy(),
26+
param: paramValue,
27+
onChangeConsumes: () => {},
28+
pathMethod: [],
29+
getConfigs: () => { return {} },
30+
specPath: List([])
31+
}
32+
33+
render(<ParameterRow {...props}/>)
34+
35+
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue")
36+
})
37+
it("should apply an OpenAPI 3.0 default value", function(){
38+
39+
const paramValue = fromJS({
40+
description: "a pet",
41+
schema: {
42+
type: "string",
43+
default: "MyDefaultValue"
44+
}
45+
})
46+
47+
let props = {
48+
getComponent: ()=> "div",
49+
specSelectors: {
50+
security(){},
51+
parameterWithMeta(){ return paramValue },
52+
isOAS3(){ return true }
53+
},
54+
operation: {get: ()=>{}},
55+
onChange: createSpy(),
56+
param: paramValue,
57+
onChangeConsumes: () => {},
58+
pathMethod: [],
59+
getConfigs: () => { return {} },
60+
specPath: List([])
61+
}
62+
63+
render(<ParameterRow {...props}/>)
64+
65+
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue")
66+
})
67+
})

0 commit comments

Comments
 (0)