cyclosproject / ng-openapi-gen

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

FormData send all form data values as Blob #55

Closed forestmaxime closed 4 years ago

forestmaxime commented 4 years ago

The issue seems to be related to the formDataValue function in request-builder.ts (requestBuilder.handlebars). If I check the differences between ng-swagger-gen and ng-openapi-gen when sending my request, the primitive types and JSON objects are sent as non-blob in the request contrary to what I found here in the following function.

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' });
    }
    return new Blob([String(value)], { type: 'text/plain' });
  }

Changing it to something around those lines seems to have fixed my issue:

private formDataValue(value: any): any {
    if (value === null || value === undefined) {
      return null;
    }
    if (value instanceof Blob) {
      return value;
    }

    if (typeof value === 'number' || typeof value === 'string' || typeof value === 'boolean' || typeof value === 'object') {
      return JSON.stringify(value);
    }
   try {
      var o = JSON.parse(value);
      if (o && typeof o === 'object') {
        return JSON.stringify(o);
      }
    } catch {
    }
    return new Blob([String(value)], { type: 'text/plain' });
  }

How could we improve my dummy code and fix this in a cleaner way?