Skip to content
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

Fix for discovery paths bug #1162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions Swashbuckle.Core/Application/SwaggerUiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public SwaggerUiConfig(IEnumerable<string> discoveryPaths, Func<HttpRequestMessa
{ "%(DocumentTitle)", "Swagger UI" },
{ "%(StylesheetIncludes)", "" },
{ "%(DiscoveryPaths)", String.Join("|", discoveryPaths) },
{ "%(DiscoveryUrlSelectorEnabled)", "false" },
{ "%(BooleanValues)", "true|false" },
{ "%(ValidatorUrl)", "" },
{ "%(CustomScripts)", "" },
Expand All @@ -35,8 +36,8 @@ public SwaggerUiConfig(IEnumerable<string> discoveryPaths, Func<HttpRequestMessa
{ "%(OAuth2AppName)", "" },
{ "%(OAuth2ScopeSeperator)", " " },
{ "%(OAuth2AdditionalQueryStringParams)", "{}" },
{ "%(ApiKeyName)", "api_key" },
{ "%(ApiKeyIn)", "query" }
{ "%(ApiKeyName)", "api_key" },
{ "%(ApiKeyIn)", "query" }
};
_rootUrlResolver = rootUrlResolver;

Expand Down Expand Up @@ -112,7 +113,7 @@ public void CustomAsset(string path, Assembly resourceAssembly, string resourceN

public void EnableDiscoveryUrlSelector()
{
InjectJavaScript(GetType().Assembly, "Swashbuckle.SwaggerUi.CustomAssets.discoveryUrlSelector.js");
_templateParams["%(DiscoveryUrlSelectorEnabled)"] = "true";
}

public void EnableOAuth2Support(string clientId, string realm, string appName)
Expand Down
33 changes: 0 additions & 33 deletions Swashbuckle.Core/SwaggerUi/CustomAssets/discoveryUrlSelector.js

This file was deleted.

41 changes: 40 additions & 1 deletion Swashbuckle.Core/SwaggerUi/CustomAssets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
window.swashbuckleConfig = {
rootUrl: '%(RootUrl)',
discoveryPaths: arrayFrom('%(DiscoveryPaths)'),
discoveryUrlSelectorEnabled: ('%(DiscoveryUrlSelectorEnabled)' == 'true'),
booleanValues: arrayFrom('%(BooleanValues)'),
validatorUrl: stringOrNullFrom('%(ValidatorUrl)'),
customScripts: arrayFrom('%(CustomScripts)'),
Expand Down Expand Up @@ -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 id="input_baseUrl" name="baseUrl"></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 = $('<option>' + rootUrl + "/" + path + '</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) {
Expand Down
1 change: 0 additions & 1 deletion Swashbuckle.Core/Swashbuckle.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
</ItemGroup>
<ItemGroup />
<ItemGroup>
<EmbeddedResource Include="SwaggerUi\CustomAssets\discoveryUrlSelector.js" />
<EmbeddedResource Include="SwaggerUi\CustomAssets\typography.css" />
<EmbeddedResource Include="SwaggerUi\CustomAssets\index.html" />
<EmbeddedResource Include="SwaggerUi\CustomAssets\screen.css" />
Expand Down
2 changes: 1 addition & 1 deletion Swashbuckle.Tests/SwaggerUi/SwaggerUiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down