|
1 | 1 | using System.Diagnostics.CodeAnalysis; |
2 | 2 | using Microsoft.AspNetCore.Http; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
3 | 4 |
|
4 | 5 | namespace APIWeaver; |
5 | 6 |
|
@@ -148,74 +149,60 @@ public static OpenApiOptions AddAuthResponse(this OpenApiOptions options) |
148 | 149 | public static OpenApiOptions AddResponseDescriptions(this OpenApiOptions options) => options.AddOperationTransformer<ResponseDescriptionTransformer>(); |
149 | 150 |
|
150 | 151 | /// <summary> |
151 | | - /// Adds the name of the request body parameter to the <see cref="OpenApiOperation"/> by adding the <c>x-name</c> key to the operation. |
| 152 | + /// Adds the name of the request body parameter to the <see cref="OpenApiOperation" /> by adding the <c>x-name</c> key to the operation. |
152 | 153 | /// </summary> |
153 | 154 | /// <param name="options"><see cref="OpenApiOptions" />.</param> |
154 | 155 | public static OpenApiOptions AddRequestBodyParameterName(this OpenApiOptions options) => options.AddOperationTransformer<RequestBodyParameterNameTransformer>(); |
155 | 156 |
|
156 | | - /// <summary> |
157 | | - /// Adds a server to the OpenAPI document with the specified URL. |
158 | | - /// </summary> |
159 | | - /// <param name="options"><see cref="OpenApiOptions" />.</param> |
160 | | - /// <param name="url">The URL of the server to add.</param> |
161 | | - /// <param name="replace">If true, replaces the existing servers; otherwise, adds to the existing servers.</param> |
162 | | - public static OpenApiOptions AddServer(this OpenApiOptions options, [StringSyntax(StringSyntaxAttribute.Uri)] string url, bool replace = false) |
163 | | - { |
164 | | - var server = new OpenApiServer |
165 | | - { |
166 | | - Url = url |
167 | | - }; |
168 | | - return options.AddServer(server, replace); |
169 | | - } |
170 | | - |
171 | 157 | /// <summary> |
172 | 158 | /// Adds a server to the OpenAPI document. |
173 | 159 | /// </summary> |
174 | 160 | /// <param name="options"><see cref="OpenApiOptions" />.</param> |
175 | | - /// <param name="server">The <see cref="OpenApiServer"/> instance to add.</param> |
176 | | - /// <param name="replace">If true, replaces the existing servers; otherwise, adds to the existing servers.</param> |
177 | | - public static OpenApiOptions AddServer(this OpenApiOptions options, OpenApiServer server, bool replace = false) |
| 161 | + /// <param name="urls">The list of server URLs to add.</param> |
| 162 | + /// <remarks>Existing servers are replaced.</remarks> |
| 163 | + public static OpenApiOptions AddServer(this OpenApiOptions options, [StringSyntax(StringSyntaxAttribute.Uri)] params IEnumerable<string> urls) |
178 | 164 | { |
179 | | - options.AddDocumentTransformer((document, _) => |
| 165 | + var servers = urls.Select(url => new OpenApiServer |
180 | 166 | { |
181 | | - if (replace || document.Servers is null) |
182 | | - { |
183 | | - document.Servers = []; |
184 | | - } |
185 | | - |
186 | | - document.Servers.Add(server); |
| 167 | + Url = url |
187 | 168 | }); |
188 | | - return options; |
| 169 | + return options.AddServer(servers); |
189 | 170 | } |
190 | 171 |
|
191 | 172 | /// <summary> |
192 | 173 | /// Adds a server to the OpenAPI document. |
193 | 174 | /// </summary> |
194 | 175 | /// <param name="options"><see cref="OpenApiOptions" />.</param> |
195 | | - /// <param name="urls">The list of server URLs to add.</param> |
| 176 | + /// <param name="servers">The list of <see cref="OpenApiServer" /> instances to add.</param> |
196 | 177 | /// <remarks>Existing servers are replaced.</remarks> |
197 | | - public static OpenApiOptions AddServers(this OpenApiOptions options, [StringSyntax(StringSyntaxAttribute.Uri)] params IEnumerable<string> urls) |
| 178 | + public static OpenApiOptions AddServer(this OpenApiOptions options, params IEnumerable<OpenApiServer> servers) |
198 | 179 | { |
199 | | - var servers = urls.Select(u => new OpenApiServer |
200 | | - { |
201 | | - Url = u |
202 | | - }); |
203 | | - return options.AddServers(servers); |
| 180 | + options.AddDocumentTransformer((document, _) => { document.Servers = [..servers]; }); |
| 181 | + return options; |
204 | 182 | } |
205 | 183 |
|
206 | 184 | /// <summary> |
207 | | - /// Adds a server to the OpenAPI document. |
| 185 | + /// Adds a server to the OpenAPI document based on the current HTTP request. |
208 | 186 | /// </summary> |
209 | 187 | /// <param name="options"><see cref="OpenApiOptions" />.</param> |
210 | | - /// <param name="servers">The list of <see cref="OpenApiServer"/> instances to add.</param> |
211 | | - /// <remarks>Existing servers are replaced.</remarks> |
212 | | - public static OpenApiOptions AddServers(this OpenApiOptions options, params IEnumerable<OpenApiServer> servers) |
| 188 | + /// <param name="description">An optional description for the server.</param> |
| 189 | + /// <remarks>Ensure that the <see cref="IHttpContextAccessor" /> is registered in the service collection.</remarks> |
| 190 | + public static OpenApiOptions AddServerFromRequest(this OpenApiOptions options, string? description = null) |
213 | 191 | { |
214 | | - options.AddDocumentTransformer((document, _) => |
| 192 | + options.AddDocumentTransformer((document, context) => |
215 | 193 | { |
216 | | - document.Servers ??= []; |
217 | | - |
218 | | - document.Servers = servers.ToList(); |
| 194 | + var contextAccessor = context.ApplicationServices.GetRequiredService<IHttpContextAccessor>(); |
| 195 | + var httpContext = contextAccessor.HttpContext; |
| 196 | + if (httpContext is not null) |
| 197 | + { |
| 198 | + var url = httpContext.Request.GetRequestPath(); |
| 199 | + var server = new OpenApiServer |
| 200 | + { |
| 201 | + Url = url, |
| 202 | + Description = description |
| 203 | + }; |
| 204 | + document.Servers = [server]; |
| 205 | + } |
219 | 206 | }); |
220 | 207 | return options; |
221 | 208 | } |
|
0 commit comments