From b6acf7b814bd34d0220f53e5420e0ee70d7a6b51 Mon Sep 17 00:00:00 2001
From: pomianowski <13592821+pomianowski@users.noreply.github.com>
Date: Thu, 30 May 2024 15:35:26 +0200
Subject: [PATCH] Update client
---
.../Commands/GenerateCommand.cs | 26 +++++++++++++++--
src/OpenApi.Client.Cli/GlobalUsings.cs | 4 +--
.../OpenApi.Client.Cli.csproj | 4 +++
src/OpenApi.Client.Cli/Program.cs | 9 +++++-
.../Settings/GenerateCommandSettings.cs | 29 +++++++++++++++++--
.../Settings/JsonSerializerType.cs | 27 +++++++++++++++++
6 files changed, 90 insertions(+), 9 deletions(-)
create mode 100644 src/OpenApi.Client.Cli/Settings/JsonSerializerType.cs
diff --git a/src/OpenApi.Client.Cli/Commands/GenerateCommand.cs b/src/OpenApi.Client.Cli/Commands/GenerateCommand.cs
index 645d65f..f734432 100644
--- a/src/OpenApi.Client.Cli/Commands/GenerateCommand.cs
+++ b/src/OpenApi.Client.Cli/Commands/GenerateCommand.cs
@@ -4,20 +4,40 @@
// All Rights Reserved.
using OpenApi.Client.Cli.Settings;
+using OpenApi.Client.SourceGenerators.Schema;
+using OpenApi.Client.SourceGenerators.Serialization;
namespace OpenApi.Client.Cli.Commands;
+///
+/// Represents the Generate command in the Open API Client CLI.
+///
+///
+/// This command reads an Open API file, deserializes it into an IApiDocument object using the OpenApiSerializer,
+/// and then creates a new client class.
+///
+// ReSharper disable once ClassNeverInstantiated.Global
public sealed class GenerateCommand : AsyncCommand
{
///
- public override async Task ExecuteAsync(CommandContext context, GenerateCommandSettings settings)
+ public override async Task ExecuteAsync(
+ CommandContext context,
+ GenerateCommandSettings settings
+ )
{
- await Task.CompletedTask;
+ string contents = await File.ReadAllTextAsync(settings.File);
+
+ SerializationResult? serializationResult =
+ new OpenApiSerializer().Deserialize(settings.File, contents);
return 0;
}
- public override ValidationResult Validate(CommandContext context, GenerateCommandSettings settings)
+ ///
+ public override ValidationResult Validate(
+ CommandContext context,
+ GenerateCommandSettings settings
+ )
{
if (!Path.Exists(settings.File))
{
diff --git a/src/OpenApi.Client.Cli/GlobalUsings.cs b/src/OpenApi.Client.Cli/GlobalUsings.cs
index eb656df..8520283 100644
--- a/src/OpenApi.Client.Cli/GlobalUsings.cs
+++ b/src/OpenApi.Client.Cli/GlobalUsings.cs
@@ -3,9 +3,9 @@
// Copyright (C) Leszek Pomianowski and OpenAPI Client Contributors.
// All Rights Reserved.
+global using System.ComponentModel;
+global using System.Threading.Tasks;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Spectre.Console;
global using Spectre.Console.Cli;
-global using System.ComponentModel;
-global using System.Threading.Tasks;
diff --git a/src/OpenApi.Client.Cli/OpenApi.Client.Cli.csproj b/src/OpenApi.Client.Cli/OpenApi.Client.Cli.csproj
index 8090af8..15c62e4 100644
--- a/src/OpenApi.Client.Cli/OpenApi.Client.Cli.csproj
+++ b/src/OpenApi.Client.Cli/OpenApi.Client.Cli.csproj
@@ -1,9 +1,13 @@
+ openapiclient
OpenApiClient.Cli
net8.0
exe
+ true
+ openapiclient
+ true
diff --git a/src/OpenApi.Client.Cli/Program.cs b/src/OpenApi.Client.Cli/Program.cs
index 39f183a..d3f428a 100644
--- a/src/OpenApi.Client.Cli/Program.cs
+++ b/src/OpenApi.Client.Cli/Program.cs
@@ -13,7 +13,14 @@
app.Configure(config =>
{
- config.AddCommand("generate");
+ config.Settings.ApplicationName = "openapiclient";
+ config.Settings.CaseSensitivity = CaseSensitivity.None;
+
+ config
+ .AddCommand("generate")
+ .WithDescription("Generates the Open API Client from the provided json.")
+ .WithExample(["generate", "my-open-api.json", "--output", "MyOpenApiClient.cs"]);
+ ;
});
await app.RunAsync(args);
diff --git a/src/OpenApi.Client.Cli/Settings/GenerateCommandSettings.cs b/src/OpenApi.Client.Cli/Settings/GenerateCommandSettings.cs
index 11b1d9e..f33f218 100644
--- a/src/OpenApi.Client.Cli/Settings/GenerateCommandSettings.cs
+++ b/src/OpenApi.Client.Cli/Settings/GenerateCommandSettings.cs
@@ -5,13 +5,36 @@
namespace OpenApi.Client.Cli.Settings;
+///
+/// Settings for the Generate command in the Open API Client CLI.
+///
+///
+/// This class represents the command line options and arguments for the Generate command.
+/// The File property is a required argument that specifies the Open API file to parse.
+/// The Output property is an optional option that specifies the output directory. If not provided, the current directory is used.
+/// The Serializer property is an optional option that specifies the JSON serializer to use. If not provided, System.Text.Json is used.
+///
+// ReSharper disable once ClassNeverInstantiated.Global
public sealed class GenerateCommandSettings : CommandSettings
{
- [CommandOption("-f|--file ")]
+ ///
+ /// Gets or sets the path to the Open API file to parse.
+ ///
+ [CommandArgument(0, "")]
[Description("The Open API file to parse.")]
- public string File { get; set; }
+ public string File { get; set; } = default!;
+ ///
+ /// Gets or sets the output directory. If not provided, the current directory is used.
+ ///
[CommandOption("-o|--output ")]
[Description("The output directory.")]
- public string Output { get; set; }
+ public string Output { get; set; } = "./";
+
+ ///
+ /// Gets or sets the JSON serializer to use. If not provided, System.Text.Json is used.
+ ///
+ [CommandOption("-s|--serializer ")]
+ [Description("The JSON serializer.")]
+ public JsonSerializerType Serializer { get; set; } = JsonSerializerType.SystemTextJson;
}
diff --git a/src/OpenApi.Client.Cli/Settings/JsonSerializerType.cs b/src/OpenApi.Client.Cli/Settings/JsonSerializerType.cs
new file mode 100644
index 0000000..81d3190
--- /dev/null
+++ b/src/OpenApi.Client.Cli/Settings/JsonSerializerType.cs
@@ -0,0 +1,27 @@
+// This Source Code Form is subject to the terms of the MIT License.
+// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
+// Copyright (C) Leszek Pomianowski and OpenAPI Client Contributors.
+// All Rights Reserved.
+
+namespace OpenApi.Client.Cli.Settings;
+
+///
+/// Specifies the type of JSON serializer to use.
+///
+///
+/// This enum is used in the GenerateCommandSettings to allow the user to choose the JSON serializer.
+/// SystemTextJson represents the System.Text.Json serializer.
+/// NewtonsoftJson represents the Newtonsoft.Json serializer.
+///
+public enum JsonSerializerType
+{
+ ///
+ /// Represents the System.Text.Json serializer.
+ ///
+ SystemTextJson,
+
+ ///
+ /// Represents the Newtonsoft.Json serializer.
+ ///
+ NewtonsoftJson
+}