I am writing a Web API and have defined a controller with various GET, POST methods etc. I am using Swagger Open API for my documentation and want to understand the correct way to annotate. Here's an example of a controller method I have:
/// <summary>Download a file based on its Id.</summary>
/// <param name="id">Identity of file to download.</param>
/// <returns><see cref="MyFile" /> file content found.</returns>
[HttpGet("download/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerResponse(200, "Myfile content", typeof(MyFile))]
[SwaggerResponse(404, "Could not find file", typeof(MyFile))]
public async Task<IActionResult> DownloadAsync(int id)
{
const string mimeType = "application/octet-stream";
var myFile = await _dbContext.MyFiles.FindAsync(id);
// If we cannot find the mapping, return 404.
if (myFile.IsNullOrDefault())
{
return NotFound();
}
// Download using file stream.
var downloadStream = await _blobStorage.DownloadBlob(myFile.FileLocation);
return new FileStreamResult(downloadStream, mimeType) { FileDownloadName = myFile.FileName };
}
As you can see, I'm using both ProducesResponseType and SwaggerResponse to describe the download method. I'm a bit confused as to the correct attribute to use - swagger response or produces response type? Should I use both? Why would I favor one over the other?
Thanks for any pointers in advance! :)