Skip to content
Permalink
Browse files

Add BasePath override config option for C# Controller (#2439)

* Added an annotations extension template at controller class level

* Add BasePath override config option for C# Controller.

To separate config from code-gen, a base path config variable is added so the base path can be overridden.

This can be useful when:

  - You have a virtual directory that you don't want in your routes.

  - You have a difference in the paths of your environments.

* Add BasePath override config option for C# Controller.

Change BasePath evalutation to use string.IsNullOrEmpty.

* Add BasePath override config option for C# Controller.

Change BasePath evalutation to use string.IsNullOrEmpty.
  • Loading branch information...
basvandrongelen authored and RicoSuter committed Oct 8, 2019
1 parent 98a3cf4 commit b9780fe83648ebdbdf15d430ef54ac2f278297cc
@@ -71,3 +71,4 @@ src/packages/**

.editorconfig
/samples/packages
/src/NSwag.Console/Properties/launchSettings.json
@@ -0,0 +1,89 @@
using System.Threading.Tasks;
using Xunit;

namespace NSwag.CodeGeneration.CSharp.Tests
{
public class ControllerGenerationBasePathTests
{
[Fact]
public async Task When_custom_BasePath_is_not_specified_then_the_BasePath_from_document_is_used_as_Route()
{
//// Arrange
var document = await OpenApiDocument.FromJsonAsync(_swagger);

//// Act
var settings = new CSharpControllerGeneratorSettings();
var generator = new CSharpControllerGenerator(document, settings);
var code = generator.GenerateFile();

//// Assert
Assert.Contains("Route(\"virtual_directory/v1\")]", code);
}

[Fact]
public async Task When_custom_BasePath_is_specified_then_that_is_used_as_Route()
{
//// Arrange
var document = await OpenApiDocument.FromJsonAsync(_swagger);

//// Act
var settings = new CSharpControllerGeneratorSettings { BasePath = "v1" };
var generator = new CSharpControllerGenerator(document, settings);
var code = generator.GenerateFile();

//// Assert
Assert.Contains("Route(\"v1\")]", code);
}


private readonly string _swagger = @"{
""swagger"" : ""2.0"",
""info"" : {
""version"" : ""1.0.2"",
""title"" : ""Test API""
},
""host"" : ""localhost:8080"",
""basePath"" : ""/virtual_directory/v1"",
""tags"" : [ {
""name"" : ""api""
} ],
""schemes"" : [ ""http"" ],
""paths"" : {
""/removeElement"" : {
""delete"" : {
""tags"" : [ ""api"" ],
""summary"" : ""Removes elements"",
""description"" : ""Removes elements"",
""operationId"" : ""removeElement"",
""consumes"" : [ ""application/json"" ],
""produces"" : [ ""application/json"" ],
""parameters"" : [ {
""name"" : ""X-User"",
""in"" : ""header"",
""description"" : ""User identifier"",
""required"" : true,
""type"" : ""string""
}, {
""name"" : ""elementId"",
""in"" : ""query"",
""description"" : ""The ids of existing elements that should be removed"",
""required"" : false,
""type"" : ""array"",
""items"" : {
""type"" : ""integer"",
""format"" : ""int64""
},
} ],
""responses"" : {
""default"" : {
""description"" : ""successful operation""
}
}
}
}
},
""definitions"" : { }
}
";
}
}
@@ -60,5 +60,8 @@ public string GetRouteName(OpenApiOperation operation)

/// <summary>Gets or sets a value indicating whether ASP.Net Core (2.1) ActionResult type is used (default: false).</summary>
public bool UseActionResultType { get; set; }

/// <summary>Gets or sets the base path on which the API is served, which is relative to the Host.</summary>
public string BasePath { get; set; }
}
}
@@ -69,7 +69,7 @@ public class CSharpControllerTemplateModel : CSharpTemplateModelBase
public bool HasBasePath => !string.IsNullOrEmpty(BasePath);

/// <summary>Gets or sets the base path.</summary>
public string BasePath => _document.BasePath?.TrimStart('/');
public string BasePath => string.IsNullOrEmpty(_settings.BasePath) ? _document.BasePath?.TrimStart('/') : _settings.BasePath.TrimStart('/');

/// <summary>Gets a value indicating whether to generate optional parameters.</summary>
public bool GenerateOptionalParameters => _settings.GenerateOptionalParameters;
@@ -78,6 +78,13 @@ public CSharpControllerRouteNamingStrategy RouteNamingStrategy
set { Settings.RouteNamingStrategy = value; }
}

[Argument(Name = "BasePath", Description = "The Base path on which the API is served, which is relative to the Host", IsRequired = false)]
public string BasePath
{
get { return Settings.BasePath; }
set { Settings.BasePath = value; }
}

public override async Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
{
var code = await RunAsync();
@@ -97,6 +97,9 @@

<GroupBox Header="Web API Controller" Margin="0,0,0,12">
<StackPanel Margin="4,8,4,-8">
<TextBlock Text="BasePath override (RoutePrefix)" FontWeight="Bold" Margin="0,0,0,6" />
<TextBox Text="{Binding Command.BasePath, Mode=TwoWay}" Margin="0,0,0,12" />

<TextBlock Text="Controller Class Name" FontWeight="Bold" Margin="0,0,0,6" />
<TextBox Text="{Binding Command.ClassName, Mode=TwoWay}" Margin="0,0,0,12" />

0 comments on commit b9780fe

Please sign in to comment.
You can’t perform that action at this time.