swagger-api / swagger-api/swagger-codegen
[JAVA] "allOf" with an enum in schema does not generate in Java properly
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
I am working with a third party to convert their schema to Java. Unfortunately, the way they have done their enums has rendered them nearly unusable in Java. Please note that I cannot alter this schema. While I have played around with it for debugging purposes, I ultimately cannot change it for production.
Swagger-codegen version
3.034
Swagger declaration file content or url
The third party has provided both a json and yml version of their schema with equal results on my part.
schemas:
Employee:
type: object
properties:
EmployeeGender:
$ref: '#/components/schemas/Gender'
Gender:
allOf:
- type: string
- enum:
- Female
- Male
And here is the same thing in JSON:
"components": {
"schemas": {
"Employee": {
"type": "object",
"properties": {
"EmployeeGender": {"$ref": "#/components/schemas/Gender"}
}
},
"Gender": {
"allOf": [
{"type": "string"},
{
"enum": [
"Female",
"Male"
]
}
]
}
}
It then presents me with Java that has no usable enums.
public class EmployeeGender {
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return true;
}
@Override
public int hashCode() {
return Objects.hash();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class EmployeeGender {\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
Obviously I would rather have something like:
public enum EmployeeGender {
Female("Female"),
Male("Male");
Command line used for generation
I have not used the command line, as this is generated through maven using Java 17. I will paste the relevant properties in my pom.xml:
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.13.3</version>
</dependency>
And the plugin:
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.34</version>
<executions>
<execution>
<id>aproject</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<inputSpec>${project.basedir}/resources/schema.yml</inputSpec>
<language>java</language>
<library>resttemplate</library>
<generateApis>false</generateApis>
<apiPackage>this.is.a.package</apiPackage>
<modelPackage>this.is.a.package</modelPackage>
<invokerPackage>this.is.a.package</invokerPackage>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateSupportingFiles>true</generateSupportingFiles>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<dateLibrary>legacy</dateLibrary>
</configOptions>
</configuration>
</plugin>
I then run a mvn clean swagger-codegen:generate or maven clean compile or whatever I need.
Steps to reproduce
- Set up schema as shown and pom.xml as shown, either using the json or yml file for generation. (Both have the same results for me)
- Run mvn clean swagger-codegen:generate or maven clean compile or any other maven command that would generate the code.
- Check the relevant generated Java files. I would expect the enums to generate enum Java files instead of whatever mess they end up generating.
Related issues/PRs
Issue 1746
Issue 2450
Issue 10446
Suggest a fix/enhancement
From my testing and messing with the schemas, removing the "allOf" from the enum in the schema will produce the Java class I am looking for. However, I cannot alter the schema for production. I need a fix within the code generation itself.
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
Reproduce the generation with the supplied schema.yml or JSON and pom.xml using the swagger-codegen Maven plugin. Inspect the generated Java for the allOf enum case and compare it with the expected Female and Male enum values; done means the schema generates a usable Java enum rather than an empty model class.
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
- Clearly specified
- Newbie friendliness
- 35/100