Skip to content
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ A collection of Visual Studio C# custom tool code generators for Swagger / OpenA
- `.refitter` settings files from [Refitter](https://github.com/christianhelle/refitter) by including it in the project and using the **Generate Refitter output** context menu
- `kiota-lock.json` configuration files from [Microsoft Kiota](https://github.com/microsoft/kiota) by including it in the project and using the **Generate Kiota output** context menu

### OpenAPI Generator configuration files

For the `rapicgen csharp openapi` command, you can now pass `--config-file <path>` to use a specific OpenAPI Generator configuration file. When no explicit file is supplied, Rapicgen also auto-discovers adjacent files named `<spec>.config.<ext>`, `<spec>.config.json`, and `<spec>.config.yaml` when `--use-configuration-file` is enabled. This makes it easier to drive package name, output layout, project format, and similar OpenAPI Generator options from configuration rather than relying only on the defaults.

### Custom Tools

Custom tools let you associate a tool with an item in a project and run that tool whenever the file is saved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class OpenApiCSharpGeneratorCommandSettings :
[Description("Path to directory containing additional mustache template files")]
public string? TemplatesPath { get; set; }

[CommandOption("--config-file")]
[Description("Path to an OpenAPI Generator configuration file to use instead of auto-discovery")]
public string? ConfigurationFile { get; set; }

[CommandOption("--use-configuration-file")]
[Description("Use the configuration file if present")]
public bool UseConfigurationFile { get; set; } = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using ApiClientCodeGen.Tests.Common;
using System;
using System.IO;
using ApiClientCodeGen.Tests.Common;
using ApiClientCodeGen.Tests.Common.Infrastructure;
using AutoFixture.Xunit2;
using Moq;
using Rapicgen.Core;
using Rapicgen.Core.External;
using Rapicgen.Core.Generators;
using Rapicgen.Core.Generators.OpenApi;
using Moq;
using Rapicgen.Core.Installer;
using Rapicgen.Core.Options.General;
using Rapicgen.Core.Options.OpenApiGenerator;
using Rapicgen.Core.Extensions;
using Xunit;

namespace ApiClientCodeGen.Core.Tests.Generators.OpenApi
Expand All @@ -23,5 +31,60 @@ public void Updates_Progress(
It.IsAny<uint>()),
Times.Exactly(5));
}

[Fact]
public void GenerateCode_Uses_Explicit_Configuration_File_When_Specified()
{
var tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempDirectory);

try
{
var swaggerFile = Path.Combine(tempDirectory, "petstore.yaml");
File.WriteAllText(swaggerFile, "openapi: 3.0.0");

var configFile = Path.Combine(tempDirectory, "petstore.config.yaml");
File.WriteAllText(configFile, "generatorName: csharp");

var version = OpenApiSupportedVersion.V7230;
var jarFile = Path.Combine(Path.GetTempPath(), $"openapi-generator-cli-{version.GetDescription()}.jar");
File.WriteAllText(jarFile, "jar");

var processLauncher = new Mock<IProcessLauncher>();
var dependencyInstaller = new Mock<IDependencyInstaller>();
var options = new DefaultGeneralOptions();
var openApiGeneratorOptions = new DefaultOpenApiGeneratorOptions
{
Version = version,
ConfigurationFile = configFile,
UseConfigurationFile = false
};

var sut = new OpenApiCSharpCodeGenerator(
swaggerFile,
"GeneratedCode",
options,
openApiGeneratorOptions,
processLauncher.Object,
dependencyInstaller.Object);

sut.GenerateCode(null);

processLauncher.Verify(
p => p.Start(
It.IsAny<string>(),
It.Is<string>(arguments => arguments.Contains($"-c \"{configFile}\"")),
It.IsAny<string?>()),
Times.Once);
}
finally
{
Directory.Delete(tempDirectory, recursive: true);
if (File.Exists(Path.Combine(Path.GetTempPath(), $"openapi-generator-cli-{OpenApiSupportedVersion.V7230.GetDescription()}.jar")))
{
File.Delete(Path.Combine(Path.GetTempPath(), $"openapi-generator-cli-{OpenApiSupportedVersion.V7230.GetDescription()}.jar"));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ public string GenerateCode(IProgressReporter? pGenerateProgress)
arguments += $"--http-user-agent \"{openApiGeneratorOptions.HttpUserAgent}\" ";
}

if (openApiGeneratorOptions.UseConfigurationFile)
if (!string.IsNullOrWhiteSpace(openApiGeneratorOptions.ConfigurationFile))
{
var configFile = openApiGeneratorOptions.ConfigurationFile;
if (!Path.IsPathRooted(configFile))
{
var swaggerDirectory = Path.GetDirectoryName(swaggerFile) ?? Directory.GetCurrentDirectory();
configFile = Path.GetFullPath(Path.Combine(swaggerDirectory, configFile));
}

arguments += $"-c \"{configFile}\" ";
}
else if (openApiGeneratorOptions.UseConfigurationFile)
{
var extension = Path.GetExtension(swaggerFile);
if (extension != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class DefaultOpenApiGeneratorOptions : IOpenApiGeneratorOptions

public string? TemplatesPath { get; set; }

public string? ConfigurationFile { get; set; }

public bool UseConfigurationFile { get; set; } = false;

public bool GenerateMultipleFiles { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IOpenApiGeneratorOptions
string? CustomAdditionalProperties { get; set; }
bool SkipFormModel { get; set; }
string? TemplatesPath { get; set; }
string? ConfigurationFile { get; set; }
bool UseConfigurationFile { get; set; }
bool GenerateMultipleFiles { get; set; }
public OpenApiSupportedVersion Version { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ private sealed class OpenApiOptions(SettingValues values) : IOpenApiGeneratorOpt
= NormalizeEmpty(values.ValueOrDefault(OpenApiGeneratorSettings.OpenApiCustomAdditionalProperties, string.Empty));
public bool SkipFormModel { get; set; } = values.ValueOrDefault(OpenApiGeneratorSettings.OpenApiSkipFormModel, true);
public string? TemplatesPath { get; set; } = NormalizeEmpty(values.ValueOrDefault(OpenApiGeneratorSettings.OpenApiTemplatesPath, string.Empty));
public string? ConfigurationFile { get; set; }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
public bool UseConfigurationFile { get; set; } = values.ValueOrDefault(OpenApiGeneratorSettings.OpenApiUseConfigurationFile, true);
public bool GenerateMultipleFiles { get; set; } = values.ValueOrDefault(OpenApiGeneratorSettings.OpenApiGenerateMultipleFiles, false);
public OpenApiSupportedVersion Version { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public class OpenApiGeneratorOptionsPage : DialogPage, IOpenApiGeneratorOptions
"This should be either an absolute path or a path relative to the swagger file.")]
public string? TemplatesPath { get; set; } = null!;

[Category(Name)]
[DisplayName("Configuration File")]
[Description("Optional path to an OpenAPI Generator configuration file. When set, the generator uses this file instead of auto-discovering one next to the swagger file.")]
public string? ConfigurationFile { get; set; } = null!;

[Category(Name)]
[DisplayName("Use Configuration File")]
[Description("Use the configuration file if present.")]
Expand Down
Loading