swagger-api / swagger-api/swagger-codegen
[JAVA] Bug generating models with enum discriminator
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
The model for an object with an enum discriminator generates a constructor that causes a compilation error.
The following is an example generated model with some irrelevant code removed.
package com.alianza.client.model;
// imports
public class RingFailoverAction {
/**
* Gets or Sets type
*/
@JsonAdapter(TypeEnum.Adapter.class)
public enum TypeEnum {
// values
private String value;
TypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public static TypeEnum fromValue(String text) {
for (TypeEnum b : TypeEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
public static class Adapter extends TypeAdapter<TypeEnum> {
@Override
public void write(final JsonWriter jsonWriter, final TypeEnum enumeration) throws IOException {
jsonWriter.value(enumeration.getValue());
}
@Override
public TypeEnum read(final JsonReader jsonReader) throws IOException {
String value = jsonReader.nextString();
return TypeEnum.fromValue(String.valueOf(value));
}
}
}
@SerializedName("@type")
private TypeEnum type = null;
public RingFailoverAction() {
this.type = this.getClass().getSimpleName();
}
public RingFailoverAction type(TypeEnum type) {
this.type = type;
return this;
}
/**
* Get type
* @return type
**/
@ApiModelProperty(required = true, value = "")
public TypeEnum getType() {
return type;
}
public void setType(TypeEnum type) {
this.type = type;
}
// equals
// hashCode
// toString
// toIndentedString
}
The statement causing the error is this.type = this.getClass().getSimpleName(), which is attempting to assign a String to a TypeEnum.
Swagger-codegen version
2.4.15 (not a regression)
Swagger declaration file content or url
Full file: https://api.alianza.com/v2/apidocs/swagger.json
Relevant example section
"RingFailoverAction" : {
"type" : "object",
"required" : [ "@type" ],
"discriminator" : "@type",
"properties" : {
"@type" : {
"type" : "string",
"enum" : [ "VoicemailRingFailoverAction", "BusyRingFailoverAction", "ForwardRingFailoverAction" ]
}
}
}
Command line used for generation
java -jar swagger-codegen-cli-2.4.15.jar generate -i https://api.alianza.com/v2/apidocs/swagger.json --api-package com.alianza.client.api --model-package com.alianza.client.model --invoker-package com.alianza.client.invoker --group-id com.alianza -l java -o ./generated_java_client --additional-properties dateLibrary=java8
Steps to reproduce
- Run the command line
- Attempt to compile
Related issues/PRs
https://github.com/swagger-api/swagger-codegen/issues/10196#issuecomment-659673863
Suggest a fix/enhancement
Wrapping this.getClass().getSimpleName() with the enum's valueOf() fixes the compilation error, but I don't know if this is the proper fix because I don't know what the use of that constructor even is.
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 by running the reported swagger-codegen-cli command against the linked Swagger declaration and inspect the Java output for RingFailoverAction. Trace the constructor generation for the enum discriminator, then verify the generated client compiles without assigning a String to TypeEnum.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100