Skip to content

Commit ea26b98

Browse files
Add support for generating OpenAPI spec in YAML. Closes #956 (#957)
* Add support for generating OpenAPI spec in YAML. Closes #956 * Remove unused namespace reference from OpenApiSpecGeneratorPlugin.cs --------- Co-authored-by: Waldek Mastykarz <[email protected]>
1 parent a8d59ee commit ea26b98

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

dev-proxy-plugins/RequestLogs/OpenApiSpecGeneratorPlugin.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@ internal enum SpecVersion
5050
v3_0
5151
}
5252

53+
[JsonConverter(typeof(JsonStringEnumConverter))]
54+
internal enum SpecFormat
55+
{
56+
Json,
57+
Yaml
58+
}
59+
5360
internal class OpenApiSpecGeneratorPluginConfiguration
5461
{
5562
public bool IncludeOptionsRequests { get; set; } = false;
5663

5764
public SpecVersion SpecVersion { get; set; } = SpecVersion.v3_0;
65+
66+
public SpecFormat SpecFormat { get; set; } = SpecFormat.Json;
5867
}
5968

6069
public class OpenApiSpecGeneratorPlugin(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : BaseReportingPlugin(pluginEvents, context, logger, urlsToWatch, configSection)
@@ -364,7 +373,7 @@ request.Context is null ||
364373
foreach (var openApiDoc in openApiDocs)
365374
{
366375
var server = openApiDoc.Servers.First();
367-
var fileName = GetFileNameFromServerUrl(server.Url);
376+
var fileName = GetFileNameFromServerUrl(server.Url, _configuration.SpecFormat);
368377

369378
var openApiSpecVersion = _configuration.SpecVersion switch
370379
{
@@ -373,7 +382,12 @@ request.Context is null ||
373382
_ => OpenApiSpecVersion.OpenApi3_0
374383
};
375384

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

378392
Logger.LogDebug(" Writing OpenAPI spec to {fileName}...", fileName);
379393
File.WriteAllText(fileName, docString);
@@ -922,10 +936,16 @@ private void AddOrMergeResponse(OpenApiOperation operation, OpenApiResponses api
922936
MergeSchema(contentFromOperation.Schema, apiResponse.Content.First().Value.Schema);
923937
}
924938

925-
private static string GetFileNameFromServerUrl(string serverUrl)
939+
private static string GetFileNameFromServerUrl(string serverUrl, SpecFormat format)
926940
{
927941
var uri = new Uri(serverUrl);
928-
var fileName = $"{uri.Host}-{DateTime.Now:yyyyMMddHHmmss}.json";
942+
var ext = format switch
943+
{
944+
SpecFormat.Json => "json",
945+
SpecFormat.Yaml => "yaml",
946+
_ => "json"
947+
};
948+
var fileName = $"{uri.Host}-{DateTime.Now:yyyyMMddHHmmss}.{ext}";
929949
return fileName;
930950
}
931951

0 commit comments

Comments
 (0)