Skip to content

Commit

Permalink
Sanitize BaseUrl on write. (#4541)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado authored Oct 31, 2023
1 parent a099ebc commit 5dbeb3f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
15 changes: 12 additions & 3 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{ ClientClassAccessModifier }} partial class {{ Class }} {% if HasBaseType %}: {% endif %}{% if HasBaseClass %}{{ BaseClass }}{% if GenerateClientInterfaces %}, {% endif %}{% endif %}{% if GenerateClientInterfaces %}I{{ Class }}{% endif %}
{
{% if UseBaseUrl and GenerateBaseUrlProperty -%}
private string _baseUrl = "{{ BaseUrl }}";
private string _baseUrl;
{% endif -%}
{% if InjectHttpClient -%}
private {{ HttpClientType }} _httpClient;
Expand Down Expand Up @@ -35,6 +35,9 @@
{% else -%}
public {{ Class }}()
{
{% endif -%}
{% if UseBaseUrl and GenerateBaseUrlProperty -%}
BaseUrl = "{{ BaseUrl }}";
{% endif -%}
{% template Client.Class.Constructor %}
}
Expand All @@ -58,7 +61,12 @@
public string BaseUrl
{
get { return _baseUrl; }
set { _baseUrl = value; }
set
{
_baseUrl = value;
if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
baseUrl += '/';

This comment has been minimized.

Copy link
@MaximeMorin-Devolutions

MaximeMorin-Devolutions Nov 5, 2023

@paulomorgado When I generated and tested, baseUrl did not exists here. Did you mean _baseUrl? If so, there were a couple of places, where I had to make the change.

}
}

{% endif -%}
Expand Down Expand Up @@ -132,7 +140,8 @@

{% endif -%}
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append({% if UseBaseUrl %}BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/{% else %}"{% endif %}{{ operation.Path }}{% if operation.HasQueryParameters %}?{% endif %}");
{% if UseBaseUrl %}if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);{% endif %}
urlBuilder_.Append("{{ operation.Path }}{% if operation.HasQueryParameters %}?{% endif %}");
{% for parameter in operation.PathParameters -%}
{% if parameter.IsOptional -%}
if ({{ parameter.VariableName }} != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Client
{
private string _baseUrl = "";
private string _baseUrl;
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

public Client(string baseUrl, System.Net.Http.HttpClient httpClient)
{
BaseUrl = baseUrl;
_httpClient = httpClient;
BaseUrl = "";

This comment has been minimized.

Copy link
@MaximeMorin-Devolutions

MaximeMorin-Devolutions Nov 5, 2023

@paulomorgado This new line caused an issue for me. It's faily weird that it was correctly initialized 2 lines up and then reset to empty. Is this really expected?

}

private static Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
Expand All @@ -42,7 +43,12 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
set { _baseUrl = value; }
set
{
_baseUrl = value;
if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
baseUrl += '/';
}
}

protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }
Expand All @@ -64,7 +70,8 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<string> GetAsync(System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -142,7 +149,8 @@ namespace MyNamespace
throw new System.ArgumentNullException("b");

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/sum/{a}/{b}");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("sum/{a}/{b}");
urlBuilder_.Replace("{a}", System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Replace("{b}", System.Uri.EscapeDataString(ConvertToString(b, System.Globalization.CultureInfo.InvariantCulture)));

Expand Down Expand Up @@ -311,14 +319,15 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ExampleClient
{
private string _baseUrl = "";
private string _baseUrl;
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

public ExampleClient(string baseUrl, System.Net.Http.HttpClient httpClient)
{
BaseUrl = baseUrl;
_httpClient = httpClient;
BaseUrl = "";
}

private static Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
Expand All @@ -331,7 +340,12 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
set { _baseUrl = value; }
set
{
_baseUrl = value;
if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
baseUrl += '/';
}
}

protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }
Expand All @@ -353,7 +367,8 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<FileResponse> GetAsync(System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/examples");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("examples");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down
29 changes: 22 additions & 7 deletions src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Client
{
private string _baseUrl = "";
private string _baseUrl;
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

public Client(string baseUrl, System.Net.Http.HttpClient httpClient)
{
BaseUrl = baseUrl;
_httpClient = httpClient;
BaseUrl = "";
}

private static Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
Expand All @@ -42,7 +43,12 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
set { _baseUrl = value; }
set
{
_baseUrl = value;
if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
baseUrl += '/';
}
}

protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }
Expand All @@ -64,7 +70,8 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<string> GetAsync(System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -142,7 +149,8 @@ namespace MyNamespace
throw new System.ArgumentNullException("b");

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/sum/{a}/{b}");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("sum/{a}/{b}");
urlBuilder_.Replace("{a}", System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Replace("{b}", System.Uri.EscapeDataString(ConvertToString(b, System.Globalization.CultureInfo.InvariantCulture)));

Expand Down Expand Up @@ -311,14 +319,15 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ExampleClient
{
private string _baseUrl = "";
private string _baseUrl;
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

public ExampleClient(string baseUrl, System.Net.Http.HttpClient httpClient)
{
BaseUrl = baseUrl;
_httpClient = httpClient;
BaseUrl = "";
}

private static Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
Expand All @@ -331,7 +340,12 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
set { _baseUrl = value; }
set
{
_baseUrl = value;
if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
baseUrl += '/';
}
}

protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }
Expand All @@ -353,7 +367,8 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<FileResponse> GetAsync(System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/examples");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("examples");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down
29 changes: 22 additions & 7 deletions src/NSwag.Sample.NET80Minimal/GeneratedClientsCs.gen
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Client
{
private string _baseUrl = "";
private string _baseUrl;
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

public Client(string baseUrl, System.Net.Http.HttpClient httpClient)
{
BaseUrl = baseUrl;
_httpClient = httpClient;
BaseUrl = "";
}

private static Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
Expand All @@ -42,7 +43,12 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
set { _baseUrl = value; }
set
{
_baseUrl = value;
if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
baseUrl += '/';
}
}

protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }
Expand All @@ -64,7 +70,8 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<string> GetAsync(System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -142,7 +149,8 @@ namespace MyNamespace
throw new System.ArgumentNullException("b");

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/sum/{a}/{b}");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("sum/{a}/{b}");
urlBuilder_.Replace("{a}", System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Replace("{b}", System.Uri.EscapeDataString(ConvertToString(b, System.Globalization.CultureInfo.InvariantCulture)));

Expand Down Expand Up @@ -311,14 +319,15 @@ namespace MyNamespace
[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ExampleClient
{
private string _baseUrl = "";
private string _baseUrl;
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

public ExampleClient(string baseUrl, System.Net.Http.HttpClient httpClient)
{
BaseUrl = baseUrl;
_httpClient = httpClient;
BaseUrl = "";
}

private static Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
Expand All @@ -331,7 +340,12 @@ namespace MyNamespace
public string BaseUrl
{
get { return _baseUrl; }
set { _baseUrl = value; }
set
{
_baseUrl = value;
if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
baseUrl += '/';
}
}

protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }
Expand All @@ -353,7 +367,8 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<FileResponse> GetAsync(System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/examples");
if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);
urlBuilder_.Append("examples");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down

0 comments on commit 5dbeb3f

Please sign in to comment.