RicoSuter/NSwag

NSwag generate wrong code for FileContentResult type.

Open

#791 建立於 2017年6月12日

在 GitHub 查看
 (51 留言) (5 反應) (0 負責人)C# (6,291 star) (1,189 fork)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 !!

貢獻者指南

NSwag generate wrong code for FileContentResult type. · RicoSuter/NSwag#791 | Good First Issue