Skip to content

Commit 04fb9c6

Browse files
Rafa Castelblanquefrantuma
authored andcommitted
issue 7355 Bug generating equals method for a child class with no additional field
1 parent 4b7a8d7 commit 04fb9c6

File tree

6 files changed

+118
-4
lines changed

6 files changed

+118
-4
lines changed

modules/swagger-codegen/src/main/resources/Java/pojo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
197197
return {{#vars}}ObjectUtils.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
198198
{{/hasMore}}{{/vars}}{{#parent}} &&
199199
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
200-
return true;{{/hasVars}}
200+
return super.equals(o);{{/hasVars}}
201201
}
202202

203203
@Override

modules/swagger-codegen/src/main/resources/JavaJaxRS/pojo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
111111
return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
112112
{{/hasMore}}{{/vars}}{{#parent}} &&
113113
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
114-
return true;{{/hasVars}}
114+
return super.equals(o);{{/hasVars}}
115115
}
116116

117117
@Override

modules/swagger-codegen/src/main/resources/JavaPlayFramework/pojo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
105105
return {{#vars}}Objects.equals({{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
106106
{{/hasMore}}{{/vars}}{{#parent}} &&
107107
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
108-
return true;{{/hasVars}}
108+
return super.equals(o);{{/hasVars}}
109109
}
110110

111111
@Override

modules/swagger-codegen/src/main/resources/JavaSpring/pojo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
133133
return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
134134
{{/hasMore}}{{/vars}}{{#parent}} &&
135135
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
136-
return true;{{/hasVars}}
136+
return super.equals(o);{{/hasVars}}
137137
}
138138

139139
@Override
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.swagger.codegen.spring;
2+
3+
import io.swagger.codegen.ClientOptInput;
4+
import io.swagger.codegen.ClientOpts;
5+
import io.swagger.codegen.CodegenConfig;
6+
import io.swagger.codegen.DefaultGenerator;
7+
import io.swagger.codegen.languages.SpringCodegen;
8+
import io.swagger.models.Swagger;
9+
import io.swagger.parser.SwaggerParser;
10+
import io.swagger.parser.util.ParseOptions;
11+
import org.apache.commons.io.FileUtils;
12+
import org.junit.rules.TemporaryFolder;
13+
import org.testng.annotations.AfterMethod;
14+
import org.testng.annotations.BeforeMethod;
15+
import org.testng.annotations.Test;
16+
17+
import java.io.File;
18+
import java.io.IOException;
19+
20+
import static org.testng.Assert.assertFalse;
21+
import static org.testng.Assert.assertTrue;
22+
23+
public class SpringCodegenTest {
24+
25+
public TemporaryFolder folder = new TemporaryFolder();
26+
27+
public static final String EQUALS_SUPER = "@Override\n" +
28+
" public boolean equals(java.lang.Object o) {\n" +
29+
" if (this == o) {\n" +
30+
" return true;\n" +
31+
" }\n" +
32+
" if (o == null || getClass() != o.getClass()) {\n" +
33+
" return false;\n" +
34+
" }\n" +
35+
" return super.equals(o);\n" +
36+
" }";
37+
38+
@BeforeMethod
39+
public void setUp() throws Exception {
40+
folder.create();
41+
}
42+
43+
@AfterMethod
44+
public void tearDown() throws Exception {
45+
folder.delete();
46+
}
47+
48+
@Test
49+
public void testIssue7355() throws Exception {
50+
final File outputFolder = folder.getRoot();
51+
final ParseOptions parseOptions = new ParseOptions();
52+
parseOptions.setFlatten(true);
53+
54+
final Swagger swagger = new SwaggerParser().read("2_0/issue-7355.yaml", null, parseOptions);
55+
final CodegenConfig codegenConfig = new SpringCodegen();
56+
codegenConfig.setLibrary("spring-boot");
57+
codegenConfig.setOutputDir(outputFolder.getAbsolutePath());
58+
59+
final ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);
60+
61+
//generate
62+
new DefaultGenerator().opts(clientOptInput).generate();
63+
64+
// Check simple type
65+
final File modelWithoutProps = new File(outputFolder, "src/main/java/io/swagger/model/ModelWithoutProps.java");
66+
assertTrue(modelWithoutProps.exists());
67+
assertTrue(containsString(modelWithoutProps, EQUALS_SUPER));
68+
69+
// Check simple type
70+
final File modelWithProps = new File(outputFolder, "src/main/java/io/swagger/model/ModelWithProps.java");
71+
assertTrue(modelWithProps.exists());
72+
assertTrue(containsString(modelWithProps, "@Override\n public boolean equals(java.lang.Object o) {"));
73+
assertFalse(containsString(modelWithProps, EQUALS_SUPER));
74+
}
75+
76+
private boolean containsString(File file, String search) throws IOException {
77+
return trim(FileUtils.readFileToString(file)).contains(trim(search));
78+
}
79+
80+
private String trim(String value) {
81+
return value.replace(" ", "").replace("\n", "").replace("\r", "");
82+
}
83+
84+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
swagger: '2.0'
2+
info:
3+
title: API contract for testing the "equals" method generation in POJOs
4+
description: API for testing
5+
version: v1
6+
consumes:
7+
- application/json; charset=utf-8
8+
produces:
9+
- application/json; charset=utf-8
10+
paths:
11+
'/dummyPath':
12+
get:
13+
responses:
14+
'200':
15+
description: OK
16+
definitions:
17+
ModelWithoutProps:
18+
type: array
19+
items:
20+
properties:
21+
value:
22+
type: string
23+
ModelWithProps:
24+
type: object
25+
allOf:
26+
- $ref: '#/definitions/ArrayWithoutProps'
27+
- type: object
28+
properties:
29+
anotherValue:
30+
type: string

0 commit comments

Comments
 (0)