OpenAPITools / OpenAPITools/openapi-generator

[BUG][MAVEN] Bug generating .Net Client with Maven 7.6.0 Generator

Open
#18,994 0 comments 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

When generating clients with the posted api definition, the .Net Client has a generated Serialize helper function for the Cartype:
localVarRequestOptions.FormParameters.Add("cartype", MyCusomWebapiClient.Client.ClientUtils.Serialize(cartype)); // form parameter
instead of the ParameterToString function:
localVarRequestOptions.FormParameters.Add("cartype", MyCusomWebapiClient.Client.ClientUtils.ParameterToString(cartype)); // form parameter

openapi-generator version

maven 7.6.0 for .Net Client

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  version: 1.1.1
  title: MyTitle
servers:
  - url: "http://localhost:50505"
paths:
  /api/myApi/CustomCar:
    post:
      tags:
        - Custom Cars
      summary: Imports a custom Car.
      operationId: GenerateCustomCar
      requestBody:
        description: The description of the new car.
        content:
          multipart/form-data:
            schema:
              $ref: "#/components/schemas/CustomCar"
        required: true
      responses:
        202:
          description: Accepted
        400:
          description: Bad input.
        404:
          description: Nothing here.
        500:
          description: Error.

components:
  schemas:
    CarType:
      description: Possible car types.
      type: string
      nullable: false
      enum:
        - Van
        - Hachback
        - Convertable
        - None
      default: Van

    CustomCar:
      description: A model which represents a custom Car.
      type: object
      properties:
        cartype:
          $ref: "#/components/schemas/CarType"

Generated CustomCarsApi.cs:

// CustomCarsApi.cs
namespace MyCusomWebapiClient.Api
{
[...]
    /// <summary>
    /// Represents a collection of functions to interact with the API endpoints
    /// </summary>
    public partial class CustomCarsApi : ICustomCarsApi
    {
    
    [...]
        /// <summary>
        /// Imports a custom Car. 
        /// </summary>
        /// <exception cref="MyCusomWebapiClient.Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="cartype"> (optional)</param>
        /// <param name="operationIndex">Index associated with the operation.</param>
        /// <returns>ApiResponse of Object(void)</returns>
        public MyCusomWebapiClient.Client.ApiResponse<Object> GenerateCustomCarWithHttpInfo(CarType? cartype = default(CarType?), int operationIndex = 0)
        {
            MyCusomWebapiClient.Client.RequestOptions localVarRequestOptions = new MyCusomWebapiClient.Client.RequestOptions();

            string[] _contentTypes = new string[] {
                "multipart/form-data"
            };

            // to determine the Accept header
            string[] _accepts = new string[] {
            };

            var localVarContentType = MyCusomWebapiClient.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
            if (localVarContentType != null)
            {
                localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
            }

            var localVarAccept = MyCusomWebapiClient.Client.ClientUtils.SelectHeaderAccept(_accepts);
            if (localVarAccept != null)
            {
                localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);
            }

            if (cartype != null)
            {
                localVarRequestOptions.FormParameters.Add("cartype", MyCusomWebapiClient.Client.ClientUtils.Serialize(cartype)); // form parameter
            }

            localVarRequestOptions.Operation = "CustomCarsApi.GenerateCustomCar";
            localVarRequestOptions.OperationIndex = operationIndex;


            // make the HTTP request
            var localVarResponse = this.Client.Post<Object>("/api/myApi/CustomCar", localVarRequestOptions, this.Configuration);
            if (this.ExceptionFactory != null)
            {
                Exception _exception = this.ExceptionFactory("GenerateCustomCar", localVarResponse);
                if (_exception != null)
                {
                    throw _exception;
                }
            }

            return localVarResponse;
        }

 [...]
}

Here the problematic part is:

[...]
            if (cartype != null)
            {
                localVarRequestOptions.FormParameters.Add("cartype", MyCusomWebapiClient.Client.ClientUtils.Serialize(cartype)); // form parameter
            }
[...]

this results in ""Van"" to be added to the form parameters, instead of the expected "Van" (default case).

The expected behavior can be achieved by changing the CustomCar Component and specifically add the string as type for the cartype, as follows:

 CustomCar:
      description: A model which represents a custom Car.
      type: object
      properties:
        cartype:
          type: string
          $ref: "#/components/schemas/CarType"

Resulting in MyCusomWebapiClient.Client.ClientUtils.ParameterToString() being used for the form parameter.

[...]
            if (cartype != null)
            {
                localVarRequestOptions.FormParameters.Add("cartype", MyCusomWebapiClient.Client.ClientUtils.ParameterToString(cartype)); // form parameter
            }
[...]
Generation Details

java argumentList :
"-jar openapi-generator-cli-7.6.0
"generate"
"-i $($apiDefinition.Path)"
"-g csharp"
"-o $tempClientDirectory"
"--additional-properties packageName=$($apiDefinition.ClientPackageName)"
"--additional-properties targetFramework=net8.0"
"--global-property skipFormModel=false"
)

Issue

The issue is basically, that the type is not deduced by the generator itself.

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 with the provided OpenAPI declaration and generated CustomCarsApi.cs, then trace how the C# generator handles multipart/form-data enum references. Compare the output for the direct CarType reference with the string-typed workaround. Done when the direct reference produces ParameterToString output without the workaround and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.