diff --git a/src/libs/OpenApiGenerator.Cli/Commands/SimplifyCommand.cs b/src/libs/OpenApiGenerator.Cli/Commands/SimplifyCommand.cs new file mode 100644 index 0000000000..b3b9d796dd --- /dev/null +++ b/src/libs/OpenApiGenerator.Cli/Commands/SimplifyCommand.cs @@ -0,0 +1,55 @@ +using System.CommandLine; +using Microsoft.OpenApi; +using Microsoft.OpenApi.Extensions; +using OpenApiGenerator.Core.Extensions; + +namespace OpenApiGenerator.Cli.Commands; + +public class SimplifyCommand : Command +{ + public SimplifyCommand() : base(name: "simplify", description: "Simplifies OpenAPI spec.") + { + var inputOption = new Argument( + name: "input", + getDefaultValue: () => string.Empty, + description: "Input file path"); + var outputOption = new Option( + aliases: ["--output", "-o"], + getDefaultValue: () => "simplified.yaml", + description: "Output file path"); + AddArgument(inputOption); + AddOption(outputOption); + + this.SetHandler( + HandleAsync, + inputOption, + outputOption); + } + + private static async Task HandleAsync( + string inputPath, + string outputPath) + { + Console.WriteLine($"Loading {inputPath}..."); + + using var client = new HttpClient(); + var yamlOrJson = inputPath.StartsWith("http", StringComparison.OrdinalIgnoreCase) + ? await client.GetStringAsync(new Uri(inputPath)).ConfigureAwait(false) + : await File.ReadAllTextAsync(inputPath).ConfigureAwait(false); + var openApiDocument = yamlOrJson.GetOpenApiDocument(); + + Console.WriteLine("Simplifying..."); + + openApiDocument = openApiDocument.Simplify(); + + var text = Path.GetExtension(outputPath).ToUpperInvariant() switch + { + ".JSON" => openApiDocument.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0), + _ => openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0), + }; + + await File.WriteAllTextAsync(outputPath, text).ConfigureAwait(false); + + Console.WriteLine("Done."); + } +} \ No newline at end of file diff --git a/src/libs/OpenApiGenerator.Cli/Program.cs b/src/libs/OpenApiGenerator.Cli/Program.cs index 8a95f959c8..4c1e04782e 100644 --- a/src/libs/OpenApiGenerator.Cli/Program.cs +++ b/src/libs/OpenApiGenerator.Cli/Program.cs @@ -4,5 +4,6 @@ var rootCommand = new RootCommand( description: "CLI tool to use OpenApiGenerator"); rootCommand.AddCommand(new GenerateCommand()); +rootCommand.AddCommand(new SimplifyCommand()); return await rootCommand.InvokeAsync(args).ConfigureAwait(false); \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.IntegrationTests.Cli/CliTests.cs b/src/tests/OpenApiGenerator.IntegrationTests.Cli/CliTests.cs index 97dc172b7c..b9a4a4bddb 100644 --- a/src/tests/OpenApiGenerator.IntegrationTests.Cli/CliTests.cs +++ b/src/tests/OpenApiGenerator.IntegrationTests.Cli/CliTests.cs @@ -20,7 +20,7 @@ public class CliTests [DataRow("twitch.json")] [DataRow("https://dedoose-rest-api.onrender.com/swagger/v1/swagger.json")] [DataRow("together.yaml")] - public async Task Run(string spec) + public async Task Generate(string spec) { var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try