0

I have a backend service written in .NET Core and I use NSwag to generate a Swagger client. My client app, which is written in TypeScript, uses this Swagger to generate code for the requests. This works perfectly fine except for files. I have one endpoint that downloads a file and this endpoint can't be automatically type-hinted no matter what! This is my code for the endpoint:

[HttpGet("{id}/download")]
[Produces("text/plain", Type = typeof(FileStreamResult))]
public ActionResult GenerateCreateTableStatements(long id)
{
    try
    {
        // code
        return new FileStreamResult(stream, "text/plain") { FileDownloadName = "text_file.txt" };

    }
    catch (Exception e)
    {
        return BadRequest(e.Message);
    }
}

This code produces the following configuration in the Swagger's json:

"/api/Project/{id}/download": {
  "get": {
    "tags": [
      "xx"
    ],
    "parameters": [
      {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "integer",
          "format": "int64"
        }
      }
    ],
    "responses": {
      "200": {
        "description": "Success",
        "content": {
          "text/plain": {
            "schema": {
              "type": "file"
            }
          }
        }
      }
    }
  }
}

Later on in my Typescript app, I run openapi-generator-cli generate -i URL/swagger/v1/swagger.json -g typescript-axios -o src/client and this generates me all of the code for all of the endpoints. For this particular endpoint that returns a FileStreamResult, I get

async apiProjectIdDownload(id: number, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<any>> {
    const localVarAxiosArgs = await localVarAxiosParamCreator.apiProjectIdDownload(id options);
    return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
}

As you can see, it type-hints it as any instead of Blob which is my goal. I tried some suggestions from an open issue on NSwag's github but still no luck.

Has anyone stumbled upon an issue like this and how did you manage to resolve it?

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
anthino12
  • 770
  • 1
  • 6
  • 29

0 Answers0