OpenAPITools / OpenAPITools/openapi-generator
[NOT A BUG] [SHOULD BE CLOSED] Incomplete `JsonSubTypes` decorator support returns `null` by "subcasting"
Nobody has claimed this yet.
- 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?
- [Optional] Bounty to sponsor the fix (example)
Description
The source OpenAPI specification contains subtypes and enums.
I have possible unresolved cyclic dependency in generated models:
Both parent and child classes has the property (getter) which refers enum declared in the same class.
The generated code cannot be compiled properly.
public class UnknownTOV extends TOVCreation {
/**
* Gets or Sets unknown
*/
public enum UnknownEnum {
UNKNOWNTOV("UnknownTOV");
private String value;
UnknownEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static UnknownEnum fromValue(String text) {
for (UnknownEnum b : UnknownEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + text + "'");
}
}
@JsonProperty("unknown")
private UnknownEnum unknown;
public UnknownTOV unknown(UnknownEnum unknown) {
this.unknown = unknown;
return this;
}
/**
* Get unknown
* @return unknown
**/
@ApiModelProperty(required = true, value = "")
public UnknownEnum getUnknown() {
return unknown;
}
public void setUnknown(UnknownEnum unknown) {
this.unknown = unknown;
}
....
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes({
....
@JsonSubTypes.Type(value = UnknownTOV.class, name = "UnknownTOV"),
....
})
public class TOVCreation {
/**
* Gets or Sets unknown
*/
public enum UnknownEnum {
UNKNOWNTOV("UnknownTOV");
private String value;
UnknownEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static UnknownEnum fromValue(String text) {
for (UnknownEnum b : UnknownEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + text + "'");
}
}
@JsonProperty("unknown")
private UnknownEnum unknown;
public TOVCreation unknown(UnknownEnum unknown) {
this.unknown = unknown;
return this;
}
/**
* Get unknown
* @return unknown
**/
@ApiModelProperty(value = "")
// Incompatible types.
// Required:
// com.xxx.mmmm.persistent.UnknownTOV.UnknownEnum
// Found:
// com.xxx.mmmm.persistent.TOVCreation.UnknownEnum
public UnknownTOV.UnknownEnum getUnknown() {
return unknown;
}
public void setUnknown(UnknownEnum unknown) {
this.unknown = unknown;
}
I am unable to fix this issue to make code compilable.
As I mentioned @Override cannot help in this case.
The IntelliJ IDEA red bulb 💡 fix helper causes infinite loop.
The only way I found is comment out problem code.
How can I resolve issue in right way
openapi-generator version
4.0.0-SNAPSHOT
OpenAPI declaration file content or url
TOVCreation:
type: object
discriminator: type
properties:
unknown:
type: string
enum:
- UnknownTOV
UnknownTOV:
allOf:
- $ref: '#/definitions/TOVCreation'
- type: object
properties:
unknown:
type: string
enum:
- UnknownTOV
required:
- unknown
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 generating Java models from the supplied YAML and compiling the TOVCreation and UnknownTOV classes shown in the report. Compare the duplicated unknown enum and accessor types; done means the generated model set compiles without incompatible enum types while retaining the discriminator and subtype behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100