swagger-api / swagger-api/swagger-codegen

[JAVA] swagger-codgen generates code with syntax error in Enum when defined in separate definition file

Open
#11,616 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
Swagger-codegen version

3.0.30

Swagger declaration file content or url

All source files

Command line used for generation

swagger-codegen generate -i ./api.yml -l java -o ./generated-code

Steps to reproduce
  1. Keep the files api.yml, base-definitions.json and consumer.json in some directory
  2. swagger-codegen generate -i ./api.yml -l java -o ./generated-code
  3. cd generated code
  4. mvn clean install
Related issues/PRs
Suggest a fix/enhancement

The category definitions in base-definitions.json file produces errors in read method generated in Cateogory.java

    @Override
    public Category read(final JsonReader jsonReader) throws IOException {
      Object value = jsonReader.nextString();
      return Category.fromValue(()(value));
    }

This seems to be a bug in codegen. The fix would be just to typecast the object to string i.e. generate the code as follows -
return Category.fromValue((String)(value));

Note: If we directly inline the enum inside some property instead of referring to external file definition, the issue doesn't happen and the inner class enum compiles successfully.

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

Start with api.yml, base-definitions.json, and consumer.json, then run the documented swagger-codegen command and inspect the generated Category.java. Build the generated project with mvn clean install and compare the external-definition enum's read method with the working inline-enum case; done means the generated Java compiles without the reported syntax error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.