OpenAPITools / OpenAPITools/openapi-generator

[BUG] csharp: api.mustache doesn't check isPrimitiveType for all settings to localVarRequestOptions.FormParameters

Open
#17,699 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

Description

There is an object type parameter in an multipart/form-data for an POST request with application/json content-type, but the object is not serialized as JSON.

openapi-generator version

7.2.0

OpenAPI declaration file content or url

https://developer.datev.de/datev/platform/de/product/42564/api/29463#/accountingdocuments_20/overview
-> blob:https://developer.datev.de/8d9c6b50-cc51-44a8-ab2c-fcbd0e2a9b77

These are the affected sections of the YAML:

[...]
paths:
  /clients/{client-id}/documents:
    post:
      tags:
        - Transfer a document
      parameters:
        - name: client-id
          in: path
          description: ID that identifies the client.
          required: true
          schema:
            type: string
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
                - file
              properties:
                file:
                  type: string
                  description: |
                    one single file    
                  format: binary
                metadata:
                  $ref: '#/components/schemas/metadata'
        required: true
      responses:
        '201':
[...]
components:
  schemas:    
    metadata:
      type: object
      properties:
        document_type:
          type: string
          description: Name of the document type
          example: Rechnungseingang
        note:
          maxLength: 255
          type: string
          description: Note pertaining to the document for the tax consultant.
          example: This is an example note.
[...]

"metadata" is an object and not a primitive type. The generator doesn't recognize and generates the wrong ClientUtils-call.

Generation Details

The generated TransferADocumentApi.cs contains:
localVarRequestOptions.FormParameters.Add("metadata", OpenApi.V2.Documents.Client.ClientUtils.ParameterToString(metadata)); // form parameter

The error is usage of Client.ClientUtils.ParameterToString() instead of Client.ClientUtils.Serialize()

Steps to reproduce

Generate with -g csharp and the given YAML above.

Suggest a fix

The fix has to be done in
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/csharp/api.mustache
in lines 400, 650 and 672.
The correct code is in line 378

localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // form parameter

should be changed to

localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.{{#isPrimitiveType}}ParameterToString{{/isPrimitiveType}}{{^isPrimitiveType}}Serialize{{/isPrimitiveType}}({{paramName}})); // form parameter

I tested it using option -t for a custom template

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

Start in modules/openapi-generator/src/main/resources/csharp/api.mustache, especially the form-parameter sections around lines 400, 650, and 672, and compare them with line 378. Generate the supplied OpenAPI declaration with -g csharp and verify that object form parameters use ClientUtils.Serialize while primitive parameters continue using ParameterToString.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.