Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Swashbuckle.AspNetCore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public static int Main(string[] args)
? Path.Combine(Directory.GetCurrentDirectory(), arg1)
: null;

if (!string.IsNullOrEmpty(outputPath))
{
string directoryPath = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}

using (Stream stream = outputPath != null ? File.Create(outputPath) : Console.OpenStandardOutput())
using (var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture))
{
Expand Down
41 changes: 39 additions & 2 deletions test/Swashbuckle.AspNetCore.Cli.Test/ToolTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using Swashbuckle.AspNetCore.TestSupport.Utilities;
using Xunit;
Expand Down Expand Up @@ -131,10 +132,32 @@ public static void Does_Not_Run_Crashing_HostedService()
Assert.True(path.TryGetProperty("get", out _));
}

private static JsonDocument RunApplication(Func<string, string[]> setup)
[Fact]
public static void Creates_New_Folder_Path()
{
using var document = RunApplication(outputPath =>
[
"tofile",
"--output",
outputPath,
"--serializeasv2",
Path.Combine(Directory.GetCurrentDirectory(), "Basic.dll"),
"v1"
], GenerateRandomString(5));

// verify one of the endpoints
var paths = document.RootElement.GetProperty("paths");
var productsPath = paths.GetProperty("/products");
Assert.True(productsPath.TryGetProperty("post", out _));
}

private static JsonDocument RunApplication(Func<string, string[]> setup, string subOutputPath = default)
{
using var temporaryDirectory = new TemporaryDirectory();
string outputPath = Path.Combine(temporaryDirectory.Path, "swagger.json");

var outputPath = !string.IsNullOrEmpty(subOutputPath)
? Path.Combine(temporaryDirectory.Path, subOutputPath, "swagger.json")
: Path.Combine(temporaryDirectory.Path, "swagger.json");

string[] args = setup(outputPath);

Expand All @@ -143,5 +166,19 @@ private static JsonDocument RunApplication(Func<string, string[]> setup)
string json = File.ReadAllText(outputPath);
return JsonDocument.Parse(json);
}

private static string GenerateRandomString(int length)
{
const string Choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var result = new StringBuilder(length);

for (int i = 0; i < length; i++)
{
result.Append(Choices[Random.Shared.Next(Choices.Length)]);
}

return result.ToString();
}

}
}