-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for skipping deprecated endpoints
This commit adds a new test class, DeprecatedMultipleInterfaceByTagEndpointsSkippedTests, which verifies that deprecated endpoints tagged by interfaces are correctly skipped when generating code. This ensures that users do not unintentionally use deprecated API endpoints.
- Loading branch information
1 parent
1afd361
commit 6a90e55
Showing
1 changed file
with
124 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
src/Refitter.Tests/Examples/DeprecatedMultipleInterfaceByTagEndpointsSkippedTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using FluentAssertions; | ||
|
||
using Refitter.Core; | ||
using Refitter.Tests.Build; | ||
|
||
using Xunit; | ||
|
||
namespace Refitter.Tests.Examples; | ||
|
||
public class DeprecatedMultipleInterfaceByTagEndpointsSkippedTests | ||
{ | ||
private const string OpenApiSpec = @" | ||
openapi: '3.0.0' | ||
paths: | ||
/foo/{id}: | ||
get: | ||
tags: | ||
- 'Foo' | ||
operationId: 'Get foo details' | ||
description: 'Get the details of the specified foo' | ||
parameters: | ||
- in: 'path' | ||
name: 'id' | ||
description: 'Foo ID' | ||
required: true | ||
schema: | ||
type: 'string' | ||
responses: | ||
'200': | ||
description: 'successful operation' | ||
/foo: | ||
get: | ||
tags: | ||
- 'Foo' | ||
operationId: 'Get all foos' | ||
description: 'Get all foos' | ||
responses: | ||
'200': | ||
description: 'successful operation' | ||
/bar: | ||
get: | ||
tags: | ||
- 'Bar' | ||
operationId: 'Get all bars' | ||
description: 'Get all bars' | ||
deprecated: true | ||
responses: | ||
'200': | ||
description: 'successful operation' | ||
/bar/{id}: | ||
get: | ||
tags: | ||
- 'Bar' | ||
operationId: 'Get bar details' | ||
description: 'Get the details of the specified bar' | ||
deprecated: true | ||
responses: | ||
'200': | ||
description: 'successful operation' | ||
"; | ||
|
||
[Fact] | ||
public async Task Can_Generate_Code() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_NotContain_Obsolete_Attribute() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().NotContain("[System.Obsolete]"); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_NotContain_GetAllBars() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().NotContain("GetAllBarsEndpoint"); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_NotContain_GetBarDetails() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().NotContain("GetBarDetailsEndpoint"); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Build_Generated_Code() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
BuildHelper | ||
.BuildCSharp(generateCode) | ||
.Should() | ||
.BeTrue(); | ||
} | ||
|
||
private static async Task<string> GenerateCode() | ||
{ | ||
var swaggerFile = await CreateSwaggerFile(OpenApiSpec); | ||
var settings = new RefitGeneratorSettings | ||
{ | ||
OpenApiPath = swaggerFile, | ||
GenerateDeprecatedOperations = false, | ||
MultipleInterfaces = MultipleInterfaces.ByTag, | ||
}; | ||
|
||
var sut = await RefitGenerator.CreateAsync(settings); | ||
var generateCode = sut.Generate(); | ||
return generateCode; | ||
} | ||
|
||
private static async Task<string> CreateSwaggerFile(string contents) | ||
{ | ||
var filename = $"{Guid.NewGuid()}.yml"; | ||
var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
Directory.CreateDirectory(folder); | ||
var swaggerFile = Path.Combine(folder, filename); | ||
await File.WriteAllTextAsync(swaggerFile, contents); | ||
return swaggerFile; | ||
} | ||
} |