Skip to content

Commit d1ac06c

Browse files
committed
Refactor OpenAPI spec version handling to use string type and improve error logging for unsupported versions
1 parent 1b52d4f commit d1ac06c

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

dev-proxy-plugins/RequestLogs/OpenApiSpecGeneratorPlugin.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
4545
internal class OpenApiSpecGeneratorPluginConfiguration
4646
{
4747
public bool IncludeOptionsRequests { get; set; } = false;
48-
49-
public int SpecVersion { get; set; } = 3;
48+
49+
public string SpecVersion { get; set; } = "3";
5050
}
5151

5252
public class OpenApiSpecGeneratorPlugin(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : BaseReportingPlugin(pluginEvents, context, logger, urlsToWatch, configSection)
@@ -294,6 +294,12 @@ public override async Task RegisterAsync()
294294

295295
ConfigSection?.Bind(_configuration);
296296

297+
if (_configuration.SpecVersion != "2" && _configuration.SpecVersion != "3")
298+
{
299+
Logger.LogError("OpenAPI spec version is not supported. Supported versions are 2 and 3.");
300+
return;
301+
}
302+
297303
PluginEvents.AfterRecordingStop += AfterRecordingStopAsync;
298304
}
299305

@@ -358,13 +364,8 @@ request.Context is null ||
358364
var server = openApiDoc.Servers.First();
359365
var fileName = GetFileNameFromServerUrl(server.Url);
360366

361-
var openApiSpecVersion = _configuration.SpecVersion switch
362-
{
363-
2 => OpenApiSpecVersion.OpenApi2_0,
364-
3 => OpenApiSpecVersion.OpenApi3_0,
365-
_ => throw new NotSupportedException($"OpenAPI spec version {_configuration.SpecVersion} is not supported. Supported versions are 2 and 3.")
366-
};
367-
367+
var openApiSpecVersion = _configuration.SpecVersion == "2" ? OpenApiSpecVersion.OpenApi2_0 : OpenApiSpecVersion.OpenApi3_0;
368+
368369
var docString = openApiDoc.SerializeAsJson(openApiSpecVersion);
369370

370371
Logger.LogDebug(" Writing OpenAPI spec to {fileName}...", fileName);
@@ -459,7 +460,8 @@ private async Task<string> GetOperationIdAsync(string method, string serverUrl,
459460
{
460461
var prompt = $"For the specified request, generate an operation ID, compatible with an OpenAPI spec. Respond with just the ID in plain-text format. For example, for request such as `GET https://api.contoso.com/books/{{books-id}}` you return `getBookById`. For a request like `GET https://api.contoso.com/books/{{books-id}}/authors` you return `getAuthorsForBookById`. Request: {method.ToUpper()} {serverUrl}{parametrizedPath}";
461462
ILanguageModelCompletionResponse? id = null;
462-
if (await Context.LanguageModelClient.IsEnabledAsync()) {
463+
if (await Context.LanguageModelClient.IsEnabledAsync())
464+
{
463465
id = await Context.LanguageModelClient.GenerateCompletionAsync(prompt);
464466
}
465467
return id?.Response ?? $"{method}{parametrizedPath.Replace('/', '.')}";
@@ -469,7 +471,8 @@ private async Task<string> GetOperationDescriptionAsync(string method, string se
469471
{
470472
var prompt = $"You're an expert in OpenAPI. You help developers build great OpenAPI specs for use with LLMs. For the specified request, generate a one-sentence description. Respond with just the description. For example, for a request such as `GET https://api.contoso.com/books/{{books-id}}` you return `Get a book by ID`. Request: {method.ToUpper()} {serverUrl}{parametrizedPath}";
471473
ILanguageModelCompletionResponse? description = null;
472-
if (await Context.LanguageModelClient.IsEnabledAsync()) {
474+
if (await Context.LanguageModelClient.IsEnabledAsync())
475+
{
473476
description = await Context.LanguageModelClient.GenerateCompletionAsync(prompt);
474477
}
475478
return description?.Response ?? $"{method} {parametrizedPath}";

0 commit comments

Comments
 (0)