Skip to content

Commit b405ad0

Browse files
Fix critical bug with query parameters not added to URL when path parameters exist, fix HttpClientHandler resource leak, remove unused IsYaml method, add missing OneFilePerTag to AllowedValues
Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com>
1 parent 3cacbd1 commit b405ad0

File tree

3 files changed

+26
-34
lines changed

3 files changed

+26
-34
lines changed

src/HttpGenerator.Core/HttpFileGenerator.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -261,29 +261,30 @@ private static string GenerateRequest(
261261
code);
262262

263263
var url = operationPath.Key.Replace("{", "{{").Replace("}", "}}");
264-
if (url.Contains("{") || url.Contains("}"))
264+
265+
// Handle path parameters - replace placeholders in the URL
266+
foreach (var parameterName in parameterNameMap)
265267
{
266-
foreach (var parameterName in parameterNameMap)
267-
{
268-
url = url.Replace($"{{{{{parameterName.Key}}}}}", $"{{{{{parameterName.Value}}}}}");
269-
}
268+
url = url.Replace($"{{{{{parameterName.Key}}}}}", $"{{{{{parameterName.Value}}}}}");
270269
}
271-
else
270+
271+
// Handle query parameters - append to URL
272+
var queryParameters = operation
273+
.Parameters
274+
.Where(c => c.In == ParameterLocation.Query)
275+
.ToArray();
276+
277+
if (queryParameters.Length > 0)
272278
{
273-
if (parameterNameMap.Count > 0)
274-
{
275-
url += "?";
276-
}
277-
278-
foreach (var parameterName in parameterNameMap)
279-
{
280-
url += $"{parameterName.Key}={{{{{parameterName.Value}}}}}&";
281-
}
282-
283-
if (parameterNameMap.Count > 0)
279+
url += "?";
280+
foreach (var queryParam in queryParameters)
284281
{
285-
url = url.Remove(url.Length - 1);
282+
var variableName = parameterNameMap.TryGetValue(queryParam.Name, out var mappedName)
283+
? mappedName
284+
: queryParam.Name;
285+
url += $"{queryParam.Name}={{{{{variableName}}}}}&";
286286
}
287+
url = url.Remove(url.Length - 1); // Remove trailing &
287288
}
288289

289290
code.AppendLine($"{verb.ToUpperInvariant()} {{{{baseUrl}}}}{url}");

src/HttpGenerator.Core/OpenApiDocumentFactory.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ private static string DowngradeOpenApi31To30(string content)
6666
/// <returns>The content of the HTTP request.</returns>
6767
private static async Task<string> GetHttpContent(string openApiPath)
6868
{
69-
var httpMessageHandler = new HttpClientHandler();
70-
httpMessageHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
71-
httpMessageHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
72-
using var http = new HttpClient(httpMessageHandler);
69+
using var httpMessageHandler = new HttpClientHandler
70+
{
71+
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
72+
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
73+
};
74+
using var http = new HttpClient(httpMessageHandler, disposeHandler: false);
7375
var content = await http.GetStringAsync(openApiPath);
7476
return content;
7577
}
@@ -84,15 +86,4 @@ private static bool IsHttp(string path)
8486
return path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
8587
path.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
8688
}
87-
88-
/// <summary>
89-
/// Determines whether the specified path is a YAML file.
90-
/// </summary>
91-
/// <param name="path">The path to check.</param>
92-
/// <returns>True if the path is a YAML file, otherwise false.</returns>
93-
private static bool IsYaml(string path)
94-
{
95-
return path.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
96-
path.EndsWith(".yml", StringComparison.OrdinalIgnoreCase);
97-
}
9889
}

src/HttpGenerator/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class Settings : CommandSettings
5757
$"{nameof(OutputType.OneFilePerTag)} generates one .http file per first tag associated with each request.")]
5858
[CommandOption("--output-type <OUTPUT-TYPE>")]
5959
[DefaultValue(OutputType.OneRequestPerFile)]
60-
[AllowedValues(nameof(OutputType.OneRequestPerFile), nameof(OutputType.OneFile))]
60+
[AllowedValues(nameof(OutputType.OneRequestPerFile), nameof(OutputType.OneFile), nameof(OutputType.OneFilePerTag))]
6161
public OutputType OutputType { get; set; } = OutputType.OneRequestPerFile;
6262

6363
[Description("Azure Entra ID Scope to use for retrieving Access Token for Authorization header")]

0 commit comments

Comments
 (0)