Skip to content

SwaggerUI Bundle request/response interceptors #635

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

Open
wants to merge 2 commits into
base: main
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
12 changes: 12 additions & 0 deletions docs/openapi-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ Suppose you want to customise the look and feels of the Swagger UI page. In this

// DO SOMETHING AFTER CALLING THE BASE METHOD
}

// Declare if you want to add a request interceptor function to the SwaggerUI instance.
public override Task<string> GetRequestInterceptorAsync()
{
return Task.FromResult("function(request) {/* DO SOMETHING WITH THE REQUEST */; return request;}");
}

// Declare if you want to add a response interceptor function to the SwaggerUI instance.
public override Task<string> GetResponseInterceptorAsync()
{
return Task.FromResult("function(response) {/* DO SOMETHING WITH THE RESPONSE */; return response;}");
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,17 @@ public interface IOpenApiCustomUIOptions
/// </summary>
/// <returns>The JavaScript string for custom UI.</returns>
Task<string> GetJavaScriptAsync();

/// <summary>
/// Gets the custom request interceptor to be used by Swagger UI.
/// </summary>
/// <returns>The JavaScript string containing a function that operates on a request.</returns>
Task<string> GetRequestInterceptorAsync();

/// <summary>
/// Gets the custom response interceptor to be used by Swagger UI.
/// </summary>
/// <returns>The JavaScript string containing a function that operates on a response.</returns>
Task<string> GetResponseInterceptorAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ public virtual async Task<string> GetJavaScriptAsync()
return await this.GetJavaScript.Invoke().ConfigureAwait(false);
}

/// <inheritdoc/>
public virtual Task<string> GetRequestInterceptorAsync()
{
return Task.FromResult(string.Empty);
}

/// <inheritdoc/>
public virtual Task<string> GetResponseInterceptorAsync()
{
return Task.FromResult(string.Empty);
}

private async Task<string> ReadFromStreamAsync(string path)
{
using (var stream = this.Assembly.GetManifestResourceStream($"{this.Assembly.GetName().Name}.{path}"))
Expand Down
18 changes: 17 additions & 1 deletion src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class SwaggerUI : ISwaggerUI
private const string SwaggerUIStandalonePresetJsPlaceholder = "[[SWAGGER_UI_STANDALONE_PRESET_JS]]";
private const string SwaggerUIApiPrefix = "[[SWAGGER_UI_API_PREFIX]]";
private const string SwaggerUrlPlaceholder = "[[SWAGGER_URL]]";
private const string SwaggerUIRequestInterceptor = "[[SWAGGER_UI_REQUEST_INTERCEPTOR]]";
private const string SwaggerUIResponseInterceptor = "[[SWAGGER_UI_RESPONSE_INTERCEPTOR]]";

private readonly string indexHtml = $"{typeof(SwaggerUI).Namespace}.dist.index.html";
private readonly string oauth2RedirectHtml = $"{typeof(SwaggerUI).Namespace}.dist.oauth2-redirect.html";
Expand All @@ -41,6 +43,8 @@ public class SwaggerUI : ISwaggerUI
private string _swaggerUiCustomJs;
private string _swaggerUiStandalonePresetJs;
private string _swaggerUiApiPrefix;
private string _swaggerUiRequestInterceptor;
private string _swaggerUiResponseInterceptor;
private string _indexHtml;
private string _oauth2RedirectHtml;

Expand Down Expand Up @@ -95,6 +99,8 @@ public async Task<ISwaggerUI> BuildAsync(Assembly assembly, IOpenApiCustomUIOpti
{
this._swaggerUiCustomCss = await options.GetStylesheetAsync();
this._swaggerUiCustomJs = await options.GetJavaScriptAsync();
this._swaggerUiRequestInterceptor = await options.GetRequestInterceptorAsync();
this._swaggerUiResponseInterceptor = await options.GetResponseInterceptorAsync();
}

using (var stream = assembly.GetManifestResourceStream(swaggerUiCss))
Expand Down Expand Up @@ -179,13 +185,23 @@ private string Render(string endpoint, OpenApiAuthLevelType authLevel = OpenApiA
swaggerUrl += "?" + string.Join("&", queries.SelectMany(p => p.Value.Select(q => $"{p.Key}={q}")));
}

var swaggerUiRequestInterceptor = this._swaggerUiRequestInterceptor.IsNullOrWhiteSpace()
? string.Empty
: $"requestInterceptor: {this._swaggerUiRequestInterceptor},";

var swaggerUiResponseInterceptor = this._swaggerUiResponseInterceptor.IsNullOrWhiteSpace()
? string.Empty
: $"responseInterceptor: {this._swaggerUiResponseInterceptor},";

var html = this._indexHtml.Replace(SwaggerUITitlePlaceholder, swaggerUiTitle)
.Replace(SwaggerUICssPlaceholder, this._swaggerUiCss)
.Replace(SwaggerUICustomCssPlaceholder, this._swaggerUiCustomCss)
.Replace(SwaggerUIBundleJsPlaceholder, this._swaggerUiBundleJs)
.Replace(SwaggerUICustomJsPlaceholder, this._swaggerUiCustomJs)
.Replace(SwaggerUIStandalonePresetJsPlaceholder, this._swaggerUiStandalonePresetJs)
.Replace(SwaggerUrlPlaceholder, swaggerUrl);
.Replace(SwaggerUrlPlaceholder, swaggerUrl)
.Replace(SwaggerUIRequestInterceptor, swaggerUiRequestInterceptor)
.Replace(SwaggerUIResponseInterceptor, swaggerUiResponseInterceptor);

return html;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
[[SWAGGER_UI_REQUEST_INTERCEPTOR]]
[[SWAGGER_UI_RESPONSE_INTERCEPTOR]]
layout: "StandaloneLayout"
});
// End Swagger UI call region
Expand Down