|
9 | 9 | using Microsoft.Extensions.Configuration;
|
10 | 10 | using Microsoft.Extensions.Logging;
|
11 | 11 | using Microsoft.OpenApi;
|
| 12 | +using Microsoft.OpenApi.Any; |
12 | 13 | using Microsoft.OpenApi.Extensions;
|
13 | 14 | using Microsoft.OpenApi.Interfaces;
|
14 | 15 | using Microsoft.OpenApi.Models;
|
@@ -56,6 +57,7 @@ public sealed class OpenApiSpecGeneratorPluginConfiguration
|
56 | 57 | public SpecFormat SpecFormat { get; set; } = SpecFormat.Json;
|
57 | 58 | public SpecVersion SpecVersion { get; set; } = SpecVersion.v3_0;
|
58 | 59 | public bool IgnoreResponseTypes { get; set; }
|
| 60 | + public IEnumerable<string> IncludeParameters { get; set; } = []; |
59 | 61 | }
|
60 | 62 |
|
61 | 63 | public sealed class OpenApiSpecGeneratorPlugin(
|
@@ -319,21 +321,35 @@ private void SetParametersFromQueryString(OpenApiOperation operation, NameValueC
|
319 | 321 | }
|
320 | 322 |
|
321 | 323 | Logger.LogDebug(" Processing query string parameters...");
|
322 |
| -#pragma warning disable IDE0004 |
323 |
| - var dictionary = (queryParams.AllKeys as string[]).ToDictionary(k => k, k => queryParams[k] as object); |
324 |
| -#pragma warning restore IDE0004 |
| 324 | + var dictionary = queryParams.AllKeys |
| 325 | + .Where(k => k is not null).Cast<string>() |
| 326 | + .ToDictionary(k => k, k => queryParams[k] as object); |
325 | 327 |
|
326 |
| - foreach (var parameter in dictionary) |
| 328 | + foreach (var (key, value) in dictionary) |
327 | 329 | {
|
328 |
| - operation.Parameters.Add(new() |
| 330 | + var isRequired = Configuration.IncludeParameters.Any(p => string.Equals(p, key, StringComparison.Ordinal)); |
| 331 | + |
| 332 | + OpenApiParameter parameter = new() |
329 | 333 | {
|
330 |
| - Name = parameter.Key, |
| 334 | + Name = key, |
331 | 335 | In = ParameterLocation.Query,
|
332 |
| - Required = false, |
| 336 | + Required = isRequired, |
333 | 337 | Schema = new() { Type = "string" }
|
334 |
| - }); |
335 |
| - Logger.LogDebug(" Added query string parameter {ParameterKey}", parameter.Key); |
| 338 | + }; |
| 339 | + SetParameterDefault(parameter, value); |
| 340 | + |
| 341 | + operation.Parameters.Add(parameter); |
| 342 | + Logger.LogDebug(" Added query string parameter {ParameterKey}", key); |
| 343 | + } |
| 344 | + } |
| 345 | + |
| 346 | + private static void SetParameterDefault(OpenApiParameter parameter, object? value) |
| 347 | + { |
| 348 | + if (!parameter.Required || value is null) |
| 349 | + { |
| 350 | + return; |
336 | 351 | }
|
| 352 | + parameter.Schema.Default = new OpenApiString(value.ToString()); |
337 | 353 | }
|
338 | 354 |
|
339 | 355 | private void SetResponseFromSession(OpenApiOperation operation, Response response)
|
|
0 commit comments