Skip to content

Commit 381dcca

Browse files
committed
Add support for generating OpenAPI spec in YAML. Closes #956
1 parent 5dd14af commit 381dcca

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

dev-proxy-plugins/RequestLogs/OpenApiSpecGeneratorPlugin.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Extensions.Logging;
1717
using Microsoft.DevProxy.Abstractions.LanguageModel;
1818
using System.Text.Json.Serialization;
19+
using Microsoft.DevProxy.Plugins.ApiCenter;
1920

2021
namespace Microsoft.DevProxy.Plugins.RequestLogs;
2122

@@ -50,11 +51,20 @@ internal enum SpecVersion
5051
v3_0
5152
}
5253

54+
[JsonConverter(typeof(JsonStringEnumConverter))]
55+
internal enum SpecFormat
56+
{
57+
Json,
58+
Yaml
59+
}
60+
5361
internal class OpenApiSpecGeneratorPluginConfiguration
5462
{
5563
public bool IncludeOptionsRequests { get; set; } = false;
5664

5765
public SpecVersion SpecVersion { get; set; } = SpecVersion.v3_0;
66+
67+
public SpecFormat SpecFormat { get; set; } = SpecFormat.Json;
5868
}
5969

6070
public class OpenApiSpecGeneratorPlugin(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : BaseReportingPlugin(pluginEvents, context, logger, urlsToWatch, configSection)
@@ -364,7 +374,7 @@ request.Context is null ||
364374
foreach (var openApiDoc in openApiDocs)
365375
{
366376
var server = openApiDoc.Servers.First();
367-
var fileName = GetFileNameFromServerUrl(server.Url);
377+
var fileName = GetFileNameFromServerUrl(server.Url, _configuration.SpecFormat);
368378

369379
var openApiSpecVersion = _configuration.SpecVersion switch
370380
{
@@ -373,7 +383,12 @@ request.Context is null ||
373383
_ => OpenApiSpecVersion.OpenApi3_0
374384
};
375385

376-
var docString = openApiDoc.SerializeAsJson(openApiSpecVersion);
386+
var docString = _configuration.SpecFormat switch
387+
{
388+
SpecFormat.Json => openApiDoc.SerializeAsJson(openApiSpecVersion),
389+
SpecFormat.Yaml => openApiDoc.SerializeAsYaml(openApiSpecVersion),
390+
_ => openApiDoc.SerializeAsJson(openApiSpecVersion)
391+
};
377392

378393
Logger.LogDebug(" Writing OpenAPI spec to {fileName}...", fileName);
379394
File.WriteAllText(fileName, docString);
@@ -922,10 +937,16 @@ private void AddOrMergeResponse(OpenApiOperation operation, OpenApiResponses api
922937
MergeSchema(contentFromOperation.Schema, apiResponse.Content.First().Value.Schema);
923938
}
924939

925-
private static string GetFileNameFromServerUrl(string serverUrl)
940+
private static string GetFileNameFromServerUrl(string serverUrl, SpecFormat format)
926941
{
927942
var uri = new Uri(serverUrl);
928-
var fileName = $"{uri.Host}-{DateTime.Now:yyyyMMddHHmmss}.json";
943+
var ext = format switch
944+
{
945+
SpecFormat.Json => "json",
946+
SpecFormat.Yaml => "yaml",
947+
_ => "json"
948+
};
949+
var fileName = $"{uri.Host}-{DateTime.Now:yyyyMMddHHmmss}.{ext}";
929950
return fileName;
930951
}
931952

0 commit comments

Comments
 (0)