Skip to content

Commit

Permalink
Settings: ExcludeDeprecated + ExcludeByPathRegex: Tests for typescrip…
Browse files Browse the repository at this point in the history
…t added
  • Loading branch information
DanielKStan committed Dec 30, 2024
1 parent 481e230 commit 9862560
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public class CSharpClientSettingsTests
{
public class FooController : Controller
{
#pragma warning disable S1133 // Deprecated code should be removed
[Obsolete("Testing generation of obsolete endpoints")]
#pragma warning restore S1133 // Deprecated code should be removed
public object GetPerson(bool @override = false)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NJsonSchema.CodeGeneration.TypeScript;
using NSwag.Generation.WebApi;
using System.Runtime.Serialization;
using Xunit;
using NJsonSchema.NewtonsoftJson.Converters;
using NJsonSchema;
using NJsonSchema.NewtonsoftJson.Generation;
using static NSwag.CodeGeneration.TypeScript.Tests.OperationParameterTests;

namespace NSwag.CodeGeneration.TypeScript.Tests
{
public class TypeScriptClientSettingTests
{
public class FooController
{
[Route("test")]
public string Test(int a, int? b)
{
return null;
}

[Obsolete("Obsolete endpoint for testing")]
[Route("obsoleteEndpoint")]
public string ObsoleteEndpoint(int a, int? b)
{
return null;
}
}

[Fact]
public async Task When_depreacted_endpoints_are_excluded_the_client_will_not_generate_these_endpoint()
{
// Arrange
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings()
});

var document = await swaggerGenerator.GenerateForControllerAsync<FooController>();
var generator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
{
ExcludeDeprecated = true
});

// Act
var code = generator.GenerateFile();

// Assert
Assert.DoesNotContain("obsoleteEndpoint", code);
Assert.DoesNotContain("deprecated", code);
Assert.Contains("test", code); // contains other endpoint
}

[Fact]
public async Task When_regex_is_set_to_excluded_endpoints_the_client_will_not_generate_these_endpoint()
{
// Arrange
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings()
});

var document = await swaggerGenerator.GenerateForControllerAsync<FooController>();
var generator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
{
ExcludeByPathRegex = "test"
});

// Act
var code = generator.GenerateFile();

// Assert
Assert.DoesNotContain("foo", code);
Assert.Contains("obsoleteEndpoint", code); // contains other endpoint
Assert.Contains("deprecated", code);
}
}
}
5 changes: 0 additions & 5 deletions src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,10 @@ public virtual string GenerateControllerName(string controllerName)
/// <summary>Gets or sets the name of the response class (supports the '{controller}' placeholder).</summary>
public string ResponseClass { get; set; }

//-------
/// <summary>Gets or sets the value indicating if deprecated endpoints shall be rendered</summary>
public bool ExcludeDeprecated { get; set; }

/// <summary>Gets or sets the regular expression to indicate for which path's client code should be generated (null/empty means all)</summary>
public string ExcludeByPathRegex { get; set; }




}
}

0 comments on commit 9862560

Please sign in to comment.