0

I have this controller endpoint in .NET Core 6. I am using Swagger and NSwag Studio (latest version v13.17.0.0) to generate C# client :

[HttpGet("GetAllMinimalCalculationDtoOfMaterialByLoggedinUser/{isExistingBuildingCalculation:bool?}", Name = "GetAllMinimalCalculationDtoOfMaterialByLoggedinUser")]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<MinimalCalculationDto>), (int)HttpStatusCode.OK),
    ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public ActionResult GetAllMinimalCalculationDtoOfMaterialByLoggedinUser(bool isExistingBuildingCalculation = false)

But the NSwag Studio generated client does not have this parameter as optional parameter:

public virtual async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<MinimalCalculationDto>> GetAllMinimalCalculationDtoOfMaterialByLoggedinUserAsync(bool isExistingBuildingCalculation, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
    if (isExistingBuildingCalculation == null)
        throw new System.ArgumentNullException("isExistingBuildingCalculation");

    var urlBuilder_ = new System.Text.StringBuilder();
    urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/v1/Calculation/GetAllMinimalCalculationDtoOfMaterialByLoggedinUser/{isExistingBuildingCalculation}");
    urlBuilder_.Replace("{isExistingBuildingCalculation}", System.Uri.EscapeDataString(ConvertToString(isExistingBuildingCalculation, System.Globalization.CultureInfo.InvariantCulture)));

    var client_ = _httpClient;
}

This above is the generated client result and as we see the result has explicit code to throw an exception if the parameter isExistingBuildingCalculation is null. Also, it does not have the default value of false for this parameter in its method signature.

I want the case that even if a null check is still there then at least the default value of false should appear in the method signature so that I can use the generated method without passing any parameter as well as with passing a parameter.

I have tried the setting of 'Generate optional parameters' in NSwag Studio but that also did nothing, same result:

NSwag Studio Settings

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36

1 Answers1

0

Change the signature to this:

[HttpGet("GetAllMinimalCalculationDtoOfMaterialByLoggedinUser/{isExistingBuildingCalculation:bool?}", Name = "GetAllMinimalCalculationDtoOfMaterialByLoggedinUser")]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<MinimalCalculationDto>), (int)HttpStatusCode.OK),
ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public ActionResult GetAllMinimalCalculationDtoOfMaterialByLoggedinUser(bool? isExistingBuildingCalculation)
Oliver Weichhold
  • 10,259
  • 5
  • 45
  • 87