RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.76k stars 1.29k forks source link

Generated Client NRT Warnings #3929

Open csehatt741 opened 2 years ago

csehatt741 commented 2 years ago

When the request body is required and nullable reference types are allowed in the project containing the generated code, warnings are raised in the generated client.

Command: openapi2csclient JsonLibrary: SystemTextJson NSwag version: 13.15.10.0

OpenAPI:

requestBody:
  required: true
  content:
    application/json:
      schema:
if (status_ == 200)
{
    var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
    var result_ = (ResponseType)System.Convert.ChangeType(responseData_, typeof(ResponseType));
    return new SwaggerResponse<ResponseType>(status_, headers_, result_);
}

Required fixes: var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); ->

if (response_.Content == null)
{
    string responseText_ = (response_.Content == null) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
    throw new ApiException("A non - empty request body is required.", status_, responseText_, headers_, null);
}

var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
FlorianArnould commented 2 years ago

I have the same kind of warning using an equivalent configuration:

Command: openapi2csclient JsonLibrary: Newtonsoft.Json NSwag version: 13.15.10.0

I have a route responding a text/plain content-type, I have this code generated :

if (status_ == 200)
{
    var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
    var result_ = (string)System.Convert.ChangeType(responseData_, typeof(string)); // [CS8600] Converting null literal or possible null value to non-nullable type.
    return result_;
}

and this is giving me this warning : [CS8600] Converting null literal or possible null value to non-nullable type.

FlorianArnould commented 2 years ago

Still the same with NSwag.MSBuild 13.16.1

ariatpi commented 1 year ago

Still seeing this in 13.18.0