Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Swashbuckle.AspNetCore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static int Main(string[] args)
? Path.Combine(Directory.GetCurrentDirectory(), arg1)
: null;

using (Stream stream = outputPath != null ? File.OpenWrite(outputPath) : Console.OpenStandardOutput())
using (Stream stream = outputPath != null ? File.Create(outputPath) : Console.OpenStandardOutput())
using (var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture))
{
IOpenApiWriter writer;
Expand Down
22 changes: 22 additions & 0 deletions test/Swashbuckle.AspNetCore.Cli.Test/ToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ public void Can_Generate_Swagger_Json()
Assert.True(productsPath.TryGetProperty("post", out _));
}

[Fact]
public void Overwrites_Existing_File()
{
using var temporaryDirectory = new TemporaryDirectory();
var path = Path.Combine(temporaryDirectory.Path, "swagger.json");

var dummyContent = new string('x', 100_000);
File.WriteAllText(path, dummyContent);

var args = new string[] { "tofile", "--output", path, Path.Combine(Directory.GetCurrentDirectory(), "Basic.dll"), "v1" };
Assert.Equal(0, Program.Main(args));

var readContent = File.ReadAllText(path);
Assert.True(readContent.Length < dummyContent.Length);
using var document = JsonDocument.Parse(readContent);

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

[Fact]
public void CustomDocumentSerializer_Writes_Custom_V2_Document()
{
Expand Down