Skip to content

Commit

Permalink
Add tests for skipping deprecated endpoints
Browse files Browse the repository at this point in the history
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
christianhelle committed Oct 5, 2023
1 parent 1afd361 commit 6a90e55
Showing 1 changed file with 124 additions and 0 deletions.
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;
}
}

0 comments on commit 6a90e55

Please sign in to comment.