Skip to content

Commit 0f949b0

Browse files
bartizanwaldekmastykarzCopilot
authored
Extends OpenAPISpecGeneratorPlugin to include default values for parameters (#1323)
* refactor: Fix null exception as key cannot be null * feat: Add IncludeParameters property to list params be included with their values as defaults * refactor: Use deconstruction * feat: Add matched IncludeParameters into spec with their default values * Fix includeParameters description * Update openapispecgeneratorplugin.schema.json Co-authored-by: Copilot <[email protected]> * fix: Update null check to use pattern matching for query parameters --------- Co-authored-by: Waldek Mastykarz <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent eb78ef9 commit 0f949b0

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

DevProxy.Plugins/Generation/OpenApiSpecGeneratorPlugin.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Extensions.Configuration;
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.OpenApi;
12+
using Microsoft.OpenApi.Any;
1213
using Microsoft.OpenApi.Extensions;
1314
using Microsoft.OpenApi.Interfaces;
1415
using Microsoft.OpenApi.Models;
@@ -56,6 +57,7 @@ public sealed class OpenApiSpecGeneratorPluginConfiguration
5657
public SpecFormat SpecFormat { get; set; } = SpecFormat.Json;
5758
public SpecVersion SpecVersion { get; set; } = SpecVersion.v3_0;
5859
public bool IgnoreResponseTypes { get; set; }
60+
public IEnumerable<string> IncludeParameters { get; set; } = [];
5961
}
6062

6163
public sealed class OpenApiSpecGeneratorPlugin(
@@ -319,21 +321,35 @@ private void SetParametersFromQueryString(OpenApiOperation operation, NameValueC
319321
}
320322

321323
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);
325327

326-
foreach (var parameter in dictionary)
328+
foreach (var (key, value) in dictionary)
327329
{
328-
operation.Parameters.Add(new()
330+
var isRequired = Configuration.IncludeParameters.Any(p => string.Equals(p, key, StringComparison.Ordinal));
331+
332+
OpenApiParameter parameter = new()
329333
{
330-
Name = parameter.Key,
334+
Name = key,
331335
In = ParameterLocation.Query,
332-
Required = false,
336+
Required = isRequired,
333337
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;
336351
}
352+
parameter.Schema.Default = new OpenApiString(value.ToString());
337353
}
338354

339355
private void SetResponseFromSession(OpenApiOperation operation, Response response)

schemas/v1.0.0/openapispecgeneratorplugin.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
"Yaml"
3131
],
3232
"description": "Specifies the format of the generated OpenAPI spec. Allowed values: 'Json' or 'Yaml'. Default: 'Json'."
33+
},
34+
"includeParameters": {
35+
"type": "array",
36+
"items": {
37+
"type": "string"
38+
},
39+
"description": "Defines the list of query string parameters to include in the generated OpenAPI spec, along with their default values. Default: []",
40+
"default": []
3341
}
3442
},
3543
"additionalProperties": false

0 commit comments

Comments
 (0)