Skip to content

Commit

Permalink
Merge branch 'main' into timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
garrytrinder authored Dec 13, 2024
2 parents 8525277 + ea26b98 commit 048153c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
28 changes: 24 additions & 4 deletions dev-proxy-plugins/RequestLogs/OpenApiSpecGeneratorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,20 @@ internal enum SpecVersion
v3_0
}

[JsonConverter(typeof(JsonStringEnumConverter))]
internal enum SpecFormat
{
Json,
Yaml
}

internal class OpenApiSpecGeneratorPluginConfiguration
{
public bool IncludeOptionsRequests { get; set; } = false;

public SpecVersion SpecVersion { get; set; } = SpecVersion.v3_0;

public SpecFormat SpecFormat { get; set; } = SpecFormat.Json;
}

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

var openApiSpecVersion = _configuration.SpecVersion switch
{
Expand All @@ -373,7 +382,12 @@ request.Context is null ||
_ => OpenApiSpecVersion.OpenApi3_0
};

var docString = openApiDoc.SerializeAsJson(openApiSpecVersion);
var docString = _configuration.SpecFormat switch
{
SpecFormat.Json => openApiDoc.SerializeAsJson(openApiSpecVersion),
SpecFormat.Yaml => openApiDoc.SerializeAsYaml(openApiSpecVersion),
_ => openApiDoc.SerializeAsJson(openApiSpecVersion)
};

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

private static string GetFileNameFromServerUrl(string serverUrl)
private static string GetFileNameFromServerUrl(string serverUrl, SpecFormat format)
{
var uri = new Uri(serverUrl);
var fileName = $"{uri.Host}-{DateTime.Now:yyyyMMddHHmmss}.json";
var ext = format switch
{
SpecFormat.Json => "json",
SpecFormat.Yaml => "yaml",
_ => "json"
};
var fileName = $"{uri.Host}-{DateTime.Now:yyyyMMddHHmmss}.{ext}";
return fileName;
}

Expand Down
2 changes: 1 addition & 1 deletion dev-proxy/dev-proxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.22" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.0" />
<PackageReference Include="Unobtanium.Web.Proxy" Version="0.1.5" />
Expand Down

0 comments on commit 048153c

Please # to comment.