Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Existing output file should be deleted/overwritten #2886

Merged
merged 3 commits into from
May 15, 2024
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
Loading