swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
We are using swagger-codegen-maven-plugin to generate Jackson mappings for a couple of our APIs
We typically use this to support camel-restdsl-swagger-plugin
The problem being that we do se a couple of serious bugs in the generated equals functions.
The first one being that code on this form
Is generated the offending test being the first one where relation is compared to relation.relation what should be compared is
this.relation to relation.relation. In fact it would be safe and probably quite easy to add the this. prefix to the first argument of all 3 Object.equals calls.
The second problem is that the generated equal seems to be bad if binary data are part of the generated class added using
type: "string"
format: "byte"
In this case we do get code like this
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PersonAttachment personAttachment = (PersonAttachment) o;
return Objects.equals(contentType, personAttachment.contentType) &&
Objects.equals(imageData, personAttachment.imageData);
}
Where imageDate is of type byte[]
Objects.equals does not work well in this case (and for arrays in general) since arrays do not override the equals method of Object. java.util.Arrays::equals should be used instead
Swagger-codegen version
We are using swagger-codegen-maven-plugin version 2.4.14 with language set to jaxrs-resteasy-eap with generateApis, generateApiTests, generateModelTests and generateSupportingFiles all set to false.
Swagger declaration file content or url
Our yaml file creating this problem is longish and for copyright reasons I can't publish it but based on my description over it shouldn't be hard to create an example.
The first problem should appear every time a class produced by a definition contains a property with the same name as the class name (different case for first letter but otherwise similar)
The second problem should pop up whenever attributes of type string and format byte are used
For the first problem prefix own properties with this.
For the second problem, use Arrays.equals instead of Objects.equals whenever arrays are compared..
Running SpotBugs on the generated code do reveal this kind of errors. See https://spotbugs.github.io/
I was made aware of the problems due to this tool.
Description
We are using swagger-codegen-maven-plugin to generate Jackson mappings for a couple of our APIs We typically use this to support camel-restdsl-swagger-plugin The problem being that we do se a couple of serious bugs in the generated equals functions. The first one being that code on this form
Is generated the offending test being the first one where relation is compared to relation.relation what should be compared is this.relation to relation.relation. In fact it would be safe and probably quite easy to add the this. prefix to the first argument of all 3 Object.equals calls.
The second problem is that the generated equal seems to be bad if binary data are part of the generated class added using
In this case we do get code like this
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } PersonAttachment personAttachment = (PersonAttachment) o; return Objects.equals(contentType, personAttachment.contentType) && Objects.equals(imageData, personAttachment.imageData); }
Where imageDate is of type byte[] Objects.equals does not work well in this case (and for arrays in general) since arrays do not override the equals method of Object. java.util.Arrays::equals should be used instead
Swagger-codegen version
We are using swagger-codegen-maven-plugin version 2.4.14 with language set to jaxrs-resteasy-eap with generateApis, generateApiTests, generateModelTests and generateSupportingFiles all set to false.
Swagger declaration file content or url
Our yaml file creating this problem is longish and for copyright reasons I can't publish it but based on my description over it shouldn't be hard to create an example. The first problem should appear every time a class produced by a definition contains a property with the same name as the class name (different case for first letter but otherwise similar)
The second problem should pop up whenever attributes of type string and format byte are used
Command line used for generation
Suggest a fix/enhancement
For the first problem prefix own properties with this. For the second problem, use Arrays.equals instead of Objects.equals whenever arrays are compared..