Upgrading swagger-ui to 3.7.0 with OAuth and error handling
window.onload = () => {
- const getBaseUrl = () => {
+ const getBaseURL = () => {
const urlMatches = /(.*)\/swagger-ui.html.*/.exec(window.location.href);
return urlMatches[1];
};
- (async () => {
- const configUIResponse = await fetch("swagger-resources/configuration/ui");
+ const buildSystemAsync = async (baseUrl) => {
+ try {
+ const configUIResponse = await fetch(baseUrl + "/swagger-resources/configuration/ui");
const configUI = await configUIResponse.json();
- const configSecurityResponse = await fetch("swagger-resources/configuration/security");
+ const configSecurityResponse = await fetch(baseUrl + "/swagger-resources/configuration/security");
const configSecurity = await configSecurityResponse.json();
- const resourcesResponse = await fetch("swagger-resources");
+ const resourcesResponse = await fetch(baseUrl + "/swagger-resources");
const resources = await resourcesResponse.json();
resources.forEach(resource => {
- resource.url = getBaseUrl() + resource.location
+ resource.url = baseUrl + resource.location;
});
+ window.ui = getUI(baseUrl, resources, configUI, configSecurity);
+ } catch (e) {
+ const retryURL = await prompt(
+ "Unable to infer base url. This is common when using dynamic servlet registration or when" +
+ " the API is behind an API Gateway. The base url is the root of where" +
+ " all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs" +
+ " then the base url is http://example.org/api/. Please enter the location manually: ",
+ window.location.href);
+
+ return buildSystemAsync(retryURL);
+ }
+ };
+
+ const getUI = (baseUrl, resources, configUI, configSecurity) => {
const ui = SwaggerUIBundle({
/*--------------------------------------------*\
* Core
\*--------------------------------------------*/
configUrl: null,
- dom_id: '#swagger-ui',
+ dom_id: "#swagger-ui",
dom_node: null,
spec: {},
url: "",
@@ -31,7 +46,7 @@ window.onload = () => {
/*--------------------------------------------*\
* Plugin system
\*--------------------------------------------*/
- layout: 'StandaloneLayout',
+ layout: "StandaloneLayout",
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
@@ -46,18 +61,18 @@ window.onload = () => {
displayOperationId: false,
defaultModelsExpandDepth: 1,
defaultModelExpandDepth: 1,
- defaultModelRendering: 'example',
+ defaultModelRendering: resources.defaultModelRendering,
displayRequestDuration: false,
- docExpansion: 'list',
+ docExpansion: resources.docExpansion,
filter: false,
maxDisplayedTags: null,
- operationsSorter: configUI.apisSorter || 'alpha',
+ operationsSorter: configUI.apisSorter || "alpha",
showExtensions: false,
- tagSorter: configUI.apisSorter || 'alpha',
+ tagSorter: configUI.apisSorter || "alpha",
/*--------------------------------------------*\
* Network
\*--------------------------------------------*/
- oauth2RedirectUrl: null,
+ oauth2RedirectUrl: baseUrl + "/webjars/springfox-swagger-ui/oauth2-redirect.html",
requestInterceptor: (a => a),
responseInterceptor: (a => a),
showMutatedRequest: true,
@@ -70,16 +85,24 @@ window.onload = () => {
});
ui.initOAuth({
+ /*--------------------------------------------*\
+ * OAuth
+ \*--------------------------------------------*/
clientId: configSecurity.clientId,
clientSecret: configSecurity.clientSecret,
realm: configSecurity.realm,
appName: configSecurity.appName,
- scopeSeparator: " ",
+ scopeSeparator: configSecurity.scopeSeparator,
additionalQueryStringParams: {},
useBasicAuthenticationWithAccessCodeGrant: false,
});
- window.ui = ui;
+ return ui;
+ };
+
+ /* Entry Point */
+ (async () => {
+ await buildSystemAsync(getBaseURL());
})();
};
