Multipart/form-data with two parts does not generate correct typescript
See original GitHub issueI have a C# method (prototype below) that contains one Object to be serialized to JSON and one file upload. No matter what option I try (JSON object as Form Data or as Body data), the JSON object is either not valid or not part of the generated typescript. This is using version 11.12.9
.
When using [FromData]
to incorporate the object as a multipart form, the generated typescript calls toString()
on the object, which uses the Object
prototype and therefore adds [object Object]
to the call, which is not valid.
When using [FromBody]
for the object, the object is entirely ignored by the generated typescript. It is part of the method signature but is never added to the HTTP request.
Providing method signature, Swagger JSON, and output Typescript below for reference.
Prototype
[HttpPost]
[AllowAnonymous]
[Consumes("multipart/form-data")]
[ActionName("applyForJob")]
[SwaggerResponse((int)HttpStatusCode.OK, null)]
[SwaggerResponse((int)HttpStatusCode.BadRequest, typeof(void))]
public async Task<IActionResult> ApplyForJob(
[FromForm] JobApplicationModel jobModel,
[FromForm] IFormFile resume)
Swagger
"/api/Contact/applyForJob": {
"post": {
"tags": [
"Contact"
],
"operationId": "Contact_applyForJob",
"consumes": [
"multipart/form-data"
],
"parameters": [
{
"type": "object",
"name": "jobModel",
"in": "formData",
"x-schema": {
"$ref": "#/definitions/JobApplicationModel"
},
"x-nullable": true
},
{
"type": "file",
"name": "resume",
"in": "formData",
"x-nullable": true
}
],
"responses": {
"200": {
"description": ""
},
"400": {
"description": ""
}
},
"security": [
{
"apiKey": []
}
]
}
},
Typescript
apiContactApplyforjob(jobModel: JobApplicationModel | null, resume: FileParameter | null): Observable<void> {
let url_ = this.baseUrl + "/api/Contact/applyForJob";
url_ = url_.replace(/[?&]$/, "");
const content_ = new FormData();
if (jobModel !== null && jobModel !== undefined)
content_.append("jobModel", jobModel.toString());
if (resume !== null && resume !== undefined)
content_.append("resume", resume.data, resume.fileName ? resume.fileName : "resume");
....[snipped for clarity]
Issue Analytics
- State:
- Created 6 years ago
- Reactions:18
- Comments:12 (5 by maintainers)
@jonmill I run into the same problem as you described and manage to go around it by overriding toString() method of that specific type.
I used this as guidance https://github.com/RicoSuter/NJsonSchema/wiki/TypeScriptGenerator#extended-classes-and-extension-code
Hope this will help somebody in the same boat!
Any update on this? I am still experiencing this using version 13.7.0.
C# code
Generated TypeScript
As you can see, the
request: CreateProjectRequest
is part of the generated method, but it’s never used anywhere in the body of the method and never added to the form data.