[BUG][typescript-fetch] Complex values are serialized as the string [object Object] in multipart/form-data
Created by: verokarhu
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Complex types are incorrectly serialized as the string [object Object]
when they are used as a part in a multipart/form-data
request body.
openapi-generator version
4.3.1 / 5.0.0-beta2 / 2ec96f7f
OpenAPI declaration file content or url
---
openapi: "3.0.0"
info:
title: Bug test case
version: "1.0"
paths:
/example:
post:
operationId: exampleOperation
requestBody:
content:
multipart/form-data:
schema:
properties:
complexvalue:
properties:
value1:
type: number
value2:
type: string
type: object
type: object
responses:
"201":
description: Created
Generation Details
java -jar openapi-generator-cli.jar generate -g typescript-fetch -i testcase.yml -o generated
Steps to reproduce
Related issues/PRs
Suggest a fix
The generated model (models/ExampleComplexvalue.ts, 21-34
) is correct:
export interface ExampleComplexvalue {
/**
*
* @type {number}
* @memberof ExampleComplexvalue
*/
value1?: number;
/**
*
* @type {string}
* @memberof ExampleComplexvalue
*/
value2?: string;
}
However, the generated serialization code (apis/DefaultApi.ts, 53-55
) is incorrect:
if (requestParameters.complexvalue !== undefined) {
formParams.append('complexvalue', requestParameters.complexvalue as any);
}
As you can see from the documentation of FormData.append(), all other types than Blob or subclasses will be converted into string:
value
The field's value. This can be a USVString or Blob (including subclasses such as File). If none of these are specified the value is converted to a string.
The correct way to serialize this would be to convert the complex type into a JSON Blob before serializing it:
new Blob([JSON.stringify(requestParameters.complexvalue)], { type: "application/json", }
This should not be done to simple string types though, since they need to be appended as-is and should have the default text/plain
Content-Type.