Skip to content

Commit 797929f

Browse files
authored
fix(parameter-row): rendering of default/example values of 0 (#6454)
Co-authored-by: @danxmoran
1 parent db2cca8 commit 797929f

File tree

2 files changed

+158
-10
lines changed

2 files changed

+158
-10
lines changed

src/core/components/parameter-row.jsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,26 @@ export default class ParameterRow extends Component {
119119
//// Find an initial value
120120

121121
if (specSelectors.isSwagger2()) {
122-
initialValue = paramWithMeta.get("x-example")
123-
|| paramWithMeta.getIn(["schema", "example"])
124-
|| (schema && schema.getIn(["default"]))
122+
initialValue =
123+
paramWithMeta.get("x-example") !== undefined
124+
? paramWithMeta.get("x-example")
125+
: paramWithMeta.getIn(["schema", "example"]) !== undefined
126+
? paramWithMeta.getIn(["schema", "example"])
127+
: (schema && schema.getIn(["default"]))
125128
} else if (specSelectors.isOAS3()) {
126129
const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
127-
initialValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"])
128-
|| paramWithMeta.getIn(["content", parameterMediaType, "example"])
129-
|| paramWithMeta.get("example")
130-
|| (schema && schema.get("example"))
131-
|| (schema && schema.get("default"))
132-
|| paramWithMeta.get("default") // ensures support for `parameterMacro`
130+
initialValue =
131+
paramWithMeta.getIn(["examples", currentExampleKey, "value"]) !== undefined
132+
? paramWithMeta.getIn(["examples", currentExampleKey, "value"])
133+
: paramWithMeta.getIn(["content", parameterMediaType, "example"]) !== undefined
134+
? paramWithMeta.getIn(["content", parameterMediaType, "example"])
135+
: paramWithMeta.get("example") !== undefined
136+
? paramWithMeta.get("example")
137+
: (schema && schema.get("example")) !== undefined
138+
? (schema && schema.get("example"))
139+
: (schema && schema.get("default")) !== undefined
140+
? (schema && schema.get("default"))
141+
: paramWithMeta.get("default") // ensures support for `parameterMacro`
133142
}
134143

135144
//// Process the initial value

test/unit/components/parameter-row.jsx

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import { fromJS } from "immutable"
2+
import { List, fromJS } from "immutable"
33
import { render } from "enzyme"
44
import ParameterRow from "components/parameter-row"
55

@@ -84,3 +84,142 @@ describe("<ParameterRow/>", () => {
8484
expect(wrapper.find(".parameter__type").text()).toEqual("string")
8585
})
8686
})
87+
88+
describe("bug #5573: zero default and example values", function () {
89+
it("should apply a Swagger 2.0 default value of zero", function () {
90+
const paramValue = fromJS({
91+
description: "a pet",
92+
type: "integer",
93+
default: 0
94+
})
95+
96+
let props = {
97+
getComponent: () => "div",
98+
specSelectors: {
99+
security() { },
100+
parameterWithMetaByIdentity() { return paramValue },
101+
isOAS3() { return false },
102+
isSwagger2() { return true }
103+
},
104+
fn: {},
105+
operation: { get: () => { } },
106+
onChange: jest.fn(),
107+
param: paramValue,
108+
rawParam: paramValue,
109+
onChangeConsumes: () => { },
110+
pathMethod: [],
111+
getConfigs: () => { return {} },
112+
specPath: List([])
113+
}
114+
115+
render(<ParameterRow {...props} />)
116+
117+
expect(props.onChange).toHaveBeenCalled()
118+
expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false)
119+
})
120+
it("should apply a Swagger 2.0 example value of zero", function () {
121+
const paramValue = fromJS({
122+
description: "a pet",
123+
type: "integer",
124+
schema: {
125+
example: 0
126+
}
127+
})
128+
129+
let props = {
130+
getComponent: () => "div",
131+
specSelectors: {
132+
security() { },
133+
parameterWithMetaByIdentity() { return paramValue },
134+
isOAS3() { return false },
135+
isSwagger2() { return true }
136+
},
137+
fn: {},
138+
operation: { get: () => { } },
139+
onChange: jest.fn(),
140+
param: paramValue,
141+
rawParam: paramValue,
142+
onChangeConsumes: () => { },
143+
pathMethod: [],
144+
getConfigs: () => { return {} },
145+
specPath: List([])
146+
}
147+
148+
render(<ParameterRow {...props} />)
149+
150+
expect(props.onChange).toHaveBeenCalled()
151+
expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false)
152+
})
153+
it("should apply an OpenAPI 3.0 default value of zero", function () {
154+
const paramValue = fromJS({
155+
description: "a pet",
156+
schema: {
157+
type: "integer",
158+
default: 0
159+
}
160+
})
161+
162+
let props = {
163+
getComponent: () => "div",
164+
specSelectors: {
165+
security() { },
166+
parameterWithMetaByIdentity() { return paramValue },
167+
isOAS3() { return true },
168+
isSwagger2() { return false }
169+
},
170+
oas3Selectors: {
171+
activeExamplesMember: () => null
172+
},
173+
fn: {},
174+
operation: { get: () => { } },
175+
onChange: jest.fn(),
176+
param: paramValue,
177+
rawParam: paramValue,
178+
onChangeConsumes: () => { },
179+
pathMethod: [],
180+
getConfigs: () => { return {} },
181+
specPath: List([])
182+
}
183+
184+
render(<ParameterRow {...props} />)
185+
186+
expect(props.onChange).toHaveBeenCalled()
187+
expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false)
188+
})
189+
it("should apply an OpenAPI 3.0 example value of zero", function () {
190+
const paramValue = fromJS({
191+
description: "a pet",
192+
schema: {
193+
type: "integer",
194+
example: 0
195+
}
196+
})
197+
198+
let props = {
199+
getComponent: () => "div",
200+
specSelectors: {
201+
security() { },
202+
parameterWithMetaByIdentity() { return paramValue },
203+
isOAS3() { return true },
204+
isSwagger2() { return false }
205+
},
206+
oas3Selectors: {
207+
activeExamplesMember: () => null
208+
},
209+
fn: {},
210+
operation: { get: () => { } },
211+
onChange: jest.fn(),
212+
param: paramValue,
213+
rawParam: paramValue,
214+
onChangeConsumes: () => { },
215+
pathMethod: [],
216+
getConfigs: () => { return {} },
217+
specPath: List([])
218+
}
219+
220+
render(<ParameterRow {...props} />)
221+
222+
expect(props.onChange).toHaveBeenCalled()
223+
expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false)
224+
})
225+
})

0 commit comments

Comments
 (0)