RicoSuter/NSwag

NSwag generate wrong code for FileContentResult type.

Open

#791 opened on 2017年6月12日

GitHub で見る
 (51 comments) (5 reactions) (0 assignees)C# (6,291 stars) (1,189 forks)batch import
help wantedtype: question

説明

Hi Team,

I have Web.API core action method which returns FileContentResult, code example:

return new FileContentResult(fileViewModel.Content, fileViewModel.FileMetadataViewModel.MimeType)
{
      FileDownloadName = fileViewModel.FileMetadataViewModel.FileName
};

It generates code like:

var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
var result_ = default(FileContentResult);
try
{
   result_ = Newtonsoft.Json.JsonConvert.DeserializeObject<FileContentResult>(responseData_);
   return result_;
}
catch (System.Exception exception)
{
    throw new BusinessApiException("Could not deserialize the response body.", status_, result_.ToString(), headers_, exception);
}

Which cause SerializationException.

My manual fix looks like:

var result_ = new FileContentResult();
 
// Get file's byte array
byte[] responseData_ = await response_.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
 
// Set FileContentResult properties manually
result_.ContentType = headers_["Content-Type"].FirstOrDefault();
result_.FileDownloadName = headers_["Content-Disposition"].FirstOrDefault();
result_.FileContents = responseData_;
                       
try
{
    return result_;
}
catch (System.Exception exception)
{
    throw new BusinessApiException("Could not deserialize the response body.", status_, result_.ToString(), headers_, exception);
}

Could you please help me or/and suggest how to fix it. (Probably, I'm using wrong type or doing something wrong etc)

Thanks a lot in advance !!

コントリビューターガイド