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.42k stars 6.48k forks source link

[BUG][KOTLIN] Multiple tags in YAML input lead to duplicate code generated #9995

Open florianbesserprjphxcom opened 3 years ago

florianbesserprjphxcom commented 3 years ago

Bug Report Checklist

Description

Duplicate Kotlin code is generated, which then fails to compile.

openapi-generator version

We use org.openapitools:openapi-generator-gradle-plugin:5.2.0

OpenAPI declaration file content or url

Excerpt from input file:

tags:
  - name: customer
    description: xxx
  - name: customer profile
    description: yyy

as well as

paths:
  /customers:
    post:
      tags:
        - customer
        - customer profile
      summary: zzz
      operationId: createCustomer
Generation Details

The following code is produced

@PostMapping(
            value = ["/customers"],
            produces = ["application/json"]
    )
    fun createCustomer( @RequestHeader(value="requestId", required=true) requestId: kotlin.String
): ResponseEntity<CreateCustomerResponse> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }

    @PostMapping(
            value = ["/customers"],
            produces = ["application/json"]
    )
    fun createCustomer( @RequestHeader(value="requestId", required=true) requestId: kotlin.String
): ResponseEntity<CreateCustomerResponse> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }
Steps to reproduce

Due to privacy constraints we can't paste more code here. However once we remove one of the two tags from the input, e.g.:

paths:
  /customers:
    post:
      tags:
        - customer
      summary: zzz
      operationId: createCustomer

then the generated Kotlin code does not contain a duplicate method createCustomer anymore.

Related issues/PRs

None

Suggest a fix

Do not generate a duplicate method even if multiple tags are provided for a given input.

c-classen commented 3 years ago

I have the same problem and created a minimal example to reproduce the issue:

openapi: 3.0.3
info:
  title: "Example API"
  version: "1.0.0"
paths:
  /test:
    get:
      operationId: "test"
      tags: [ "First", "Second" ]
      responses:
        "200":
          description: "Success"

I have used the validator to confirm that the spec is valid. I also confirmed that the issue still persists. Sadly, I could not build from source, but I used the following jar to reproduce the problem as described here: https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.0.0-SNAPSHOT/openapi-generator-cli-6.0.0-20210712.095051-16.jar

Steps to reproduce:

  1. Save the spec above to a file called api.yaml and put it in the same directory as the jar
  2. Execute java -jar openapi-generator-cli-6.0.0-20210712.095051-16.jar generate -g kotlin-spring -i api.yaml
  3. Open the generated src/main/kotlin/org/openapitools/api/TestApi.kt and you will see the following:
package org.openapitools.api

import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity

import org.springframework.web.bind.annotation.*
import org.springframework.validation.annotation.Validated
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.beans.factory.annotation.Autowired

import javax.validation.Valid
import javax.validation.constraints.DecimalMax
import javax.validation.constraints.DecimalMin
import javax.validation.constraints.Max
import javax.validation.constraints.Min
import javax.validation.constraints.NotNull
import javax.validation.constraints.Pattern
import javax.validation.constraints.Size

import kotlin.collections.List
import kotlin.collections.Map

@RestController
@Validated
@RequestMapping("\${api.base-path:}")
class TestApiController() {

    @GetMapping(
        value = ["/test"]
    )
    fun test(): ResponseEntity<Unit> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }

    @GetMapping(
        value = ["/test"]
    )
    fun test(): ResponseEntity<Unit> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }
}

This is of course not valid Kotlin code, as the function for the GET /test is generated twice

florianbesserprjphxcom commented 3 years ago

Thanks @c-classen , I've updated the original post.

ffffionn commented 2 years ago

Still happening as of v5.3.1