Hi!
Is there any way to disable the Swagger Validator Badge?
I'm running a self hosted Web Api behind an IIS reverse proxy and I reference the localhost through the machine name. If I stick to http://localhost everything is fine, once I replace localhost with the machine name (reverse proxy config, root url and the startup url for the self host), the UI displays a error badge at the bottom of the page.
From the swagger doc:
"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). Setting it to null will disable validation. This parameter is relevant for Swagger 2.0 specs only."
Is there any way I can disable this through the Swashbuckle API?
Best regards
Bernhard Richter
Yes I'm having the same problem. I'm getting the ugly red "ERROR" validation badge on my Swagger pages when I deploy to our Intranet server. Is there any way we can disable validation in SwaggerConfig.cs?
Thanks,
George
Also, when you click the red "ERROR" badge all you get is a Http 500 error back from the swagger.io server. Does this mean that the problem is actually on the swagger.io server and not with my document? Can't seem to figure out what's going on here... Thanks again, -George

I think that when it sees something different than local host, it sends the
url of your API to swagger.io and then tries to validate it. Being an
intranet application, this would of course fail. I'm seeing the exact same
behavior with the internal server error and nothing else.
On Thursday, February 26, 2015, George Kapsambelis [email protected]
wrote:
Also, when you click the red "ERROR" badge all you get is a Http 500 error
back from the swagger.io server. Does this mean that the problem is
actually on the swagger.io server and not with my document? Can't seem to
figure out what's going on here... Thanks again, -George[image: image]
https://cloud.githubusercontent.com/assets/7048638/6402033/79553b48-bdd2-11e4-99cb-2a5256d9af14.png—
Reply to this email directly or view it on GitHub
https://github.com/domaindrivendev/Swashbuckle/issues/205#issuecomment-76275174
.
I have found a solution to this problem.
You need to provide your own index.html file using the custom asset feature of SwashBuckle.
I have based my index.html file upon this file.
<!DOCTYPE html>
<html>
<head>
<title>Swagger UI</title>
<link href='css/typography-css' media='screen' rel='stylesheet' type='text/css' />
<link href='css/reset-css' media='screen' rel='stylesheet' type='text/css' />
<link href='css/screen-css' media='screen' rel='stylesheet' type='text/css' />
<link href='css/reset-css' media='print' rel='stylesheet' type='text/css' />
<link href='css/screen-css' media='print' rel='stylesheet' type='text/css' />
%(StylesheetIncludes)
<script type="text/javascript" src="lib/shred-bundle-js"></script>
<script src='lib/jquery-1-8-0-min-js' type='text/javascript'></script>
<script src='lib/jquery-slideto-min-js' type='text/javascript'></script>
<script src='lib/jquery-wiggle-min-js' type='text/javascript'></script>
<script src='lib/jquery-ba-bbq-min-js' type='text/javascript'></script>
<script src='lib/handlebars-2-0-0-js' type='text/javascript'></script>
<script src='lib/underscore-min-js' type='text/javascript'></script>
<script src='lib/backbone-min-js' type='text/javascript'></script>
<script src='lib/swagger-client-js' type='text/javascript'></script>
<script src='swagger-ui-js' type='text/javascript'></script>
<script src='lib/highlight-7-3-pack-js' type='text/javascript'></script>
<script src='lib/marked-js' type='text/javascript'></script>
<!-- enabling this will enable oauth2 implicit scope support -->
<script src='lib/swagger-oauth-js' type='text/javascript'></script>
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://petstore.swagger.io/v2/swagger.json";
}
// Get Swashbuckle config into JavaScript
function arrayFrom(configString) {
return (configString != "") ? configString.split('|') : [];
}
window.swashbuckleConfig = {
rootUrl: '%(RootUrl)',
discoveryPaths: arrayFrom('%(DiscoveryPaths)'),
booleanValues: arrayFrom('%(BooleanValues)'),
customScripts: arrayFrom('%(CustomScripts)'),
docExpansion: '%(DocExpansion)',
oAuth2Enabled: Boolean('%(OAuth2Enabled)'),
oAuth2ClientId: '%(OAuth2ClientId)',
oAuth2Realm: '%(OAuth2Realm)',
oAuth2AppName: '%(OAuth2AppName)'
};
window.swaggerUi = new SwaggerUi({
url: swashbuckleConfig.rootUrl + "/" + swashbuckleConfig.discoveryPaths[0],
dom_id: "swagger-ui-container",
booleanValues: swashbuckleConfig.booleanValues,
validatorUrl : null,
onComplete: function (swaggerApi, swaggerUi) {
if (typeof initOAuth == "function" && swashbuckleConfig.oAuth2Enabled) {
initOAuth({
clientId: swashbuckleConfig.oAuth2ClientId,
realm: swashbuckleConfig.oAuth2Realm,
appName: swashbuckleConfig.oAuth2AppName
});
}
$('pre code').each(function (i, e) {
hljs.highlightBlock(e)
});
window.swaggerApi = swaggerApi;
_.each(swashbuckleConfig.customScripts, function (script) {
$.getScript(script);
});
},
onFailure: function (data) {
log("Unable to Load SwaggerUI");
},
docExpansion: swashbuckleConfig.docExpansion,
sorter: "alpha"
});
function addApiKeyAuthorization() {
var key = $('#input_apiKey')[0].value;
log("key: " + key);
if (key && key.trim() != "") {
log("added key " + key);
window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
}
}
$('#input_apiKey').change(function () {
addApiKeyAuthorization();
});
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
/*
var apiKey = "myApiKeyXXXX123456789";
$('#input_apiKey').val(apiKey);
addApiKeyAuthorization();
*/
window.swaggerUi.load();
});
</script>
</head>
<body class="swagger-section">
<div id='header'>
<div class="swagger-ui-wrap">
<a id="logo" href="http://swagger.io">swagger</a>
<form id='api_selector'>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text" /></div>
<div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text" /></div>
<div class='input'><a id="explore" href="#">Explore</a></div>
</form>
</div>
</div>
<div id="message-bar" class="swagger-ui-wrap"> </div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>
The line that turns validation off
validatorUrl : null,
Best regards
Bernhard Richter
Thanks, Bernard. Is it possible to add
this option to the SwaggerUIConfig?
El 27/02/2015 a las 11:36, Bernhard Richter escribió:
I have found a solution to this problem.
You need to provide your own index.html file using the custom
asset feature of SwashBuckle.
I have based my index.html file upon this file.
https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/SwaggerUi/CustomAssets/index.html
<!DOCTYPE html>
<!-- enabling this will enable oauth2 implicit scope support -->
<script src='lib/swagger-oauth-js' type='text/javascript'></script>
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://petstore.swagger.io/v2/swagger.json";
}
// Get Swashbuckle config into JavaScript
function arrayFrom(configString) {
return (configString != "") ? configString.split('|') : [];
}
window.swashbuckleConfig = {
rootUrl: '%(RootUrl)',
discoveryPaths: arrayFrom('%(DiscoveryPaths)'),
booleanValues: arrayFrom('%(BooleanValues)'),
customScripts: arrayFrom('%(CustomScripts)'),
docExpansion: '%(DocExpansion)',
oAuth2Enabled: Boolean('%(OAuth2Enabled)'),
oAuth2ClientId: '%(OAuth2ClientId)',
oAuth2Realm: '%(OAuth2Realm)',
oAuth2AppName: '%(OAuth2AppName)'
};
window.swaggerUi = new SwaggerUi({
url: swashbuckleConfig.rootUrl + "/" + swashbuckleConfig.discoveryPaths[0],
dom_id: "swagger-ui-container",
booleanValues: swashbuckleConfig.booleanValues,
validatorUrl : null,
onComplete: function (swaggerApi, swaggerUi) {
if (typeof initOAuth == "function" && swashbuckleConfig.oAuth2Enabled) {
initOAuth({
clientId: swashbuckleConfig.oAuth2ClientId,
realm: swashbuckleConfig.oAuth2Realm,
appName: swashbuckleConfig.oAuth2AppName
});
}
$('pre code').each(function (i, e) {
hljs.highlightBlock(e)
});
window.swaggerApi = swaggerApi;
_.each(swashbuckleConfig.customScripts, function (script) {
$.getScript(script);
});
},
onFailure: function (data) {
log("Unable to Load SwaggerUI");
},
docExpansion: swashbuckleConfig.docExpansion,
sorter: "alpha"
});
function addApiKeyAuthorization() {
var key = $('#input_apiKey')[0].value;
log("key: " + key);
if (key && key.trim() != "") {
log("added key " + key);
window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
}
}
$('#input_apiKey').change(function () {
addApiKeyAuthorization();
});
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
/*
var apiKey = "myApiKeyXXXX123456789";
$('#input_apiKey').val(apiKey);
addApiKeyAuthorization();
*/
window.swaggerUi.load();
});
</script>
<div id="message-bar" class="swagger-ui-wrap"> </div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
The line that turns validation off
validatorUrl : null,
Best regards
Bernhard Richter
—
Reply to this email directly or view
it on GitHub.
Although I am not involved in the development of Swashbuckle I would think that it should be fairly easy to add support for a custom validator url. I could take a look at it given that PR's are accepted.
Thank you Bernhard (@seesharper) for the quick solution to this problem!!! Much appreciated :)
The issue seems to be related to the $ref in the definitions, at least that's for my case. It should be using the full path such as "#/definitions/MyObject" if the type has a property for the "MyObject" type, which is also included in the definitions. However, in the swashbuckle generated json doc (http://someurl/swagger/docs/v1), the ref looks like this: "$ref":"MyObject". If I change it to use the full path of the MyObject, the validation passes (I tested it using the tool on site http://editor.swagger.io/#/). In the code (Swashbuckle.Core/Swagger/Filters/HandleFromUriParams.cs), there are 2 places that specifically removes the "#/definitions" in the schema path, I am not sure why it is done so, but apparently that could have caused the schema validation failure.
disable the badge does not solve the underlying issue, if the generated json doc does pass and conform to the swagger spec, the code generator may not work.
The current behavior used to pass the validator and was in fact required for the swagger-ui to work correctly. Clearly, both the spec and swagger-ui has recently changed it's stance on this, requiring all JSON references to be fully qualified (previously the swagger-ui only worked if JSON references already under the #/definitions path used a relative path).
As it happens, the new way is an easy fix and actually simplifies the code. I'll get this out (and the validator enable/disable feature) shortly.
Thanks for pointing this out
For others that come across this issue, here is how to configure swashbuckle to disable validation and hide the error badge:
.EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
.EnableSwaggerUi(c =>
{
// Prevents the red "Error" badge in the lower right, which only occurs b/c our internal URLS aren't reachable from the external swagger validation service.
c.DisableValidator();
});
This makes a ton of sense for internal APIs. Maybe someday the swagger validation service will be updated to not return an error (possibly just a warning) when the validation service can't fetch the swagger doc.
Any way to do this in 6.0? I have not found a way to disable the badge.
Most helpful comment
For others that come across this issue, here is how to configure swashbuckle to disable validation and hide the error badge:
This makes a ton of sense for internal APIs. Maybe someday the swagger validation service will be updated to not return an error (possibly just a warning) when the validation service can't fetch the swagger doc.