OpenAPITools / OpenAPITools/openapi-generator
[BUG] typescript-angular: Generated service does not encode data correctly
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- 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
The Angular TypeScript generator produces code that incorrectly handles complex objects when the API expects form-encoded data (application/x-www-form-urlencoded or multipart/form-data). The generated code attempts to append complex objects directly to HttpParams, resulting in [object Object] being sent instead of properly serialized data.
OpenAPI Specification
paths:
/api/BuchungsmaskeBelegRequest:
post:
operationId: apiBuchungsmaskeBelegRequestPost
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
buchung:
$ref: '#/components/schemas/BuchungslisteDto'
application/json:
schema:
type: object
properties:
buchung:
$ref: '#/components/schemas/BuchungslisteDto'
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/BuchungsmaskeBelegResponse'
components:
schemas:
BuchungslisteDto:
type: object
properties:
mandant:
type: integer
nullable: true
buchungId:
type: integer
nullable: true
buchungdatum:
type: string
nullable: true
buchungen:
type: array
items:
$ref: '#/components/schemas/SplitBuchungDto'
nullable: true
Generated Code Issue
Current Generated Code
public apiBuchungsmaskeBelegRequestPost(buchung?: BuchungslisteDto, ...) {
// ...
const consumes: string[] = [
'multipart/form-data',
'application/json'
];
const canConsumeForm = this.canConsumeForm(consumes);
let localVarFormParams: { append(param: string, value: any): any; };
let localVarUseForm = false; // ← HARDCODED TO FALSE
let localVarConvertFormParamsToString = false;
if (localVarUseForm) {
localVarFormParams = new FormData();
} else {
localVarFormParams = new HttpParams({encoder: this.encoder});
}
if (buchung !== undefined) {
// This appends the complex object directly, resulting in [object Object]
localVarFormParams = localVarFormParams.append('buchung', <any>buchung);
}
// ...
return this.httpClient.request<BuchungsmaskeBelegResponse>('post', url, {
body: localVarFormParams, // Sends "buchung=[object Object]"
// ...
});
}
Problems
-
localVarUseFormis hardcoded tofalse: Despite calculatingcanConsumeForm, the variablelocalVarUseFormis alwaysfalse, preventing the use ofFormDatafor multipart uploads. -
Complex objects are not serialized: When using
HttpParams, complex objects are converted to[object Object]string instead of being properly serialized. -
No Content-Type header is set: The generated code doesn't set an appropriate Content-Type header (should be
application/x-www-form-urlencodedwhen using HttpParams ormultipart/form-datawhen using FormData).
Expectations
When consumes is application/json the content-type should also be set correctly and data should be sent as json
openapi-generator version
7.14.0
with @openapitools/openapi-generator-cli 2.20.2
Generation Details
openapi-generator generate -i api.joson -g typescript-angular -o /src/app/gen/api
Steps to reproduce
- Create an OpenAPI spec with a POST endpoint accepting both
multipart/form-dataandapplication/json - Define a request body with a complex object type
- Generate Angular TypeScript code using openapi-generator-cli
- Try to send a complex object using the generated method
- Observe that the request body contains
[object Object]instead of the actual data
Related issues/PRs
Suggest a fix
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the supplied OpenAPI specification with the typescript-angular generator using the command in the Generation Details, then inspect the generated service method's form and request-body handling. No source file or test is named; done means complex form data is not emitted as [object Object], multipart and urlencoded requests use appropriate handling, and application/json sends JSON with the correct content type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100