Skip to content

Commit

Permalink
Avoid generating unused _baseUrl (C# gen)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Feb 4, 2024
1 parent 2add8c6 commit 202f3c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[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 or GenerateBaseUrlProperty -%}
{% if UseBaseUrl -%}
#pragma warning disable 8618
private string _baseUrl;
#pragma warning restore 8618
Expand Down
22 changes: 13 additions & 9 deletions src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen
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 All @@ -45,6 +46,7 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
set
{
_baseUrl = value;
Expand Down Expand Up @@ -81,7 +83,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 +164,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 +246,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 +327,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 +499,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 All @@ -519,6 +522,7 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
set
{
_baseUrl = value;
Expand Down Expand Up @@ -555,7 +559,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
22 changes: 13 additions & 9 deletions src/NSwag.Sample.NET80Minimal/GeneratedClientsCs.gen
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 All @@ -45,6 +46,7 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
set
{
_baseUrl = value;
Expand Down Expand Up @@ -81,7 +83,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 +164,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 +246,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 +327,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 +499,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 All @@ -519,6 +522,7 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
set
{
_baseUrl = value;
Expand Down Expand Up @@ -555,7 +559,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

5 comments on commit 202f3c2

@alexcheveau
Copy link

@alexcheveau alexcheveau commented on 202f3c2 Feb 5, 2024

Choose a reason for hiding this comment

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

Why change BaseUrl to _baseUrl in the urlBuilder_?
I use GenerateBaseUrlProperty=false in my project and have base proxy classes that return the BaseUrl :(

@olegd-superoffice
Copy link
Contributor

Choose a reason for hiding this comment

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

@alexcheveau Is this the same problem as in #4705 ?

@alexcheveau
Copy link

Choose a reason for hiding this comment

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

@olegd-superoffice yeah looks like

My case is:
useBaseUrl=true
generateBaseUrlProperty=false
I have a base class that implements BaseURL and return the baseurl to be used in urlBuilder but now it's ignored. The proxy is using directly _baseUrl

I totally agree on this:
In 14.0.3 code is getting compiled again but works differently - BaseUrl property is not generated and is not used at all. Instead private string _baseUrl field is assigned from a host value defined in OpenAPI document. The only workaround I could find to change _baseUrl value is to put some weird logic into override of PrepareRequest method.

@jonmarozick
Copy link

Choose a reason for hiding this comment

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

I also have the same issue. I use useBaseUrl=true and generateBaseUrlProperty=false. My base class has a BaseUrl property that I set via config. I relied on that in the generated code.

@KirkMunroSagent
Copy link

Choose a reason for hiding this comment

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

I'm in a different boat, but 14.0.3 was a breaking change for us. We invoke nswag with useBaseUrl=false. I don't set generateBaseUrlProperty at all. This was working fine. Upon upgrading to 14.0.3, now the generated library is generating errors (we have warnings as errors turned on in all projects), because the client class is generated with the following new code that wasn't included in the client before recent changes:

        #pragma warning disable 8618
        private string _baseUrl;
        #pragma warning restore 8618

So now we either need to rollback to 14.0.2, which is a minor annoyance because it means publishing a new library to our internal NuGet server since we centralize our nswag code (we just updated it to 14.0.3 since it had been a month, figured the dust had settled), or manually delete these properties that are not used while waiting for a fix for this breaking change. This commit was completed a month ago, and it looks like it would fix the issue. When is 14.0.4 expected to be released @RicoSuter ?

Please sign in to comment.