Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for BaseUrl in C# base class not being used #4880

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 8 additions & 2 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
{{ ClientClassAccessModifier }} partial class {{ Class }} {% if HasBaseType %}: {% endif %}{% if HasBaseClass %}{{ BaseClass }}{% if GenerateClientInterfaces %}, {% endif %}{% endif %}{% if GenerateClientInterfaces %}I{{ Class }}{% endif %}
{
{% 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 Expand Up @@ -34,7 +36,7 @@
public {{ Class }}({{ constructorParameters }}){% if HasConfigurationClass and HasBaseClass -%}{{ " : base(configuration)"}}{% endif %}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
{% if UseBaseUrl -%}
{% if UseBaseUrl and HasBaseClass == false and GenerateBaseUrlProperty == true -%}
{% if HasBaseUrl -%}
{% if GenerateBaseUrlProperty -%}
BaseUrl = "{{ BaseUrl }}";
Expand Down Expand Up @@ -290,7 +292,11 @@
{% endif -%}

var urlBuilder_ = new System.Text.StringBuilder();
{% if UseBaseUrl %}if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);{% endif %}
{% if HasBaseClass == true and GenerateBaseUrlProperty == false -%}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Not using UseBaseUrl anymore...

if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
{% else -%}
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
{% endif -%}
// Operation Path: "{{ operation.Path }}"
{% if operation.Path contains "{" -%}
{% assign pathParts = operation.Path | split: "{" -%}
Expand Down
Loading