Skip to content

Disable the validation badge for those who do not want it #5994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Parameter name | Docker variable | Description
<a name="responseInterceptor"></a>`responseInterceptor` | _Unavailable_ | `Function=(a => a)`. MUST be a function. Function to intercept remote definition, "Try it out", and OAuth 2.0 responses. Accepts one argument responseInterceptor(response) and must return the modified response, or a Promise that resolves to the modified response.
<a name="showMutatedRequest"></a>`showMutatedRequest` | `SHOW_MUTATED_REQUEST` | `Boolean=true`. If set to `true`, uses the mutated request returned from a requestInterceptor to produce the curl command in the UI, otherwise the request before the requestInterceptor was applied is used.
<a name="supportedSubmitMethods"></a>`supportedSubmitMethods` | `SUPPORTED_SUBMIT_METHODS` | `Array=["get", "put", "post", "delete", "options", "head", "patch", "trace"]`. List of HTTP methods that have the "Try it out" feature enabled. An empty array disables "Try it out" for all operations. This does not filter the operations from the display.
<a name="validatorUrl"></a>`validatorUrl` | `VALIDATOR_URL` | `String="https://validator.swagger.io/validator" OR null`. By default, Swagger UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to `null` will disable validation.
<a name="validatorUrl"></a>`validatorUrl` | `VALIDATOR_URL` | `String="https://validator.swagger.io/validator" OR null`. By default, Swagger UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to either `none`, `127.0.0.1` or `localhost` will disable validation.
<a name="withCredentials"></a>`withCredentials` | `WITH_CREDENTIALS` | `Boolean=false` If set to `true`, enables passing credentials, [as defined in the Fetch standard](https://fetch.spec.whatwg.org/#credentials), in CORS requests that are sent by the browser. Note that Swagger UI cannot currently set cookies cross-domain (see [swagger-js#1163](https://github.com/swagger-api/swagger-js/issues/1163)) - as a result, you will have to rely on browser-supplied cookies (which this setting enables sending) that Swagger UI cannot control.

##### Macros
Expand Down
6 changes: 3 additions & 3 deletions src/core/components/online-validator-badge.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react"
import URL from "url-parse"

import PropTypes from "prop-types"
import { sanitizeUrl } from "core/utils"
import { sanitizeUrl, requiresValidationURL } from "core/utils"
import win from "core/window"

export default class OnlineValidatorBadge extends React.Component {
Expand Down Expand Up @@ -48,8 +48,8 @@ export default class OnlineValidatorBadge extends React.Component {

if ( typeof spec === "object" && Object.keys(spec).length) return null

if (!this.state.url || !this.state.validatorUrl || this.state.url.indexOf("localhost") >= 0
|| this.state.url.indexOf("127.0.0.1") >= 0) {
if (!this.state.url || !requiresValidationURL(this.state.validatorUrl)
|| !requiresValidationURL(this.state.url)) {
return null
}

Expand Down
9 changes: 9 additions & 0 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,15 @@ export function sanitizeUrl(url) {
return braintreeSanitizeUrl(url)
}


export function requiresValidationURL(uri) {
if (!uri || uri.indexOf("localhost") >= 0 || uri.indexOf("127.0.0.1") >= 0 || uri === "none") {
return false
}
return true
}


export function getAcceptControllingResponse(responses) {
if(!Im.OrderedMap.isOrderedMap(responses)) {
// wrong type!
Expand Down
48 changes: 47 additions & 1 deletion test/mocha/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getExtensions,
getCommonExtensions,
sanitizeUrl,
requiresValidationURL,
extractFileNameFromContentDispositionHeader,
deeplyStripKey,
getSampleSchema,
Expand Down Expand Up @@ -1339,7 +1340,52 @@ describe("utils", function() {
expect(sanitizeUrl({})).toEqual("")
})
})


describe("requiresValidationURL", function() {
it("Should tell us if we require a ValidationURL", function() {
const res = requiresValidationURL("https://example.com")

expect(res).toBe(true)
})

it(".. and localhost is not", function() {
const res = requiresValidationURL("http://localhost")

expect(res).toBe(false)
})

it(".. and neither does 127.0.0.1", function() {
const res = requiresValidationURL("http://127.0.0.1")

expect(res).toBe(false)
})

it(".. even without the proto", function() {
const res = requiresValidationURL("127.0.0.1")

expect(res).toBe(false)
})

it(".. and also not with 'none'", function() {
const res = requiresValidationURL("none")

expect(res).toBe(false)
})

it(".. and also not with 'none'", function() {
const res = requiresValidationURL("none")

expect(res).toBe(false)
})

it(".. and also not with ''", function() {
const res = requiresValidationURL("")

expect(res).toBe(false)
})

})

describe("getSampleSchema", function() {
const oriDate = Date

Expand Down