Skip to content

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

@aronkankel

Description

@aronkankel
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions