Skip to content

Commit 9e57029

Browse files
nzeeminNikita Ziminlahma
authored
Fix: C# Code Generation generates method with return default(void) (#4826)
--------- Co-authored-by: Nikita Zimin <[email protected]> Co-authored-by: Marko Lahma <[email protected]>
1 parent 84997dd commit 9e57029

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/JIRA_OpenAPI.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50345,7 +50345,7 @@ namespace MyNamespace
5034550345
var status_ = (int)response_.StatusCode;
5034650346
if (status_ == 200)
5034750347
{
50348-
return default(void);
50348+
return;
5034950349
}
5035050350
else
5035150351
if (status_ == 400)

src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public string MethodAccessModifier
115115
/// <summary>
116116
/// The default value of the result type, i.e. default(T) or default(T)! depending on whether NRT are enabled.
117117
/// </summary>
118-
public string UnwrappedResultDefaultValue => $"default({UnwrappedResultType}){(_settings is CSharpClientGeneratorSettings { CSharpGeneratorSettings.GenerateNullableReferenceTypes: true } ? "!" : "")}";
118+
public string UnwrappedResultDefaultValue => HasResult
119+
? $"default({UnwrappedResultType}){(_settings is CSharpClientGeneratorSettings { CSharpGeneratorSettings.GenerateNullableReferenceTypes: true } ? "!" : "")}"
120+
: null;
119121

120122
/// <summary>Gets or sets the synchronous type of the result.</summary>
121123
public string SyncResultType

src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.ProcessResponse.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescri
5757
{% if operation.WrapResponse -%}
5858
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
5959
{% else -%}
60-
return {{ operation.UnwrappedResultDefaultValue }};
60+
{% if operation.HasResult %}return {{ operation.UnwrappedResultDefaultValue }};{% else %}return;{% endif %}
6161
{% endif -%}
6262
{% else -%}
6363
{% if operation.WrapResponse -%}

src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@
413413
{% if operation.WrapResponse and operation.UnwrappedResultType != "FileResponse" %}
414414
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
415415
{% else -%}
416-
return {{ operation.UnwrappedResultDefaultValue }};
416+
{% if operation.HasResult %}return {{ operation.UnwrappedResultDefaultValue }};{% else %}return;{% endif %}
417417
{% endif -%}
418418
{% elsif operation.WrapResponse -%}
419419
return new {{ ResponseClass }}(status_, headers_);
@@ -431,7 +431,7 @@
431431
{% if operation.WrapResponse and operation.UnwrappedResultType != "FileResponse" %}
432432
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
433433
{% else -%}
434-
return {{ operation.UnwrappedResultDefaultValue }};
434+
{% if operation.HasResult %}return {{ operation.UnwrappedResultDefaultValue }};{% else %}return;{% endif %}
435435
{% endif -%}
436436
{% elsif operation.WrapResponse -%}
437437
return new {{ ResponseClass }}(status_, headers_);

0 commit comments

Comments
 (0)