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

Move argument validation exception to methods to allow JIT inlining. #4610

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task TestNoGuardForOptionalBodyParameter()
var code = codeGen.GenerateFile();

// Assert
Assert.DoesNotContain("throw new System.ArgumentNullException(\"body\")", code);
Assert.DoesNotContain("ThrowArgumentNullException(\"body\")", code);
}

[Fact]
Expand All @@ -67,8 +67,8 @@ public async Task TestNullableBodyWithAllowNullableBodyParameters()
var code = generator.GenerateFile();

// Assert
Assert.Contains("throw new System.ArgumentNullException(\"requiredBody\")", code);
Assert.DoesNotContain("throw new System.ArgumentNullException(\"notRequiredBody\")", code);
Assert.Contains("ThrowArgumentNullException(\"requiredBody\")", code);
Assert.DoesNotContain("ThrowArgumentNullException(\"notRequiredBody\")", code);
}

[Fact]
Expand All @@ -81,8 +81,8 @@ public async Task TestNullableBodyWithoutAllowNullableBodyParameters()
var code = generator.GenerateFile();

// Assert
Assert.Contains("throw new System.ArgumentNullException(\"requiredBody\")", code);
Assert.Contains("throw new System.ArgumentNullException(\"notRequiredBody\")", code);
Assert.Contains("ThrowArgumentNullException(\"requiredBody\")", code);
Assert.Contains("ThrowArgumentNullException(\"notRequiredBody\")", code);
}

private static async Task<CSharpClientGenerator> GenerateCode(bool allowNullableBodyParameters)
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.CodeGeneration.CSharp.Tests/FormParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void When_form_parameters_are_defined_then_MultipartFormDataContent_is_ge
// Assert
Assert.Contains("new System.Net.Http.MultipartFormDataContent", code);
Assert.Contains("if (foo != null)", code);
Assert.Contains("throw new System.ArgumentNullException(\"bar\");", code);
Assert.Contains("ThrowArgumentNullException(\"bar\");", code);
}

public class FileUploadController : Controller
Expand Down Expand Up @@ -128,7 +128,7 @@ public void When_form_parameters_are_defined_then_FormUrlEncodedContent_is_gener
// Assert
Assert.Contains("new System.Net.Http.FormUrlEncodedContent", code);
Assert.Contains("if (foo != null)", code);
Assert.Contains("throw new System.ArgumentNullException(\"bar\");", code);
Assert.Contains("ThrowArgumentNullException(\"bar\");", code);
}

// TODO: Implement for JQuery, AngularJS and Angular 2
Expand Down
14 changes: 8 additions & 6 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@
{% for parameter in operation.PathParameters -%}
{% if parameter.IsNullable == false and parameter.IsRequired -%}
if ({{ parameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ parameter.VariableName }}");
ThrowArgumentNullException("{{ parameter.VariableName }}");

{% endif -%}
{% endfor -%}
{% for parameter in operation.QueryParameters -%}
{% if parameter.IsNullable == false and parameter.IsRequired -%}
if ({{ parameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ parameter.VariableName }}");
ThrowArgumentNullException("{{ parameter.VariableName }}");

{% endif -%}
{% endfor -%}
{% if operation.HasContent and operation.ContentParameter.IsRequired -%}
if ({{ operation.ContentParameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ operation.ContentParameter.VariableName }}");
ThrowArgumentNullException("{{ operation.ContentParameter.VariableName }}");

{% endif -%}
{% if InjectHttpClient -%}
Expand All @@ -162,7 +162,7 @@
{% for parameter in operation.HeaderParameters %}
{% if parameter.IsRequired -%}
if ({{ parameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ parameter.VariableName }}");
ThrowArgumentNullException("{{ parameter.VariableName }}");
{% template Client.Class.HeaderParameter %}
{% else -%}
if ({{ parameter.VariableName }} != null)
Expand Down Expand Up @@ -204,7 +204,7 @@
if ({{ parameter.VariableName }} != null)
{% else -%}
if ({{ parameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ parameter.VariableName }}");
ThrowArgumentNullException("{{ parameter.VariableName }}");
else
{% endif -%}
keyValues_.Add(new System.Collections.Generic.KeyValuePair<string, string>("{{ parameter.Name }}", ConvertToString({{ parameter.VariableName }}, System.Globalization.CultureInfo.InvariantCulture)));
Expand All @@ -220,7 +220,7 @@
if ({{ parameter.VariableName }} != null)
{% else -%}
if ({{ parameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ parameter.VariableName }}");
ThrowArgumentNullException("{{ parameter.VariableName }}");
else
{% endif -%}
{
Expand Down Expand Up @@ -444,6 +444,8 @@
public string Text { get; }
}

private static void ThrowArgumentNullException(string parameterName) => throw new global::System.ArgumentNullException(parameterName);

{% template Client.Class.ReadObjectResponse %}

{% template Client.Class.ConvertToString %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<int> CalculateSumAsync(int a, int b, System.Threading.CancellationToken cancellationToken)
{
if (a == null)
throw new System.ArgumentNullException("a");
ThrowArgumentNullException("a");

if (b == null)
throw new System.ArgumentNullException("b");
ThrowArgumentNullException("b");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -229,7 +229,7 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<int> AbsoluteValueAsync(int a, System.Threading.CancellationToken cancellationToken)
{
if (a == null)
throw new System.ArgumentNullException("a");
ThrowArgumentNullException("a");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -258,7 +258,9 @@ namespace MyNamespace
var disposeResponse_ = true;
try
{
var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
Expand Down Expand Up @@ -310,6 +312,8 @@ namespace MyNamespace
public string Text { get; }
}

private static void ThrowArgumentNullException(string parameterName) => throw new global::System.ArgumentNullException(parameterName);

public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
Expand Down Expand Up @@ -535,6 +539,8 @@ namespace MyNamespace
public string Text { get; }
}

private static void ThrowArgumentNullException(string parameterName) => throw new global::System.ArgumentNullException(parameterName);

public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
Expand Down
14 changes: 10 additions & 4 deletions src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<int> CalculateSumAsync(int a, int b, System.Threading.CancellationToken cancellationToken)
{
if (a == null)
throw new System.ArgumentNullException("a");
ThrowArgumentNullException("a");

if (b == null)
throw new System.ArgumentNullException("b");
ThrowArgumentNullException("b");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -229,7 +229,7 @@ namespace MyNamespace
public virtual async System.Threading.Tasks.Task<int> AbsoluteValueAsync(int a, System.Threading.CancellationToken cancellationToken)
{
if (a == null)
throw new System.ArgumentNullException("a");
ThrowArgumentNullException("a");

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -258,7 +258,9 @@ namespace MyNamespace
var disposeResponse_ = true;
try
{
var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
Expand Down Expand Up @@ -310,6 +312,8 @@ namespace MyNamespace
public string Text { get; }
}

private static void ThrowArgumentNullException(string parameterName) => throw new global::System.ArgumentNullException(parameterName);

public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
Expand Down Expand Up @@ -535,6 +539,8 @@ namespace MyNamespace
public string Text { get; }
}

private static void ThrowArgumentNullException(string parameterName) => throw new global::System.ArgumentNullException(parameterName);

public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
Expand Down
Loading