Skip to content

Commit

Permalink
CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Gamache committed Dec 20, 2023
1 parent c63ae7f commit c5f293f
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger)
this._logger = logger;
}

[HttpGet(Name = "GetWeatherForecast2")]
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
Expand Down
2 changes: 1 addition & 1 deletion src/WebApiDebugger/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"commandName": "Executable",
"executablePath": "dotnet",
"commandLineArgs": "msbuild /t:ValidateOpenApi",
"workingDirectory": "$(ProjectDir)",
"workingDirectory": "$(ProjectDir)"
},
"WebApiDebugger": {
"commandName": "Project",
Expand Down
4 changes: 2 additions & 2 deletions src/WebApiDebugger/WebApiDebugger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</PropertyGroup>

<PropertyGroup>
<DevelopmentMode>CodeFirst</DevelopmentMode>
<!-- <ValidateCodeSync>true</ValidateCodeSync>-->
<OpenApiDevelopmentMode>ContractFirst</OpenApiDevelopmentMode>
<OpenApiCompareCodeAgainstSpecFile>true</OpenApiCompareCodeAgainstSpecFile>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions src/Workleap.OpenApi.MSBuild/CodeFirstProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/// </summary>
internal class CodeFirstProcess
{
private const string DisableSpecGenEnvVarName = "WL_DISABLE_SPECGEN";

private readonly SpectralManager _spectralManager;
private readonly SwaggerManager _swaggerManager;

Expand All @@ -22,15 +24,13 @@ internal async Task Execute(
string openApiSpectralRulesetUrl,
CancellationToken cancellationToken)
{
var isGenerationEnable = Environment.GetEnvironmentVariable("WL_DISABLE_SPECGEN") != "true";
var isGenerationEnabled = string.Equals(Environment.GetEnvironmentVariable(DisableSpecGenEnvVarName), "true", StringComparison.OrdinalIgnoreCase);

await this.InstallDependencies(isGenerationEnable, cancellationToken);
await this.InstallDependencies(isGenerationEnabled, cancellationToken);

if (isGenerationEnable)
if (isGenerationEnabled)
{
var generateOpenApiDocsPath = (await this._swaggerManager.RunSwaggerAsync(openApiSwaggerDocumentNames, cancellationToken)).ToList();

// Will overwrite the file in the repo here
}

await this._spectralManager.RunSpectralAsync(openApiSpecificationFiles, openApiSpectralRulesetUrl, cancellationToken);
Expand Down
18 changes: 12 additions & 6 deletions src/Workleap.OpenApi.MSBuild/ContractFirstProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class ContractFirstProcess
private readonly SpectralManager _spectralManager;
private readonly SwaggerManager _swaggerManager;
private readonly OasdiffManager _oasdiffManager;

internal ContractFirstProcess(ILoggerWrapper loggerWrapper, SpectralManager spectralManager, SwaggerManager swaggerManager, OasdiffManager oasdiffManager)
{
this._loggerWrapper = loggerWrapper;
Expand All @@ -20,22 +20,28 @@ internal ContractFirstProcess(ILoggerWrapper loggerWrapper, SpectralManager spec
this._oasdiffManager = oasdiffManager;
}

internal enum CompareCodeAgainstSpecFile
{
Disabled,
Enabled,
}

internal async Task<bool> Execute(
string[] openApiSpecificationFiles,
string openApiToolsDirectoryPath,
string[] openApiSwaggerDocumentNames,
string openApiSpectralRulesetUrl,
bool validateCodeSync,
CompareCodeAgainstSpecFile compareCodeAgainstSpecFile,
CancellationToken cancellationToken)
{
if (!this.CheckIfBaseSpecExists(openApiSpecificationFiles, openApiToolsDirectoryPath))
{
return false;
}

await this.InstallDependencies(validateCodeSync, cancellationToken);
await this.InstallDependencies(compareCodeAgainstSpecFile, cancellationToken);

if (validateCodeSync)
if (compareCodeAgainstSpecFile == CompareCodeAgainstSpecFile.Enabled)
{
var generateOpenApiDocsPath = (await this._swaggerManager.RunSwaggerAsync(openApiSwaggerDocumentNames, cancellationToken)).ToList();
await this._oasdiffManager.RunOasdiffAsync(openApiSpecificationFiles, generateOpenApiDocsPath, cancellationToken);
Expand Down Expand Up @@ -70,13 +76,13 @@ private bool CheckIfBaseSpecExists(
}

private async Task InstallDependencies(
bool validateCodeSync,
CompareCodeAgainstSpecFile compareCodeAgainstSpecFile,
CancellationToken cancellationToken)
{
var installationTasks = new List<Task>();
installationTasks.Add(this._spectralManager.InstallSpectralAsync(cancellationToken));

if (validateCodeSync)
if (compareCodeAgainstSpecFile == CompareCodeAgainstSpecFile.Enabled)
{
installationTasks.Add(this._swaggerManager.InstallSwaggerCliAsync(cancellationToken));
installationTasks.Add(this._oasdiffManager.InstallOasdiffAsync(cancellationToken));
Expand Down
2 changes: 1 addition & 1 deletion src/Workleap.OpenApi.MSBuild/SpectralManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task RunSpectralAsync(IEnumerable<string> swaggerDocumentPaths, str
this._loggerWrapper.LogMessage("\n ******** Spectral: Validating {0} against ruleset ********", MessageImportance.High, documentPath);
File.Delete(htmlReportPath);
await this.GenerateSpectralReport(spectralExecutePath, documentPath, rulesetUrl, htmlReportPath, cancellationToken);
this._loggerWrapper.LogMessage("\n *********************************************************", MessageImportance.High, documentPath);
this._loggerWrapper.LogMessage("\n ****************************************************************", MessageImportance.High);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/Workleap.OpenApi.MSBuild/ValidateOpenApiTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ public sealed class ValidateOpenApiTask : CancelableAsyncTask
private const string ContractFirst = "ContractFirst";

/// <summary>
/// 2 supported mode:
/// 2 supported mode2:
/// - CodeFirst: Generate the OpenAPI specification files from the code
/// - ContractFirst: Will use the OpenAPI specification files provided
/// </summary>
[Microsoft.Build.Framework.Required]
public string DevelopmentMode { get; set; } = string.Empty;
[Required]
public string OpenApiDevelopmentMode { get; set; } = string.Empty;

/// <summary>When Development mode is Contract first, will validate if the specification match the code.</summary>
[Microsoft.Build.Framework.Required]
public bool ValidateCodeSync { get; set; } = false;
public bool OpenApiCompareCodeAgainstSpecFile { get; set; } = false;

/// <summary>The path of the ASP.NET Core project startup assembly directory.</summary>
[Required]
Expand Down Expand Up @@ -76,7 +76,7 @@ protected override async Task<bool> ExecuteAsync(CancellationToken cancellationT
await this.GeneratePublicNugetSource();
Directory.CreateDirectory(reportsPath);

switch (this.DevelopmentMode)
switch (this.OpenApiDevelopmentMode)
{
case CodeFirst:
await codeFirstProcess.Execute(
Expand All @@ -92,7 +92,7 @@ await codeFirstProcess.Execute(
this.OpenApiToolsDirectoryPath,
this.OpenApiSwaggerDocumentNames,
this.OpenApiSpectralRulesetUrl,
this.ValidateCodeSync,
this.OpenApiCompareCodeAgainstSpecFile ? ContractFirstProcess.CompareCodeAgainstSpecFile.Enabled : ContractFirstProcess.CompareCodeAgainstSpecFile.Disabled,
cancellationToken);

if (!isSuccess)
Expand All @@ -103,7 +103,7 @@ await codeFirstProcess.Execute(
break;

default:
this.Log.LogError("Invalid value for {0}. Allowed values are '{1}' or '{2}'", nameof(ValidateOpenApiTask.DevelopmentMode), ContractFirst, CodeFirst);
this.Log.LogError("Invalid value for {0}. Allowed values are '{1}' or '{2}'", nameof(ValidateOpenApiTask.OpenApiDevelopmentMode), ContractFirst, CodeFirst);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<Target Name="ValidateOpenApi" DependsOnTargets="ResolveProjectReferences" AfterTargets="Build">
<PropertyGroup>
<!-- Set development mode: ContractFirst or CodeFirst -->
<DevelopmentMode Condition="'$(DevelopmentMode)' == ''">ContractFirst</DevelopmentMode>
<OpenApiDevelopmentMode Condition="'$(OpenApiDevelopmentMode)' == ''">ContractFirst</OpenApiDevelopmentMode>

<!-- When ContractFirst, validate if the provided specification files match the code -->
<ValidateCodeSync Condition="'$(ValidateCodeSync)' == ''">false</ValidateCodeSync>
<ValidateCodeSync Condition="'$(OpenApiCompareCodeAgainstSpecFile)' == ''">false</ValidateCodeSync>

<!-- The path of the ASP.NET Core project build output directory -->
<StartupAssemblyPath Condition="'$(StartupAssemblyPath)' == ''">$(MSBuildProjectDirectory)\$(OutputPath)</StartupAssemblyPath>
Expand Down Expand Up @@ -54,8 +54,8 @@
</PropertyGroup>

<ValidateOpenApiTask
DevelopmentMode="$(DevelopmentMode)"
ValidateCodeSync="$(ValidateCodeSync)"
OpenApiDevelopmentMode="$(OpenApiDevelopmentMode)"
OpenApiCompareCodeAgainstSpecFile="$(OpenApiCompareCodeAgainstSpecFile)"
StartupAssemblyPath="$(StartupAssemblyPath)"
OpenApiWebApiAssemblyPath="$(OpenApiWebApiAssemblyPath)"
OpenApiToolsDirectoryPath="$(OpenApiToolsDirectoryPath)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>
<OpenApiSwaggerDocumentNames>v1;v1-management</OpenApiSwaggerDocumentNames>
<RootNamespace>WebApi.MsBuild.SystemTest</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<DevelopmentMode>CodeFirst</DevelopmentMode>
<OpenApiDevelopmentMode>CodeFirst</OpenApiDevelopmentMode>
<OpenApiSwaggerDocumentNames>v1;v1-management</OpenApiSwaggerDocumentNames>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>
<OpenApiSwaggerDocumentNames>v1;v1-management</OpenApiSwaggerDocumentNames>
<RootNamespace>WebApi.MsBuild.SystemTest</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<DevelopmentMode>ContractFirst</DevelopmentMode>
<ValidateCodeSync>true</ValidateCodeSync>
<OpenApiDevelopmentMode>ContractFirst</OpenApiDevelopmentMode>
<OpenApiCompareCodeAgainstSpecFile>true</OpenApiCompareCodeAgainstSpecFile>
<OpenApiSwaggerDocumentNames>v1;v1-management</OpenApiSwaggerDocumentNames>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit c5f293f

Please sign in to comment.