OpenAPITools / OpenAPITools/openapi-generator

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

Open
#7,208 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Retrofit Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
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

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

Run the shown Gradle openApiGenerate setup with the petstore.yaml specification and inspect the generated Kotlin PetsApi interface from the openapi-generator-gradle-plugin. Compare the Retrofit annotations' endpoint paths with the configured base URL; done means generated paths preserve the base path rather than starting with a leading slash.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
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.