Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,6 @@ ASALocalRun/
# BeatPulse healthcheck temp database
healthchecksdb

/GenerateAspNetCoreClient/Properties/debug-paths.txt
/GenerateAspNetCoreClient/Properties/debug-paths.txt

*.received.*
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
<PackageReference Include="Namotion.Reflection" Version="3.2.0" />
<PackageReference Include="Namotion.Reflection" Version="3.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions GenerateAspNetCoreClient.Command/GenerateClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ private static IEnumerable<string> GetImportedNamespaces(Client clientModel, Has
var namespaces = GetNamespaces(clientModel.EndpointMethods, ambiguousTypes)
.Concat(options.AdditionalNamespaces);

if (options.AddCancellationTokenParameters)
namespaces = namespaces.Append("System.Threading");

namespaces = namespaces
.OrderByDescending(ns => ns.StartsWith("System"))
.ThenBy(ns => ns);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<auto-generated />
//<auto-generated />

using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//<auto-generated />

using System.Threading;
using System.Threading.Tasks;
using Refit;
using TestWebApi.Models;

namespace Test.Name.Space
{
public partial interface ITestWebApiMinimalApiApi
{
[Post("/weather-forecast")]
Task<IApiResponse> PostWeatherForecast([Body] WeatherForecast forecast, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//<auto-generated />

using System.Threading;
using System.Threading.Tasks;
using Refit;
using TestWebApi.Models;

namespace Test.Name.Space
{
public partial interface IWeatherForecastApi
{
[Get("/weather-forecast")]
Task<IApiResponse<WeatherForecastRecord[]>> GetWeatherForecast(CancellationToken cancellationToken = default);

[Get("/weather-forecast/with-name")]
Task<IApiResponse<WeatherForecastRecord>> GetSomeWeather(int days, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<auto-generated />
//<auto-generated />

using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<auto-generated />
//<auto-generated />

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<auto-generated />
//<auto-generated />

using System.Threading.Tasks;
using Refit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<auto-generated />
//<auto-generated />

using System.Threading.Tasks;
using Refit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<auto-generated />
//<auto-generated />

using System.Threading.Tasks;
using Refit;
Expand Down
89 changes: 24 additions & 65 deletions Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading.Tasks;
using DotNet.Cli.Build;
using GenerateAspNetCoreClient.Options;
using NUnit.Framework;
using VerifyNUnit;

namespace GenerateAspNetCoreClient.Tests
{
[NonParallelizable]
public class ClientGenerationTests
{
private static readonly string _snapshotsPath = Path.Combine("..", "..", "..", "__snapshots__", "{0}");

private static readonly string _inputPath = Path.Combine("..", "..", "..", "..", "{0}", "{0}.csproj");
private static readonly string _outPath = Path.Combine("..", "..", "..", "..", "OutputTest", "Client");
private static readonly string _outProjectPath = Path.Combine("..", "..", "..", "..", "OutputTest", "OutputProject.csproj");

private static readonly bool _regenerateSnapshots = false;

[TearDown]
public void CleanOutput()
{
Expand All @@ -27,24 +23,23 @@ public void CleanOutput()
[TestCase("TestWebApi.Controllers")]
[TestCase("TestWebApi.Versioning")]
[TestCase("TestWebApi.MinimalApi")]
public void GenerationTest(string testProjectName)
public async Task GenerationTest(string testProjectName)
{
var options = new GenerateClientOptions
{
InputPath = string.Format(_inputPath, testProjectName),
OutPath = _outPath,
Namespace = "Test.Name.Space",
};

Program.CreateClient(options);

Assert.That(() => Project.FromPath(_outProjectPath).Build(), Throws.Nothing);
AssertSnapshotMatch(testProjectName);
await Verifier.VerifyDirectory(_outPath);
}


[Test]
public void GenerationTest_UseApiResponses()
public async Task GenerationTest_UseApiResponses()
{
var options = new GenerateClientOptions
{
Expand All @@ -57,61 +52,25 @@ public void GenerationTest_UseApiResponses()
Program.CreateClient(options);

Assert.That(() => Project.FromPath(_outProjectPath).Build(), Throws.Nothing);
AssertSnapshotMatch("TestWebApi.Controllers.UseApiResponses");
}

private static void RegenerateSnapshots(string testProjectName)
{
var snapshotsPath = string.Format(_snapshotsPath, testProjectName);

if (Directory.Exists(snapshotsPath))
{
Directory.Delete(snapshotsPath, recursive: true);
}

Directory.CreateDirectory(snapshotsPath);

var generatedFiles = Directory.EnumerateFiles(_outPath, "*", new EnumerationOptions { RecurseSubdirectories = true });

foreach (var generatedFile in generatedFiles)
{
var relativePath = Path.GetRelativePath(_outPath, generatedFile);
File.Move(generatedFile, Path.Combine(snapshotsPath, relativePath + ".snap"));
}

throw new Exception("Don't forget to disable snapshot regeneration.");
await Verifier.VerifyDirectory(_outPath);
}

private static void AssertSnapshotMatch(string testProjectName)

[Test]
public async Task GenerationTest_UseApiResponses_UseCancellationTokens()
{
if (_regenerateSnapshots)
{
RegenerateSnapshots(testProjectName);
}

var snapshotsPath = string.Format(_snapshotsPath, testProjectName);
var generatedFiles = Directory.EnumerateFiles(_outPath, "*", new EnumerationOptions { RecurseSubdirectories = true });

foreach (var generatedFile in generatedFiles)
{
var relativePath = Path.GetRelativePath(_outPath, generatedFile);
var snapshotPath = Path.Combine(snapshotsPath, relativePath + ".snap");

Assert.That(snapshotPath, Does.Exist, $"Unexpected file generated ({relativePath})");

var actualContent = File.ReadAllText(generatedFile);
var expectedContent = File.ReadAllText(snapshotPath);

Assert.That(actualContent, Is.EqualTo(expectedContent).Using<string>(IgnoreLineEndings),
$"File mismatch (${relativePath})");
}

static bool IgnoreLineEndings(string s1, string s2)
var options = new GenerateClientOptions
{
s1 = Regex.Replace(s1, "(\r|\n|\r\n)", "\r");
s2 = Regex.Replace(s2, "(\r|\n|\r\n)", "\r");
return s1 == s2;
}
InputPath = string.Format(_inputPath,"TestWebApi.MinimalApi"),
UseApiResponses = true,
AddCancellationTokenParameters = true,
OutPath = _outPath,
Namespace = "Test.Name.Space",
};

Program.CreateClient(options);

Assert.That(() => Project.FromPath(_outProjectPath).Build(), Throws.Nothing);
await Verifier.VerifyDirectory(_outPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0">
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Verify.NUnit" Version="29.5.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -27,4 +28,11 @@
<ProjectReference Include="..\TestWebApi.MinimalApi\TestWebApi.MinimalApi.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="**\*.verified.cs" />
<None Include="**\*.verified.cs" />
<Compile Remove="**\*.received.cs" />
<None Include="**\*.received.cs" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Tests/OutputTest/OutputProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<ItemGroup>
<PackageReference Include="refit" Version="8.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="9.0.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="9.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="9.0.4" />
<PackageReference Include="System.Text.Encodings.Web" Version="9.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestWebApi.Controllers/TestWebApi.Controllers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Tests/TestWebApi.MinimalApi/TestWebApi.MinimalApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="8.0.11">
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestWebApi.Versioning/TestWebApi.Versioning.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
</ItemGroup>
Expand Down
Loading