diff --git a/Swashbuckle.Core/Application/SwaggerUiConfig.cs b/Swashbuckle.Core/Application/SwaggerUiConfig.cs index bba5259a3..41283a7fd 100644 --- a/Swashbuckle.Core/Application/SwaggerUiConfig.cs +++ b/Swashbuckle.Core/Application/SwaggerUiConfig.cs @@ -23,6 +23,7 @@ public SwaggerUiConfig(IEnumerable discoveryPaths, Func discoveryPaths, Func'); - select - .css('margin', '0') - .css('border', '1px solid gray') - .css('padding', '3px') - .css('width', '400px') - .css('font-size', '0.9em'); - - var rootUrl = this.options.rootUrl; - _.each(this.options.discoveryPaths, function(path) { - var option = $(''); - select.append(option); - }); - - select.val(defaultVal); - this.$el.replaceWith(select); - } - return this; - } - }); - - new DiscoveryUrlSelector({ - el: $('#input_baseUrl'), - rootUrl: swashbuckleConfig.rootUrl, - discoveryPaths: swashbuckleConfig.discoveryPaths - }).render(); -})(); \ No newline at end of file diff --git a/Swashbuckle.Core/SwaggerUi/CustomAssets/index.html b/Swashbuckle.Core/SwaggerUi/CustomAssets/index.html index 90e4b5854..6454866d3 100644 --- a/Swashbuckle.Core/SwaggerUi/CustomAssets/index.html +++ b/Swashbuckle.Core/SwaggerUi/CustomAssets/index.html @@ -47,6 +47,7 @@ window.swashbuckleConfig = { rootUrl: '%(RootUrl)', discoveryPaths: arrayFrom('%(DiscoveryPaths)'), + discoveryUrlSelectorEnabled: ('%(DiscoveryUrlSelectorEnabled)' == 'true'), booleanValues: arrayFrom('%(BooleanValues)'), validatorUrl: stringOrNullFrom('%(ValidatorUrl)'), customScripts: arrayFrom('%(CustomScripts)'), @@ -126,7 +127,45 @@ } $('#input_apiKey').change(addApiKeyAuthorization); - window.swaggerUi.load(); + if (swashbuckleConfig.discoveryPaths.length === 0) { + $("#message-bar").text("No document configured"); + } else { + window.swaggerUi.load(); + } + + if (swashbuckleConfig.discoveryUrlSelectorEnabled) { + var DiscoveryUrlSelector = Backbone.View.extend({ + render: function () { + // Don't re-render on subsequent reloads + var defaultVal = this.$el.val() + if (this.$el.prop('tagName') != 'SELECT') { + var select = $(''); + select + .css('margin', '0') + .css('border', '1px solid gray') + .css('padding', '3px') + .css('width', '400px') + .css('font-size', '0.9em'); + + var rootUrl = this.options.rootUrl; + _.each(this.options.discoveryPaths, function (path) { + var option = $(''); + select.append(option); + }); + + select.val(defaultVal); + this.$el.replaceWith(select); + } + return this; + } + }); + + new DiscoveryUrlSelector({ + el: $('#input_baseUrl'), + rootUrl: swashbuckleConfig.rootUrl, + discoveryPaths: swashbuckleConfig.discoveryPaths + }).render(); + } function log() { if ('console' in window) { diff --git a/Swashbuckle.Core/Swashbuckle.Core.csproj b/Swashbuckle.Core/Swashbuckle.Core.csproj index d830ccbba..8581490b6 100644 --- a/Swashbuckle.Core/Swashbuckle.Core.csproj +++ b/Swashbuckle.Core/Swashbuckle.Core.csproj @@ -117,7 +117,6 @@ - diff --git a/Swashbuckle.Tests/SwaggerUi/SwaggerUiTests.cs b/Swashbuckle.Tests/SwaggerUi/SwaggerUiTests.cs index 0a6a520b2..34e763b3a 100644 --- a/Swashbuckle.Tests/SwaggerUi/SwaggerUiTests.cs +++ b/Swashbuckle.Tests/SwaggerUi/SwaggerUiTests.cs @@ -115,7 +115,7 @@ public void It_exposes_config_to_enable_a_discovery_url_selector() var content = GetContentAsString("http://tempuri.org/swagger/ui/index"); - StringAssert.Contains("Swashbuckle-SwaggerUi-CustomAssets-discoveryUrlSelector-js", content); + StringAssert.Contains ("discoveryUrlSelectorEnabled: ('true' == 'true')", content); } [Test]