jnwltr / swagger-angular-generator

Generator of API layer in TypeScript for Angular 2+ apps
MIT License
90 stars 46 forks source link

Endpoints for file uploads should be improved #23

Open fazpu opened 6 years ago

fazpu commented 6 years ago

For example, the swagger:

/images/scorecard/{scoreCardId}": {
      "post": {
        "tags": [
          "image-rest-controller"
        ],
        "summary": "Upload point report images",
        "operationId": "uploadPointReportImageUsingPOST",
        "consumes": [
          "multipart/form-data"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "scoreCardId",
            "in": "path",
            "description": "scoreCardId",
            "required": true,
            "type": "integer",
            "format": "int64"
          },
          {
            "name": "file1",
            "in": "formData",
            "description": "file1",
            "required": false,
            "type": "file"
          }
        }
}

produces

uploadPointReportImageUsingPOST(params: UploadPointReportImageUsingPOSTParams): Observable<string> {
    const pathParams = {
      scoreCardId: params.scoreCardId,
    };
    const formDataParams = {
      file1: params.file1
    };
    return this.http.post<string>(`/images/scorecard/${pathParams.scoreCardId}`, formDataParams);
  }

instead it should be

uploadPointReportImageUsingPOST(params: UploadPointReportImageUsingPOSTParams): Observable<string> {
    const pathParams = {
      scoreCardId: params.scoreCardId,
    };
    const formDataParams = new FormData();
    formDataParams.set('file1', params.file1);
    return this.http.post<string>(`/images/scorecard/${pathParams.scoreCardId}`, formDataParams);
  }

I believe there shoul be a condition for this behaviour based on:

"consumes": [
          "multipart/form-data"
        ],
abauske commented 4 years ago

Any update on this? I really would need to upload files and so far I liked the generated ts client. Thanks for that by the way!

jnwltr commented 4 years ago

Thanks for feedback. You are definitely welcome to make a PR, what do you think?

abauske commented 4 years ago

Please have a look at the PR and mind the question in it! I'm happy to adapt anything if there is a problem with it.