Skip to content

Commit

Permalink
Fix missing _baseUrl field and BaseUrl property
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Feb 2, 2024
1 parent 116c636 commit be42e58
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 85 deletions.
24 changes: 24 additions & 0 deletions src/NSwag.CodeGeneration.CSharp.Tests/CSharpClientSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ public async Task When_UseHttpRequestMessageCreationMethod_is_set_then_CreateReq
Assert.Contains("var request_ = await CreateHttpRequestMessageAsync(cancellationToken).ConfigureAwait(false)", code);
}

[Fact]
public async Task WhenUsingBaseUrl_ButNoProperty_ThenPropertyIsNotUsedAndFieldIsGenerated()
{
// 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
});

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

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

[Fact]
public async Task When_parameter_name_is_reserved_keyword_then_it_is_appended_with_at()
{
Expand Down
35 changes: 18 additions & 17 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
[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 GenerateBaseUrlProperty -%}
#pragma warning disable 8618 // Set by constructor via BaseUrl property
{% if UseBaseUrl or GenerateBaseUrlProperty -%}
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618

{% endif -%}
{% if InjectHttpClient -%}
private {{ HttpClientType }} _httpClient;
Expand Down Expand Up @@ -287,7 +288,7 @@
{% endif -%}

var urlBuilder_ = new System.Text.StringBuilder();
{% if UseBaseUrl %}if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);{% endif %}
{% if UseBaseUrl %}if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);{% endif %}
// Operation Path: "{{ operation.Path }}"
{% if operation.Path contains "{" -%}
{% assign pathParts = operation.Path | split: "{" -%}
Expand All @@ -299,12 +300,12 @@
{% if parameter.Name == pathParameterParts[0] -%}
{% if parameter.IsOptional -%}
{% assign pathParameterFound = false -%}
if ({{ parameter.VariableName }} != null)
{
{% template Client.Class.PathParameter %}
}
else
if (urlBuilder_.Length > 0) urlBuilder_.Length--;
if ({{ parameter.VariableName }} != null)
{
{% template Client.Class.PathParameter %}
}
else
if (urlBuilder_.Length > 0) urlBuilder_.Length--;
{% else -%}
{% template Client.Class.PathParameter %}
{% endif -%}
Expand Down Expand Up @@ -333,18 +334,18 @@
{% endunless -%}
{% endif -%}
{% if operation.HasQueryParameters -%}
urlBuilder_.Append('?');
urlBuilder_.Append('?');
{% for parameter in operation.QueryParameters -%}
{% if parameter.IsOptional -%}
if ({{ parameter.VariableName }} != null)
{
{% template Client.Class.QueryParameter %}
}
if ({{ parameter.VariableName }} != null)
{
{% template Client.Class.QueryParameter %}
}
{% else -%}
{% template Client.Class.QueryParameter %}
{% template Client.Class.QueryParameter %}
{% endif -%}
{% endfor -%}
urlBuilder_.Length--;
urlBuilder_.Length--;
{% endif -%}

{% if GeneratePrepareRequestAndProcessResponseAsAsyncMethods %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "")]
public partial class Client
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618

private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -81,7 +82,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: ""

PrepareRequest(client_, request_, urlBuilder_);
Expand Down Expand Up @@ -162,7 +163,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "sum/{a}/{b}"
urlBuilder_.Append("sum/");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -244,7 +245,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "abs({a})"
urlBuilder_.Append("abs(");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -325,7 +326,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "id:{id}"
urlBuilder_.Append("id:");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -497,9 +498,10 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "")]
public partial class ExampleClient
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618

private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -555,7 +557,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/octet-stream"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "examples"
urlBuilder_.Append("examples");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "")]
public partial class Client
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618

private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -81,7 +82,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: ""

PrepareRequest(client_, request_, urlBuilder_);
Expand Down Expand Up @@ -162,7 +163,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "sum/{a}/{b}"
urlBuilder_.Append("sum/");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -244,7 +245,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "abs({a})"
urlBuilder_.Append("abs(");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -325,7 +326,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "id:{id}"
urlBuilder_.Append("id:");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -497,9 +498,10 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "")]
public partial class ExampleClient
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618

private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -555,7 +557,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/octet-stream"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "examples"
urlBuilder_.Append("examples");

Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Sample.NET60/openapi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"x-generator": "NSwag v14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))",
"x-generator": "NSwag v14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))",
"openapi": "3.0.0",
"info": {
"title": "My Title",
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Sample.NET60Minimal/openapi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"x-generator": "NSwag v14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))",
"x-generator": "NSwag v14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))",
"openapi": "3.0.0",
"info": {
"title": "Minimal API",
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Sample.NET70/openapi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"x-generator": "NSwag v14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))",
"x-generator": "NSwag v14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))",
"openapi": "3.0.0",
"info": {
"title": "My Title",
Expand Down
32 changes: 17 additions & 15 deletions src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// Generated using the NSwag toolchain v14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------

Expand All @@ -20,12 +20,13 @@ namespace MyNamespace
{
using System = global::System;

[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Client
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618

private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -81,7 +82,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: ""

PrepareRequest(client_, request_, urlBuilder_);
Expand Down Expand Up @@ -162,7 +163,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "sum/{a}/{b}"
urlBuilder_.Append("sum/");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -244,7 +245,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "abs({a})"
urlBuilder_.Append("abs(");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -325,7 +326,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "id:{id}"
urlBuilder_.Append("id:");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture)));
Expand Down Expand Up @@ -494,12 +495,13 @@ namespace MyNamespace
}
}

[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ExampleClient
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618

private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -555,7 +557,7 @@ namespace MyNamespace
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/octet-stream"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "examples"
urlBuilder_.Append("examples");

Expand Down Expand Up @@ -723,7 +725,7 @@ namespace MyNamespace



[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class FileResponse : System.IDisposable
{
private System.IDisposable _client;
Expand Down Expand Up @@ -760,7 +762,7 @@ namespace MyNamespace
}


[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ApiException : System.Exception
{
public int StatusCode { get; private set; }
Expand All @@ -783,7 +785,7 @@ namespace MyNamespace
}
}

[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.1.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.2.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ApiException<TResult> : ApiException
{
public TResult Result { get; private set; }
Expand Down
Loading

0 comments on commit be42e58

Please sign in to comment.