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
21.81k stars 6.58k forks source link

[BUG] [Csharp] Mustash templates remove explicit namespace for System.IO.Path #19344

Open alfredo-accuris opened 2 months ago

alfredo-accuris commented 2 months ago

The newest version of the api generator v7.7.0 (#18915) removes specific namespaces (System.IO.Path) for templates on Csharp. This produces autogenerated code that may have conflict when imported to an existing project if there is an existing Path class.

To solve the problem, either the existing project has to rename their classes or add the missing namespaces in the autogenerated code.

Bug Report Checklist

On the latest release the issue #18915 remove namespaces from the mustash templates ware added.

actual output:

ApiClient.cs

       internal object Deserialize(RestResponse response, Type type)
       {
           if (type == typeof(byte[])) // return byte array
           {
               return response.RawBytes;
           }

           // TODO: ? if (type.IsAssignableFrom(typeof(Stream)))
           if (type == typeof(Stream))
           {
               var bytes = response.RawBytes;
               if (response.Headers != null)
               {
                   var filePath = string.IsNullOrEmpty(_configuration.TempFolderPath)
                       ? Path.GetTempPath()
                       : _configuration.TempFolderPath;

Expected:

       internal object Deserialize(RestResponse response, Type type)
       {
           if (type == typeof(byte[])) // return byte array
           {
               return response.RawBytes;
           }

           // TODO: ? if (type.IsAssignableFrom(typeof(Stream)))
           if (type == typeof(Stream))
           {
               var bytes = response.RawBytes;
               if (response.Headers != null)
               {
                   var filePath = string.IsNullOrEmpty(_configuration.TempFolderPath)
                       ? global::System.IO.Path.GetTempPath()                     //Full namespace or import using Path = global::System.IO.Path; at the top
                       : _configuration.TempFolderPath;
Description

The newest version of the api generator v7.7.0 (#18915) removes specific namespaces (System.IO.Path) for templates on Csharp. This produces autogenerated code that may have conflict when imported to an existing project if there is an existing Path class.

openapi-generator version

v7.7.0

OpenAPI declaration file content or url

Using a single schema the generated code removes the namespaces

openapi: 3.0.1
info:
  title: Testing single
  version: 0.0.1
paths:
  /single/session/{userId}:
    get:
      summary: Single endpoint session
      parameters:
        - name: userId
          in: path
          description: The identity of the user.
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Session found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Session'
              example:
                userId: bob
                created: 1698256753
        '400':
          description: Bad Request.
        '404':
          description: If no session found
components:
  schemas:
    Session:
      type: object
      properties:
        userId:
          type: string
          description: Identify the user for the active session.
        created:
          type: integer
          description: Identify the creation of the session.
          format: int64
      additionalProperties: false
      description: Represents the active session of the user.
Generation Details

Executing the docker

Steps to reproduce

Execute the lastes version

docker run --rm openapitools/openapi-generator-cli:v7.7.0 generate -i ./swagger.json --package-name Sdk.Simple -g csharp -o ./Sdk/Simple --additional-properties targetFramework=net8.0

The output code in /Sdk.Simple /Client/ApiClient.cs does not have the namespaces

Related issues/PRs

18915 removed namespaces for PATH

Suggest a fix

Fully qualify the autogenerate clases to avoid collision when importing on another project

devhl-labs commented 2 months ago

Take it a step further and say the expected is global::System.IO.Path.GetTempPath()

alfredo-accuris commented 2 months ago

You are right, I changed the file modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache to import the Path as using Path = global::System.IO.Path; and it avoid any collation.

Changing the expected to import as global::