OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.51k stars 6.27k forks source link

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

Open Mr-Dav-Rii opened 1 week ago

Mr-Dav-Rii commented 1 week ago
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.