swagger-api / swagger-api/swagger-codegen
[Java, Jaxrs Spec] Ommitted type in swagger enum generates corrupted Java Enum
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When you leave out the type in a enum declaration in a swagger file (which apparently is valid), the generated enum class does not compile because codegen inserts empty string as the enum's type.
Swagger-codegen version
2.4.0 snapshot
Swagger declaration file content or url
BackendType:
#type: string
enum:
- ILON
- HUE
leads to:
public enum BackendType {
ILON("ILON"),
HUE("HUE");
private value; <------------------------------------
BackendType( value) { <----------------------------
this.value = value;
}
@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static BackendType fromValue(String text) {
for (BackendType b : BackendType.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Command line used for generation
invoked via gradle, language is jax-rs spec
used options:
{
"interfaceOnly": true,
"dateLibrary":"java8",
"invokerPackage": "ch.package.impl",
"apiPackage" : "ch.package.webapi",
"modelPackage" : "ch.package.webapi.model",
"java8" : "true",
"title" : "App server",
"returnResponse": true
}
add above definition to a yaml file and generate jax-rs spec server code.
Related issues/PRs
to some degree: #8275
Suggest a fix/enhancement
One of those:
- don't generate a value attribute in the enum (and also ommit constructors) if there is no type specified
- Use string as default if no type specified
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Gradle JAX-RS spec generation entry point and reproduce the issue using the provided Swagger YAML enum without a type. Inspect the generated Java enum and ensure the output compiles with a valid enum value type or without the unnecessary value field and constructor.
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
- Mostly clear
- Newbie friendliness
- 45/100