Skip to content

Commit

Permalink
Sanitize BaseUrl on write. (RicoSuter#4541)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado authored and lahma committed Jan 20, 2024
1 parent ea9d813 commit 7007775
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 += '/';
}
}

{% 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 = "";
}

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 7007775

Please sign in to comment.