cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Requests of type multipart/form-data are not being sent correctly #8

Closed luisfpg closed 5 years ago

luisfpg commented 5 years ago

We shouldn't explicitly set the content type in this case.

sudoman281 commented 4 years ago

Requests of this type are still not sent correctly. It generates code like this:

 const rb = new RequestBuilder(this.rootUrl, TokenService.ApiMainTokenPostPath, 'post');
    if (params) {

      rb.body(params.body, 'multipart/form-data');
    }

, but the parameters need to be appended to the FormData object:

let formData: FormData = new FormData(); 
    formData.append('id', model.id); 

The request sent by Angular now carries these binary data https://imgur.com/a/slkSZ53, which cannot be read by the dotnet api.

luisfpg commented 4 years ago

@sudoman281 Can you attach the OpenAPI definition of the operation?

sudoman281 commented 4 years ago

Sure, here it is:

"/api/Main/Token": {
      "post": {
        "tags": [
          "Token"
        ],
        "summary": "Vrací autentizační JWT token",
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "required": [
                  "email",
                  "password"
                ],
                "type": "object",
                "properties": {
                  "email": {
                    "type": "string"
                  },
                  "password": {
                    "type": "string"
                  }
                }
              },
              "encoding": {
                "email": {
                  "style": "form"
                },
                "password": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "400": {
            "description": "Neplatný požadavek",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Špatné přihlašovací údaje",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "200": {
            "description": "Přihlášení úspěšné"
          }
        }
      }
    }
sudoman281 commented 4 years ago

I found that the solution for this is to change a method in one of the generated files - request-builder.ts. I needed to change

  private formDataValue(value: any): any {
    if (value === null || value === undefined) {
      return null;
    }
    if (value instanceof Blob) {
      return value;
    }
    if (typeof value === 'object') {
      return new Blob([JSON.stringify(value)], { type: 'application/json' });
    }
    new Blob([String(value)], { type: 'text/plain' });
  }

to

  private formDataValue(value: any): any {
    if (value === null || value === undefined) {
      return null;
    }
    if (value instanceof Blob) {
      return value;
    }
    if (typeof value === 'object') {
      return new Blob([JSON.stringify(value)], { type: 'application/json' });
    }
    new value;
  }

What is the reason for it to be made like it's now?