-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathSwaggerUiSettingsBase.cs
136 lines (116 loc) · 5.06 KB
/
SwaggerUiSettingsBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//-----------------------------------------------------------------------
// <copyright file="SwaggerUiSettingsBase.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------
using NSwag.Generation;
using System;
using System.Collections.Generic;
using NJsonSchema;
using System.Globalization;
using Newtonsoft.Json;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
#if AspNetOwin
using Microsoft.Owin;
namespace NSwag.AspNet.Owin
#else
using Microsoft.AspNetCore.Http;
namespace NSwag.AspNetCore
#endif
{
/// <summary>The base settings for all Swagger UIs.</summary>
#if AspNetOwin
public abstract class SwaggerUiSettingsBase<T> : SwaggerSettings<T>
where T : OpenApiDocumentGeneratorSettings, new()
#else
public abstract class SwaggerUiSettingsBase : SwaggerSettings
#endif
{
/// <summary>Initializes a new instance of the class.</summary>
public SwaggerUiSettingsBase()
{
TransformToExternalPath = (internalUiRoute, request) =>
'/' + (request.GetBasePath()?.TrimEnd('/') + '/' + internalUiRoute?.TrimStart('/')).Trim('/');
}
/// <summary>Gets or sets the internal swagger UI route (must start with '/').</summary>
public string Path { get; set; } = "/swagger";
#pragma warning disable 618
internal string ActualSwaggerUiPath => Path.Substring(MiddlewareBasePath?.Length ?? 0);
#pragma warning restore 618
/// <summary>Gets or sets custom inline styling to inject into the index.html</summary>
public string CustomInlineStyles { get; set; }
/// <summary>Gets or sets a URI to load a custom CSS Stylesheet into the index.html</summary>
public string CustomStylesheetPath { get; set; }
/// <summary>Gets or sets a URI to load a custom JavaScript file into the index.html.</summary>
public string CustomJavaScriptPath { get; set; }
/// <summary>Gets or sets a flag that indicates to use or not type="module" in a custom script tag (default: false).</summary>
public bool UseModuleTypeForCustomJavaScript { get; set; }
/// <summary>Gets or sets the external route base path (must start with '/', default: null = use SwaggerUiRoute).</summary>
#if AspNetOwin
public Func<string, IOwinRequest, string> TransformToExternalPath { get; set; }
internal abstract Task<string> TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken);
#else
public Func<string, HttpRequest, string> TransformToExternalPath { get; set; }
internal abstract Task<string> TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken);
#endif
/// <summary>
/// Gets an HTML snippet for including custom StyleSheet in swagger UI.
/// </summary>
#if AspNetOwin
protected string GetCustomStyleHtml(IOwinRequest request)
#else
protected string GetCustomStyleHtml(HttpRequest request)
#endif
{
var html = new StringBuilder();
if (!string.IsNullOrEmpty(CustomStylesheetPath))
{
var uriString = System.Net.WebUtility.HtmlEncode(TransformToExternalPath(CustomStylesheetPath, request));
html.AppendLine($"<link rel=\"stylesheet\" href=\"{uriString}\">");
}
else if (!string.IsNullOrEmpty(CustomInlineStyles))
{
html.AppendLine($"<style type=\"text/css\">{CustomInlineStyles}</style>");
}
return html.ToString();
}
/// <summary>
/// Gets an HTML snippet for including custom JavaScript in swagger UI.
/// </summary>
#if AspNetOwin
protected string GetCustomScriptHtml(IOwinRequest request)
#else
protected string GetCustomScriptHtml(HttpRequest request)
#endif
{
if (CustomJavaScriptPath == null)
{
return string.Empty;
}
var scriptType = string.Empty;
if (UseModuleTypeForCustomJavaScript)
{
scriptType = "type=\"module\"";
}
var uriString = System.Net.WebUtility.HtmlEncode(TransformToExternalPath(CustomJavaScriptPath, request));
return $"<script {scriptType} src=\"{uriString}\"></script>";
}
/// <summary>Generates the additional objects JavaScript code.</summary>
/// <param name="additionalSettings">The additional settings.</param>
/// <returns>The code.</returns>
protected string GenerateAdditionalSettings(IDictionary<string, object> additionalSettings)
{
var code = "";
foreach (var pair in additionalSettings)
{
code += pair.Key + ": " + JsonConvert.SerializeObject(pair.Value) + ", \n ";
}
return code;
}
}
}