From 464818e2abbf983643b302a25336bff7a907583e Mon Sep 17 00:00:00 2001 From: Mathias Spanhove Date: Fri, 28 Oct 2022 20:48:02 +0200 Subject: [PATCH 1/4] fix: Reset Request Body not using default values --- src/core/components/parameters/parameters.jsx | 7 +++++-- src/core/plugins/oas3/selectors.js | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/core/components/parameters/parameters.jsx b/src/core/components/parameters/parameters.jsx index eb6dd47aaae..ef6ed65dcd5 100644 --- a/src/core/components/parameters/parameters.jsx +++ b/src/core/components/parameters/parameters.jsx @@ -161,7 +161,10 @@ export default class Parameters extends Component { enabled={tryItOutEnabled} onCancelClick={this.props.onCancelClick} onTryoutClick={onTryoutClick} - onResetClick={() => oas3Actions.setRequestBodyValue({ value: undefined, pathMethod })}/> + onResetClick={() => { + const defaultRequestBodyValue = oas3Selectors.defaultRequestBodyValue(...pathMethod) + oas3Actions.setRequestBodyValue({ value: defaultRequestBodyValue, pathMethod }) + }}/> ) : null} {this.state.parametersVisible ?
@@ -220,7 +223,7 @@ export default class Parameters extends Component { onChange={(value) => { this.onChangeMediaType({ value, pathMethod }) }} - className="body-param-content-type" + className="body-param-content-type" ariaLabel="Request content type" />
diff --git a/src/core/plugins/oas3/selectors.js b/src/core/plugins/oas3/selectors.js index 72c86f01b4e..e4a477820a7 100644 --- a/src/core/plugins/oas3/selectors.js +++ b/src/core/plugins/oas3/selectors.js @@ -61,6 +61,26 @@ export const shouldRetainRequestBodyValue = onlyOAS3((state, path, method) => { } ) +export const defaultRequestBodyValue = (state, path, method) => (system) => { + const {oas3Selectors, specSelectors} = system.getSystem() + const spec = specSelectors.specJson() + if(isOAS3Helper(spec)) { + const currentMediaType = oas3Selectors.requestContentType(path, method) + if (currentMediaType) { + return getDefaultRequestBodyValue( + specSelectors.specResolvedSubtree(["paths", path, method, "requestBody"]), + currentMediaType, + oas3Selectors.activeExamplesMember( + path, method, + "requestBody", + "requestBody", + ) + ) + } + } + return null +} + export const hasUserEditedBody = (state, path, method) => (system) => { const {oas3Selectors, specSelectors} = system.getSystem() const spec = specSelectors.specJson() From 8da53988f20a417ca1a52c3316e6263a99c7926b Mon Sep 17 00:00:00 2001 From: Mathias Date: Tue, 8 Nov 2022 17:42:07 +0100 Subject: [PATCH 2/4] onResetClick extracted into a separate prop function --- src/core/components/operation.jsx | 3 +++ src/core/components/parameters/parameters.jsx | 9 ++++----- src/core/containers/OperationContainer.jsx | 6 ++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/core/components/operation.jsx b/src/core/components/operation.jsx index 49868813b26..f0c9e5129de 100644 --- a/src/core/components/operation.jsx +++ b/src/core/components/operation.jsx @@ -17,6 +17,7 @@ export default class Operation extends PureComponent { toggleShown: PropTypes.func.isRequired, onTryoutClick: PropTypes.func.isRequired, + onResetClick: PropTypes.func.isRequired, onCancelClick: PropTypes.func.isRequired, onExecute: PropTypes.func.isRequired, @@ -48,6 +49,7 @@ export default class Operation extends PureComponent { request, toggleShown, onTryoutClick, + onResetClick, onCancelClick, onExecute, fn, @@ -152,6 +154,7 @@ export default class Operation extends PureComponent { operation={operation} onChangeKey={onChangeKey} onTryoutClick = { onTryoutClick } + onResetClick = { onResetClick } onCancelClick = { onCancelClick } tryItOutEnabled = { tryItOutEnabled } allowTryItOut={allowTryItOut} diff --git a/src/core/components/parameters/parameters.jsx b/src/core/components/parameters/parameters.jsx index ef6ed65dcd5..29e22cd102a 100644 --- a/src/core/components/parameters/parameters.jsx +++ b/src/core/components/parameters/parameters.jsx @@ -25,6 +25,7 @@ export default class Parameters extends Component { tryItOutEnabled: PropTypes.bool, allowTryItOut: PropTypes.bool, onTryoutClick: PropTypes.func, + onResetClick: PropTypes.func, onCancelClick: PropTypes.func, onChangeKey: PropTypes.array, pathMethod: PropTypes.array.isRequired, @@ -73,7 +74,7 @@ export default class Parameters extends Component { }) } } - + onChangeMediaType = ({ value, pathMethod }) => { let { specActions, oas3Selectors, oas3Actions } = this.props const userHasEditedBody = oas3Selectors.hasUserEditedBody(...pathMethod) @@ -94,6 +95,7 @@ export default class Parameters extends Component { let { onTryoutClick, + onResetClick, parameters, allowTryItOut, tryItOutEnabled, @@ -161,10 +163,7 @@ export default class Parameters extends Component { enabled={tryItOutEnabled} onCancelClick={this.props.onCancelClick} onTryoutClick={onTryoutClick} - onResetClick={() => { - const defaultRequestBodyValue = oas3Selectors.defaultRequestBodyValue(...pathMethod) - oas3Actions.setRequestBodyValue({ value: defaultRequestBodyValue, pathMethod }) - }}/> + onResetClick={onResetClick}/> ) : null} {this.state.parametersVisible ?
diff --git a/src/core/containers/OperationContainer.jsx b/src/core/containers/OperationContainer.jsx index cdf560cf38e..45b75ef952f 100644 --- a/src/core/containers/OperationContainer.jsx +++ b/src/core/containers/OperationContainer.jsx @@ -122,6 +122,11 @@ export default class OperationContainer extends PureComponent { this.setState({tryItOutEnabled: !this.state.tryItOutEnabled}) } + onResetClick = () => { + const defaultRequestBodyValue = oas3Selectors.defaultRequestBodyValue(...pathMethod) + oas3Actions.setRequestBodyValue({ value: defaultRequestBodyValue, pathMethod }) + } + onExecute = () => { this.setState({ executeInProgress: true }) } @@ -225,6 +230,7 @@ export default class OperationContainer extends PureComponent { toggleShown={this.toggleShown} onTryoutClick={this.onTryoutClick} + onResetClick={this.onResetClick} onCancelClick={this.onCancelClick} onExecute={this.onExecute} specPath={specPath} From a5774d7d25139ffdb12916bb284878a0cd9c35bf Mon Sep 17 00:00:00 2001 From: Mathias Date: Tue, 8 Nov 2022 19:15:49 +0100 Subject: [PATCH 3/4] Add cypress test --- src/core/components/parameters/parameters.jsx | 2 +- src/core/containers/OperationContainer.jsx | 6 ++-- .../static/documents/bugs/8217.yaml | 28 +++++++++++++++++++ test/e2e-cypress/tests/bugs/8217.js | 24 ++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 test/e2e-cypress/static/documents/bugs/8217.yaml create mode 100644 test/e2e-cypress/tests/bugs/8217.js diff --git a/src/core/components/parameters/parameters.jsx b/src/core/components/parameters/parameters.jsx index 29e22cd102a..d76b23a56f6 100644 --- a/src/core/components/parameters/parameters.jsx +++ b/src/core/components/parameters/parameters.jsx @@ -163,7 +163,7 @@ export default class Parameters extends Component { enabled={tryItOutEnabled} onCancelClick={this.props.onCancelClick} onTryoutClick={onTryoutClick} - onResetClick={onResetClick}/> + onResetClick={() => onResetClick(pathMethod)}/> ) : null}
{this.state.parametersVisible ?
diff --git a/src/core/containers/OperationContainer.jsx b/src/core/containers/OperationContainer.jsx index 45b75ef952f..c017ac3220a 100644 --- a/src/core/containers/OperationContainer.jsx +++ b/src/core/containers/OperationContainer.jsx @@ -122,9 +122,9 @@ export default class OperationContainer extends PureComponent { this.setState({tryItOutEnabled: !this.state.tryItOutEnabled}) } - onResetClick = () => { - const defaultRequestBodyValue = oas3Selectors.defaultRequestBodyValue(...pathMethod) - oas3Actions.setRequestBodyValue({ value: defaultRequestBodyValue, pathMethod }) + onResetClick = (pathMethod) => { + const defaultRequestBodyValue = this.props.oas3Selectors.defaultRequestBodyValue(...pathMethod) + this.props.oas3Actions.setRequestBodyValue({ value: defaultRequestBodyValue, pathMethod }) } onExecute = () => { diff --git a/test/e2e-cypress/static/documents/bugs/8217.yaml b/test/e2e-cypress/static/documents/bugs/8217.yaml new file mode 100644 index 00000000000..fa3d8cf8363 --- /dev/null +++ b/test/e2e-cypress/static/documents/bugs/8217.yaml @@ -0,0 +1,28 @@ +openapi: 3.0.1 +info: + title: Example Swagger with required default body parameter + version: 1.0.0 +paths: + /pet: + post: + operationId: addPet + requestBody: + content: + application/x-www-form-urlencoded: + schema: + $ref: '#/components/schemas/BodyParameter' + required: true + responses: + 405: + description: Invalid input + content: {} +components: + schemas: + BodyParameter: + required: + - bodyParameter + type: object + properties: + bodyParameter: + type: string + default: default \ No newline at end of file diff --git a/test/e2e-cypress/tests/bugs/8217.js b/test/e2e-cypress/tests/bugs/8217.js new file mode 100644 index 00000000000..2c61c42a937 --- /dev/null +++ b/test/e2e-cypress/tests/bugs/8217.js @@ -0,0 +1,24 @@ +describe("#8217: Reset Request Body not using default values", () => { + it("it reset the user edited value and executes with the default value in case of try out reset. (#6517)", () => { + cy + .visit("?url=/documents/bugs/8217.yaml") + .get("#operations-default-addPet") + .click() + // Expand Try It Out + .get(".try-out__btn") + .click() + // replace default sample with bad value + .get(`.parameters[data-property-name="bodyParameter"] input`) + .type("{selectall}not the default value") + // Reset Try It Out + .get(".try-out__btn.reset") + .click() + // Submit using default value + .get(".btn.execute") + .click() + // No required validation error on body parameter + .get(`.parameters[data-property-name="bodyParameter"] input`) + .should("have.value", "default") + .and("not.have.class", "invalid") + }) +}) \ No newline at end of file From 814fa7fbf5c1279ff7d95d30392b00d99297be71 Mon Sep 17 00:00:00 2001 From: Mathias Date: Tue, 8 Nov 2022 21:54:54 +0100 Subject: [PATCH 4/4] Rename defaultRequestBodyValue to selectDefaultRequestBodyValue --- src/core/containers/OperationContainer.jsx | 2 +- src/core/plugins/oas3/selectors.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/containers/OperationContainer.jsx b/src/core/containers/OperationContainer.jsx index c017ac3220a..89ce7f51ae1 100644 --- a/src/core/containers/OperationContainer.jsx +++ b/src/core/containers/OperationContainer.jsx @@ -123,7 +123,7 @@ export default class OperationContainer extends PureComponent { } onResetClick = (pathMethod) => { - const defaultRequestBodyValue = this.props.oas3Selectors.defaultRequestBodyValue(...pathMethod) + const defaultRequestBodyValue = this.props.oas3Selectors.selectDefaultRequestBodyValue(...pathMethod) this.props.oas3Actions.setRequestBodyValue({ value: defaultRequestBodyValue, pathMethod }) } diff --git a/src/core/plugins/oas3/selectors.js b/src/core/plugins/oas3/selectors.js index e4a477820a7..792b844eeaf 100644 --- a/src/core/plugins/oas3/selectors.js +++ b/src/core/plugins/oas3/selectors.js @@ -61,7 +61,7 @@ export const shouldRetainRequestBodyValue = onlyOAS3((state, path, method) => { } ) -export const defaultRequestBodyValue = (state, path, method) => (system) => { +export const selectDefaultRequestBodyValue = (state, path, method) => (system) => { const {oas3Selectors, specSelectors} = system.getSystem() const spec = specSelectors.specJson() if(isOAS3Helper(spec)) {