[BUG][TypeScript] Multipart request missing Content-Type for models
Created by: eirikandre
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
Multipart requests are missing Content-Type for json parts. Without this servers like Java/Jersey will not be able to understand the request and the part will be set to null.
The multipart request body should look like this:
-----------------------------4577985812131849638818726930
Content-Disposition: form-data; name="my-thing"; filename="blob"
Content-Type: application/json <-------------- The missing part
{"foo":"bar"}
-----------------------------4577985812131849638818726930
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: application/octet-stream
<the file content>
-----------------------------4577985812131849638818726930--
openapi-generator version
4.1.1
OpenAPI declaration file content or url
openapi: "3.0.0"
info:
title: My Title
version: '1'
paths:
/test:
post:
operationId: postMultipart
summary: Legg til nytt vedlegg
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
my-object:
$ref: "#/components/schemas/MyObject"
file:
type: string
format: binary
responses:
200:
description: 200 response when everything is ok
components:
schemas:
MyObject:
type: object
properties:
foo:
type: string
Command line used for generation
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i test.yaml -g org.openapitools.codegen.languages.TypeScriptAngularClientCodegen
Steps to reproduce
- Build angular code
- Open generated angular service
- find the following code snippet:
if (my-object !== undefined) {
formParams = formParams.append('my-object', useForm ? JSON.stringify(vedlegg) : <any>my-object) as any || formParams;
}
Instead of JSON.stringify(vedlegg)
, it should have been wrapped in a new Blob to set Content-Type: new Blob([JSON.stringify(vedlegg)], {type: 'application/json'})
Related issues/PRs
Suggest a fix
Wrap JSON.stringify in Blob:
{{#useHttpClient}}formParams = {{/useHttpClient}}formParams.append('{{baseName}}', {{^isModel}}<any>{{paramName}}{{/isModel}}{{#isModel}}useForm ? new Blob([JSON.stringify({{paramName}})], {type: 'application/json'}) : <any>{{paramName}}{{/isModel}}){{#useHttpClient}} as any || formParams{{/useHttpClient}};