Skip to content

Commit

Permalink
Fix for BaseUrl in C# base class not being used - Correction
Browse files Browse the repository at this point in the history
Fixed If in template and added test case for when there is a base class
  • Loading branch information
soligen2010 committed May 3, 2024
1 parent 27c2206 commit d9b7e32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/NSwag.CodeGeneration.CSharp.Tests/CSharpClientSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ public async Task WhenUsingBaseUrl_ButNoProperty_ThenPropertyIsNotUsedAndFieldIs
Assert.Contains("string _baseUrl", code);
}

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

var document = await swaggerGenerator.GenerateForControllerAsync<FooController>();
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
{
UseBaseUrl = true,
GenerateBaseUrlProperty = false,
ClientBaseClass = "MyBaseClass"
});

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

// Assert
Assert.Contains("BaseUrl", code);
Assert.DoesNotContain("string _baseUrl", code);
}

[Fact]
public async Task When_parameter_name_is_reserved_keyword_then_it_is_appended_with_at()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
{{ ClientClassAccessModifier }} partial class {{ Class }} {% if HasBaseType %}: {% endif %}{% if HasBaseClass %}{{ BaseClass }}{% if GenerateClientInterfaces %}, {% endif %}{% endif %}{% if GenerateClientInterfaces %}I{{ Class }}{% endif %}
{
{% if UseBaseUrl and HasBaseClass == false and GenerateBaseUrlProperty == true -%}
{% if UseBaseUrl -%}
{% if HasBaseClass == false or GenerateBaseUrlProperty == true -%}
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618

{% endif -%}
{% endif -%}
{% if InjectHttpClient -%}
private {{ HttpClientType }} _httpClient;
Expand Down

0 comments on commit d9b7e32

Please sign in to comment.