From 7caef434acf222cf1d9bcefad6269f8b8f4c7720 Mon Sep 17 00:00:00 2001 From: Nikita Zimin Date: Tue, 19 Mar 2024 19:30:14 +0300 Subject: [PATCH] Fix: C# Code Generation generates method with return default(void) --- .../Models/CSharpOperationModel.cs | 4 +++- .../Templates/Client.Class.ProcessResponse.liquid | 2 +- src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs index 0c11abc0bf..6127c0ebcc 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs @@ -113,7 +113,9 @@ public string MethodAccessModifier /// /// The default value of the result type, i.e. default(T) or default(T)! depending on whether NRT are enabled. /// - public string UnwrappedResultDefaultValue => $"default({UnwrappedResultType}){((_settings as CSharpClientGeneratorSettings)?.CSharpGeneratorSettings.GenerateNullableReferenceTypes == true ? "!" : "")}"; + public string UnwrappedResultDefaultValue => HasResult + ? $"default({UnwrappedResultType}){((_settings as CSharpClientGeneratorSettings)?.CSharpGeneratorSettings.GenerateNullableReferenceTypes == true ? "!" : "")}" + : null; /// Gets or sets the synchronous type of the result. public string SyncResultType diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.ProcessResponse.liquid b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.ProcessResponse.liquid index 9201c58938..bc037fc589 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.ProcessResponse.liquid +++ b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.ProcessResponse.liquid @@ -57,7 +57,7 @@ throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescri {% if operation.WrapResponse -%} return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }}); {% else -%} -return {{ operation.UnwrappedResultDefaultValue }}; +{% if operation.HasResult %}return {{ operation.UnwrappedResultDefaultValue }};{% else %}return;{% endif %} {% endif -%} {% else -%} {% if operation.WrapResponse -%} diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid index 584842c89f..0d9cf0661b 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid +++ b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid @@ -407,7 +407,7 @@ {% if operation.WrapResponse and operation.UnwrappedResultType != "FileResponse" %} return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }}); {% else -%} - return {{ operation.UnwrappedResultDefaultValue }}; + {% if operation.HasResult %}return {{ operation.UnwrappedResultDefaultValue }};{% else %}return;{% endif %} {% endif -%} {% elsif operation.WrapResponse -%} return new {{ ResponseClass }}(status_, headers_); @@ -425,7 +425,7 @@ {% if operation.WrapResponse and operation.UnwrappedResultType != "FileResponse" %} return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }}); {% else -%} - return {{ operation.UnwrappedResultDefaultValue }}; + {% if operation.HasResult %}return {{ operation.UnwrappedResultDefaultValue }};{% else %}return;{% endif %} {% endif -%} {% elsif operation.WrapResponse -%} return new {{ ResponseClass }}(status_, headers_);