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.3k stars 6.44k forks source link

[BUG] [Kotlin] [Retrofit 2] [openapi-generator-gradle-plugin] - Generating an absolute paths instead of relative. #7208

Open shaban-rant opened 4 years ago

shaban-rant commented 4 years ago

Bug Report Checklist

Description

The openapi-generator-gradle-plugin is generating the endpoints as absolute paths, with leading / like /pets, instead of relative like pets. And because the path is absolute - Retrofit seems to be ignoring any path information given to the builder in the .baseUrl(HttpUrl) function. It seems to just be taking the schema and host, and ignoring any base path given to it.

Info from Retrofit's site:

Endpoint values which contain a leading `/` are absolute.
Absolute values retain only the host from baseUrl and ignore any specified path components.
Base URL: http://example.com/api/
Endpoint: /foo/bar/
Result: http://example.com/foo/bar/
openapi-generator version

org.openapi.generator version "4.3.1"

OpenAPI declaration file content or url

https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml

Steps to reproduce

This is the setup: gradle task:

openApiGenerate {
    generatorName = "kotlin"
    library = "jvm-retrofit2"
    inputSpec = "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml"
    configOptions = [
            dateLibrary: "java8"
    ]
}

generated PetsApi interface:

interface PetsApi {
    @POST("/pets")
    fun createPets(): Call<Unit>

    @GET("/pets")
    fun listPets(@Query("limit") limit: kotlin.Int): Call<kotlin.Array<Pet>>

    @GET("/pets/{petId}")
    fun showPetById(@Path("petId") petId: kotlin.String): Call<Pet>

}

initialising the petsApi:

    private val apiClient = ApiClient(baseUrl = "http://petstore.swagger.io/v1/", okHttpClient = okHttpClient)
    private val petsApi: PetsApi = apiClient.createService(PetsApi::class.java)
Suggest a fix

Maybe to have the generating an absolute paths configurable.

Similar / related

Here is the same issue described on stackoverflow - https://stackoverflow.com/questions/32352159/retrofit-2-removes-characters-after-hostname-from-base-url There is a presentation how to fix the issue manually, like removing the leading / to make the path relative. https://www.youtube.com/watch?v=KIAoQbAu3eA&feature=youtu.be&t=32m50s Screenshot from the video:

Screenshot 2020-08-14 at 00 25 43
auto-labeler[bot] commented 4 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.