OpenAPITools / OpenAPITools/openapi-generator

[BUG] typescript-angular: Generated service does not encode data correctly

Open
#21,888 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

  1. localVarUseForm is hardcoded to false: Despite calculating canConsumeForm, the variable localVarUseForm is always false, preventing the use of FormData for multipart uploads.

  2. Complex objects are not serialized: When using HttpParams, complex objects are converted to [object Object] string instead of being properly serialized.

  3. No Content-Type header is set: The generated code doesn't set an appropriate Content-Type header (should be application/x-www-form-urlencoded when using HttpParams or multipart/form-data when 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
  1. Create an OpenAPI spec with a POST endpoint accepting both multipart/form-data and application/json
  2. Define a request body with a complex object type
  3. Generate Angular TypeScript code using openapi-generator-cli
  4. Try to send a complex object using the generated method
  5. Observe that the request body contains [object Object] instead of the actual data
Related issues/PRs
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.