Skip to content

Commit ffe24d5

Browse files
MathiasSpanhoveMathias Spanhove
andauthored
fix(try-it-out): reset of oas3 requestBody should use default values (#8265)
Co-authored-by: Mathias Spanhove <[email protected]>
1 parent 0b8de2c commit ffe24d5

File tree

6 files changed

+86
-3
lines changed

6 files changed

+86
-3
lines changed

src/core/components/operation.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default class Operation extends PureComponent {
1717

1818
toggleShown: PropTypes.func.isRequired,
1919
onTryoutClick: PropTypes.func.isRequired,
20+
onResetClick: PropTypes.func.isRequired,
2021
onCancelClick: PropTypes.func.isRequired,
2122
onExecute: PropTypes.func.isRequired,
2223

@@ -48,6 +49,7 @@ export default class Operation extends PureComponent {
4849
request,
4950
toggleShown,
5051
onTryoutClick,
52+
onResetClick,
5153
onCancelClick,
5254
onExecute,
5355
fn,
@@ -152,6 +154,7 @@ export default class Operation extends PureComponent {
152154
operation={operation}
153155
onChangeKey={onChangeKey}
154156
onTryoutClick = { onTryoutClick }
157+
onResetClick = { onResetClick }
155158
onCancelClick = { onCancelClick }
156159
tryItOutEnabled = { tryItOutEnabled }
157160
allowTryItOut={allowTryItOut}

src/core/components/parameters/parameters.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class Parameters extends Component {
2525
tryItOutEnabled: PropTypes.bool,
2626
allowTryItOut: PropTypes.bool,
2727
onTryoutClick: PropTypes.func,
28+
onResetClick: PropTypes.func,
2829
onCancelClick: PropTypes.func,
2930
onChangeKey: PropTypes.array,
3031
pathMethod: PropTypes.array.isRequired,
@@ -73,7 +74,7 @@ export default class Parameters extends Component {
7374
})
7475
}
7576
}
76-
77+
7778
onChangeMediaType = ({ value, pathMethod }) => {
7879
let { specActions, oas3Selectors, oas3Actions } = this.props
7980
const userHasEditedBody = oas3Selectors.hasUserEditedBody(...pathMethod)
@@ -94,6 +95,7 @@ export default class Parameters extends Component {
9495

9596
let {
9697
onTryoutClick,
98+
onResetClick,
9799
parameters,
98100
allowTryItOut,
99101
tryItOutEnabled,
@@ -161,7 +163,7 @@ export default class Parameters extends Component {
161163
enabled={tryItOutEnabled}
162164
onCancelClick={this.props.onCancelClick}
163165
onTryoutClick={onTryoutClick}
164-
onResetClick={() => oas3Actions.setRequestBodyValue({ value: undefined, pathMethod })}/>
166+
onResetClick={() => onResetClick(pathMethod)}/>
165167
) : null}
166168
</div>
167169
{this.state.parametersVisible ? <div className="parameters-container">
@@ -220,7 +222,7 @@ export default class Parameters extends Component {
220222
onChange={(value) => {
221223
this.onChangeMediaType({ value, pathMethod })
222224
}}
223-
className="body-param-content-type"
225+
className="body-param-content-type"
224226
ariaLabel="Request content type" />
225227
</label>
226228
</div>

src/core/containers/OperationContainer.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ export default class OperationContainer extends PureComponent {
122122
this.setState({tryItOutEnabled: !this.state.tryItOutEnabled})
123123
}
124124

125+
onResetClick = (pathMethod) => {
126+
const defaultRequestBodyValue = this.props.oas3Selectors.selectDefaultRequestBodyValue(...pathMethod)
127+
this.props.oas3Actions.setRequestBodyValue({ value: defaultRequestBodyValue, pathMethod })
128+
}
129+
125130
onExecute = () => {
126131
this.setState({ executeInProgress: true })
127132
}
@@ -225,6 +230,7 @@ export default class OperationContainer extends PureComponent {
225230

226231
toggleShown={this.toggleShown}
227232
onTryoutClick={this.onTryoutClick}
233+
onResetClick={this.onResetClick}
228234
onCancelClick={this.onCancelClick}
229235
onExecute={this.onExecute}
230236
specPath={specPath}

src/core/plugins/oas3/selectors.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ export const shouldRetainRequestBodyValue = onlyOAS3((state, path, method) => {
6161
}
6262
)
6363

64+
export const selectDefaultRequestBodyValue = (state, path, method) => (system) => {
65+
const {oas3Selectors, specSelectors} = system.getSystem()
66+
const spec = specSelectors.specJson()
67+
if(isOAS3Helper(spec)) {
68+
const currentMediaType = oas3Selectors.requestContentType(path, method)
69+
if (currentMediaType) {
70+
return getDefaultRequestBodyValue(
71+
specSelectors.specResolvedSubtree(["paths", path, method, "requestBody"]),
72+
currentMediaType,
73+
oas3Selectors.activeExamplesMember(
74+
path, method,
75+
"requestBody",
76+
"requestBody",
77+
)
78+
)
79+
}
80+
}
81+
return null
82+
}
83+
6484
export const hasUserEditedBody = (state, path, method) => (system) => {
6585
const {oas3Selectors, specSelectors} = system.getSystem()
6686
const spec = specSelectors.specJson()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Example Swagger with required default body parameter
4+
version: 1.0.0
5+
paths:
6+
/pet:
7+
post:
8+
operationId: addPet
9+
requestBody:
10+
content:
11+
application/x-www-form-urlencoded:
12+
schema:
13+
$ref: '#/components/schemas/BodyParameter'
14+
required: true
15+
responses:
16+
405:
17+
description: Invalid input
18+
content: {}
19+
components:
20+
schemas:
21+
BodyParameter:
22+
required:
23+
- bodyParameter
24+
type: object
25+
properties:
26+
bodyParameter:
27+
type: string
28+
default: default
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
describe("#8217: Reset Request Body not using default values", () => {
2+
it("it reset the user edited value and executes with the default value in case of try out reset. (#6517)", () => {
3+
cy
4+
.visit("?url=/documents/bugs/8217.yaml")
5+
.get("#operations-default-addPet")
6+
.click()
7+
// Expand Try It Out
8+
.get(".try-out__btn")
9+
.click()
10+
// replace default sample with bad value
11+
.get(`.parameters[data-property-name="bodyParameter"] input`)
12+
.type("{selectall}not the default value")
13+
// Reset Try It Out
14+
.get(".try-out__btn.reset")
15+
.click()
16+
// Submit using default value
17+
.get(".btn.execute")
18+
.click()
19+
// No required validation error on body parameter
20+
.get(`.parameters[data-property-name="bodyParameter"] input`)
21+
.should("have.value", "default")
22+
.and("not.have.class", "invalid")
23+
})
24+
})

0 commit comments

Comments
 (0)