swagger-api / swagger-api/swagger-codegen

[JAVA] Generated code for petstore3 uses invalid base path for API calls

Open
#12,019 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I'm using swagger-codegen-cli to generate code from the petstore3 declaration file. I was invoking the code generator with -i https://petstore3.swagger.io/api/v3/openapi.json which worked perfectly fine, however, when I used a downloaded version of the json file and invoke it with -i petstore3_openapi.json (downloaded from https://petstore3.swagger.io/api/v3/openapi.json), it wouldn't work anymore and give me this stacktrace:

java.lang.IllegalArgumentException: unexpected url: /api/v3/pet/1
	at com.squareup.okhttp.Request$Builder.url(Request.java:163)
	at openapi.petStoreclient.ApiClient.buildRequest(ApiClient.java:971)
	at openapi.petStoreclient.ApiClient.buildCall(ApiClient.java:947)
	at openapi.petStoreclient.apis.PetApi.getPetByIdCall(PetApi.java:777)
	at openapi.petStoreclient.apis.PetApi.getPetByIdValidateBeforeCall(PetApi.java:787)
	at openapi.petStoreclient.apis.PetApi.getPetByIdWithHttpInfo(PetApi.java:816)
	at openapi.petStoreclient.apis.PetApi.getPetById(PetApi.java:804)

See in the Suggest a fix/enhancement section below to find my speculations.

Swagger-codegen version

3.0.36

Swagger declaration file content or url

https://petstore3.swagger.io/api/v3/openapi.json

Command line used for generation
java -jar swagger-codegen-cli-3.0.36.jar generate -i openapi.json -l java -o petstore-client
Steps to reproduce
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.36/swagger-codegen-cli-3.0.36.jar
wget https://petstore3.swagger.io/api/v3/openapi.json
java -jar swagger-codegen-cli-3.0.36.jar generate -i openapi.json -l java -o petstore-client
  1. Execute this Java code snippet
try {
    new PetApi().getPetById(1L);
} catch (ApiException e) {
    throw new RuntimeException(e);
}
  1. You should see the stacktrace from the Description section
Related issues/PRs
Suggest a fix/enhancement

I do not have a suggestion. It seems to me that it stems from defining servers.0.url = "api/v3" instead of "https://petstore3.swagger.io/api/v3" in the declaration file which causes the field basePath in ApiClient.java to become that url.
Perhaps I'm missing something here and this is desired behavior. Thanks in advance for any help.

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

Reproduce the issue with the downloaded openapi.json and the swagger-codegen-cli 3.0.36 command. Start by inspecting the generated ApiClient.java basePath handling and the PetApi.java call shown in the stack trace; done means the generated client can execute getPetById with a valid request URL in both input-file scenarios.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.