swagger-api / swagger-api/swagger-codegen

jaxrs-restasy-eap generates bad equals functions in some cases

Open
#10,524 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

@Override
 public boolean equals(Object o) {
   if (this == o) {
     return true;
   }
   if (o == null || getClass() != o.getClass()) {
     return false;
   }
   Relation relation = (Relation) o;
   return Objects.equals(relation, relation.relation) &&
       Objects.equals(descriptionL1, relation.descriptionL1) &&
       Objects.equals(descriptionL2, relation.descriptionL2);
 }

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

Command line used for generation
	<plugin>
		<groupId>io.swagger</groupId>
		<artifactId>swagger-codegen-maven-plugin</artifactId>
		<version>2.4.14</version>
		<executions>
			<execution>
				<goals>
					<goal>generate</goal>
				</goals>
			<configuration>
			       <inputSpec>${project.basedir}/src/main/resources/add-on-api-swagger.yaml</inputSpec>
					<language>jaxrs-resteasy-eap</language>
					<configOptions>
						<sourceFolder>src/main/java</sourceFolder>
					</configOptions>
					<modelPackage>com.ec.add.on.api.json</modelPackage>
					<output>${project.basedir}/target/generated-sources/swagger</output>
					<generateApis>false</generateApis>
					<generateApiTests>false</generateApiTests>
					<generateModelTests>false</generateModelTests>
					<generateSupportingFiles>false</generateSupportingFiles>
				</configuration>
			</execution>
		</executions>
	</plugin>
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..

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect the generator templates or entry points for the jaxrs-resteasy-eap model equals implementation. Reproduce both cases with a same-named property and a string/byte schema, then verify generated Java compares the instance property correctly and uses array-aware equality for byte[] values.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.