Skip to content

Commit 265bdc0

Browse files
authored
feat: add tryItOutEnabled configuration (#6865)
* feat: add tryItOutEnabled configuration allow users to set tryItOutEnabled: true to display the "Try it out" section by default tryItOutEnabled to take === "true" for the query string value or === true if someone implements query string type parsing in the query
1 parent 470e2fe commit 265bdc0

File tree

10 files changed

+58
-2
lines changed

10 files changed

+58
-2
lines changed

docker/configurator/variables.js

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ const standardVariables = {
9191
type: "array",
9292
name: "supportedSubmitMethods"
9393
},
94+
TRY_IT_OUT_ENABLED: {
95+
type: "boolean",
96+
name: "tryItOutEnabled"
97+
},
9498
VALIDATOR_URL: {
9599
type: "string",
96100
name: "validatorUrl"

docs/usage/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Parameter name | Docker variable | Description
6464
<a name="syntaxHighlight"></a>`syntaxHighlight` | _Unavailable_ | Set to `false` to deactivate syntax highlighting of payloads and cURL command, can be otherwise an object with the `activate` and `theme` properties.
6565
<a name="syntaxHighlight.activate"></a>`syntaxHighlight.activate` | _Unavailable_ | `Boolean=true`. Whether syntax highlighting should be activated or not.
6666
<a name="syntaxHighlight.theme"></a>`syntaxHighlight.theme` | _Unavailable_ | `String=["agate"*, "arta", "monokai", "nord", "obsidian", "tomorrow-night"]`. [Highlight.js](https://highlightjs.org/static/demo/) syntax coloring theme to use. (Only these 6 styles are available.)
67+
<a name="tryItOutEnabled"></a>`tryItOutEnabled` | `TRY_IT_OUT_ENABLED` | `Boolean=false`. Controls whether the "Try it out" section should be enabled by default.
6768

6869
##### Network
6970

flavors/swagger-ui-react/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ An array of functions that augment and modify Swagger UI's functionality. See Sw
113113

114114
⚠️ This prop is currently only applied once, on mount. Changes to this prop's value will not be propagated to the underlying Swagger UI instance. A future version of this module will remove this limitation, and the change will not be considered a breaking change.
115115

116+
#### `tryItOutEnabled`: PropTypes.bool
117+
118+
Controls whether the "Try it out" section should start enabled. The default is false.
119+
120+
⚠️ This prop is currently only applied once, on mount. Changes to this prop's value will not be propagated to the underlying Swagger UI instance. A future version of this module will remove this limitation, and the change will not be considered a breaking change.
121+
116122
## Limitations
117123

118124
* Not all configuration bindings are available.

flavors/swagger-ui-react/index.js renamed to flavors/swagger-ui-react/index.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class SwaggerUI extends React.Component {
2323
supportedSubmitMethods: this.props.supportedSubmitMethods,
2424
defaultModelExpandDepth: this.props.defaultModelExpandDepth,
2525
displayOperationId: this.props.displayOperationId,
26+
tryItOutEnabled: this.props.tryItOutEnabled,
2627
showMutatedRequest: typeof this.props.showMutatedRequest === "boolean" ? this.props.showMutatedRequest : true,
2728
deepLinking: typeof this.props.deepLinking === "boolean" ? this.props.deepLinking : false,
2829
})
@@ -101,6 +102,7 @@ SwaggerUI.propTypes = {
101102
defaultModelsExpandDepth: PropTypes.number,
102103
presets: PropTypes.arrayOf(PropTypes.func),
103104
deepLinking: PropTypes.bool,
105+
tryItOutEnabled: PropTypes.bool
104106
}
105107

106108
SwaggerUI.defaultProps = {

src/core/containers/OperationContainer.jsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import { Iterable, fromJS, Map } from "immutable"
77
export default class OperationContainer extends PureComponent {
88
constructor(props, context) {
99
super(props, context)
10+
11+
const { tryItOutEnabled } = props.getConfigs()
12+
1013
this.state = {
11-
tryItOutEnabled: false,
14+
tryItOutEnabled: tryItOutEnabled === true || tryItOutEnabled === "true",
1215
executeInProgress: false
1316
}
1417
}

src/core/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default function SwaggerUI(opts) {
4343
displayOperationId: false,
4444
displayRequestDuration: false,
4545
deepLinking: false,
46+
tryItOutEnabled: false,
4647
requestInterceptor: (a => a),
4748
responseInterceptor: (a => a),
4849
showMutatedRequest: true,

src/core/oauth2-authorize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default function authorize ( { auth, authActions, errActions, configs, au
6363
scopesArray = scopes.toArray()
6464
}
6565

66-
if (scopesArray.length > 0) {
66+
if (scopesArray.length > 0) {
6767
let scopeSeparator = authConfigs.scopeSeparator || " "
6868

6969
query.push("scope=" + encodeURIComponent(scopesArray.join(scopeSeparator)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
openapi: 3.0.2
2+
info:
3+
title: try it out enabled test
4+
version: 0.0.1
5+
description: |-
6+
a simple test to ensure tryItOutEnabled=true expands "Try it out"
7+
paths:
8+
/:
9+
get:
10+
summary: an operation
11+
responses:
12+
"200":
13+
description: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
describe("Try it out enabled configuration", () => {
2+
it("should enable the try it out section when true", () => {
3+
4+
cy
5+
.visit("?tryItOutEnabled=true&url=/documents/features/try-it-out-enabled.yaml")
6+
.get("#operations-default-get_")
7+
.click()
8+
.get(".try-out__btn")
9+
.should("have.text","Cancel")
10+
})
11+
12+
it("should disable the try it out section when false", () => {
13+
14+
cy
15+
.visit("?tryItOutEnabled=false&url=/documents/features/try-it-out-enabled.yaml")
16+
.get("#operations-default-get_")
17+
.click()
18+
.get(".try-out__btn")
19+
.should("have.text","Try it out ")
20+
})
21+
})
22+

test/unit/docker/translator.js

+4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ describe("docker: env translator", function() {
245245
OAUTH2_REDIRECT_URL: "http://google.com/",
246246
SHOW_MUTATED_REQUEST: "true",
247247
SUPPORTED_SUBMIT_METHODS: `["get", "post"]`,
248+
TRY_IT_OUT_ENABLED: "true",
248249
VALIDATOR_URL: "http://smartbear.com/"
249250
}
250251

@@ -270,6 +271,7 @@ describe("docker: env translator", function() {
270271
oauth2RedirectUrl: "http://google.com/",
271272
showMutatedRequest: true,
272273
supportedSubmitMethods: ["get", "post"],
274+
tryItOutEnabled: true,
273275
validatorUrl: "http://smartbear.com/",`
274276
).trim())
275277
})
@@ -299,6 +301,7 @@ describe("docker: env translator", function() {
299301
OAUTH2_REDIRECT_URL: "http://google.com/",
300302
SHOW_MUTATED_REQUEST: "true",
301303
SUPPORTED_SUBMIT_METHODS: `["get", "post"]`,
304+
TRY_IT_OUT_ENABLED: "false",
302305
VALIDATOR_URL: "http://smartbear.com/"
303306
}
304307

@@ -331,6 +334,7 @@ describe("docker: env translator", function() {
331334
oauth2RedirectUrl: "http://google.com/",
332335
showMutatedRequest: true,
333336
supportedSubmitMethods: ["get", "post"],
337+
tryItOutEnabled: false,
334338
validatorUrl: "http://smartbear.com/",`
335339
).trim())
336340
})

0 commit comments

Comments
 (0)