Skip to content

Commit ae3a06e

Browse files
fjtiradohbelmiro
authored andcommitted
Merge pull request #920 from denvitaharen/feature/add-equals-hashcode-client
Added so equals and hashcode is generated in models.
1 parent 9ab50e9 commit ae3a06e

File tree

14 files changed

+533
-1
lines changed

14 files changed

+533
-1
lines changed

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public enum ConfigName {
7474
GENERATE_APIS("generate-apis"),
7575
GENERATE_MODELS("generate-models"),
7676
BEAN_VALIDATION("use-bean-validation"),
77-
SERIALIZABLE_MODEL("serializable-model");
77+
SERIALIZABLE_MODEL("serializable-model"),
78+
EQUALS_HASHCODE("equals-hashcode");
7879

7980
private final String name;
8081

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CommonItemConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,11 @@ public class CommonItemConfig {
172172
*/
173173
@ConfigItem(name = "generate-models")
174174
public Optional<Boolean> generateModels;
175+
176+
/**
177+
* Enable the generation of equals and hashcode in models. If you set this to {@code false}, the models
178+
* will not have equals and hashcode.
179+
*/
180+
@ConfigItem(name = "equals-hashcode")
181+
public Optional<Boolean> equalsHashcode;
175182
}

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ protected void generate(OpenApiGeneratorOptions options) {
320320
getValues(smallRyeConfig, openApiFilePath, CodegenConfig.ConfigName.SERIALIZABLE_MODEL, Boolean.class)
321321
.ifPresent(generator::withSerialiableModel);
322322

323+
getValues(smallRyeConfig, openApiFilePath, CodegenConfig.ConfigName.EQUALS_HASHCODE, Boolean.class)
324+
.ifPresent(generator::withEqualsHashcode);
325+
323326
getValues(smallRyeConfig, openApiFilePath, CodegenConfig.ConfigName.NORMALIZER, String.class, String.class)
324327
.ifPresent(generator::withOpenApiNormalizer);
325328

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ private void setDefaults() {
112112
this.configurator.addAdditionalProperty("use-field-name-in-part-filename", FALSE);
113113
this.configurator.addAdditionalProperty("verbose", FALSE);
114114
this.configurator.addAdditionalProperty(CodegenConstants.SERIALIZABLE_MODEL, FALSE);
115+
this.configurator.addAdditionalProperty("equals-hashcode", TRUE);
115116
}
116117

117118
/**
@@ -202,6 +203,11 @@ public OpenApiClientGeneratorWrapper withSerialiableModel(final Boolean serialia
202203
return this;
203204
}
204205

206+
public OpenApiClientGeneratorWrapper withEqualsHashcode(final Boolean equalsHashcode) {
207+
this.configurator.addAdditionalProperty("equals-hashcode", equalsHashcode);
208+
return this;
209+
}
210+
205211
/**
206212
* Sets the global 'additionalModelTypeAnnotations' setting. If not set this setting will default to empty.
207213
*

client/deployment/src/main/resources/templates/libraries/microprofile/pojo.qute

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,64 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if serializabl
135135
return sb.toString();
136136
}
137137

138+
{#if equals-hashcode}
139+
{#if m.vars.size > 0}
140+
/**
141+
* Compares this object to the specified object. The result is
142+
* \{@code true\} if and only if the argument is not
143+
* \{@code null\} and is a \{@code {m.classname}\} object that
144+
* contains the same values as this object.
145+
*
146+
* @param obj the object to compare with.
147+
* @return \{@code true\} if the objects are the same;
148+
* \{@code false\} otherwise.
149+
**/
150+
@Override
151+
public boolean equals(Object obj) {
152+
if (this == obj) return true;
153+
if (obj == null || getClass() != obj.getClass()) return false;
154+
155+
{m.classname} model = ({m.classname}) obj;
156+
157+
{#if m.vars.size == 1}
158+
return java.util.Objects.equals({m.vars.0.name}, model.{m.vars.0.name});
159+
{#else}
160+
{#for v in m.vars}
161+
{#if v_isFirst}
162+
return java.util.Objects.equals({v.name}, model.{v.name}) &&
163+
{#else if v_isLast}
164+
java.util.Objects.equals({v.name}, model.{v.name});
165+
{#else}
166+
java.util.Objects.equals({v.name}, model.{v.name}) &&
167+
{/if}
168+
{/for}
169+
{/if}
170+
}
171+
172+
/**
173+
* Returns a hash code for a \{@code {m.classname}\}.
174+
*
175+
* @return a hash code value for a \{@code {m.classname}\}.
176+
**/
177+
@Override
178+
public int hashCode() {
179+
{#if m.vars.size == 1}
180+
return java.util.Objects.hash({m.vars.0.name});
181+
{#else}
182+
{#for v in m.vars}
183+
{#if v_isFirst}
184+
return java.util.Objects.hash({v.name},
185+
{#else if v_isLast}
186+
{v.name});
187+
{#else}
188+
{v.name},
189+
{/if}
190+
{/for}
191+
{/if}
192+
}
193+
{/if}
194+
{/if}
195+
138196
/**
139197
* Convert the given object to string with each line indented by 4 spaces
140198
* (except the first line).
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<parent>
4+
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
5+
<groupId>io.quarkiverse.openapi.generator</groupId>
6+
<version>3.0.0-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<artifactId>quarkus-openapi-generator-it-equals-hashcode</artifactId>
11+
<name>Quarkus - Openapi Generator - Integration Tests - Client - Equals hashcode</name>
12+
<description>Example project for general usage</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.quarkiverse.openapi.generator</groupId>
17+
<artifactId>quarkus-openapi-generator</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.assertj</groupId>
21+
<artifactId>assertj-core</artifactId>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>io.quarkus</groupId>
26+
<artifactId>quarkus-junit5</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>io.quarkus</groupId>
34+
<artifactId>quarkus-maven-plugin</artifactId>
35+
<extensions>true</extensions>
36+
<executions>
37+
<execution>
38+
<goals>
39+
<goal>build</goal>
40+
<goal>generate-code</goal>
41+
<goal>generate-code-tests</goal>
42+
</goals>
43+
</execution>
44+
</executions>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
<profiles>
49+
<profile>
50+
<id>native-image</id>
51+
<activation>
52+
<property>
53+
<name>native</name>
54+
</property>
55+
</activation>
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<artifactId>maven-surefire-plugin</artifactId>
60+
<configuration>
61+
<skipTests>${native.surefire.skip}</skipTests>
62+
</configuration>
63+
</plugin>
64+
<plugin>
65+
<artifactId>maven-failsafe-plugin</artifactId>
66+
<executions>
67+
<execution>
68+
<goals>
69+
<goal>integration-test</goal>
70+
<goal>verify</goal>
71+
</goals>
72+
<configuration>
73+
<systemPropertyVariables>
74+
<native.image.path>
75+
${project.build.directory}/${project.build.finalName}-runner
76+
</native.image.path>
77+
<java.util.logging.manager>org.jboss.logmanager.LogManager
78+
</java.util.logging.manager>
79+
<maven.home>${maven.home}</maven.home>
80+
</systemPropertyVariables>
81+
</configuration>
82+
</execution>
83+
</executions>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
<properties>
88+
<quarkus.package.type>native</quarkus.package.type>
89+
</properties>
90+
</profile>
91+
</profiles>
92+
93+
</project>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "Animals - OpenAPI 3.0",
5+
"version": "1.0.5"
6+
},
7+
"servers": [
8+
{
9+
"url": "/api/v3"
10+
}
11+
],
12+
"tags": [
13+
{
14+
"name": "primate",
15+
"description": "Everything about Primates"
16+
}
17+
],
18+
"paths": {
19+
"/primate/{id}": {
20+
"get": {
21+
"tags": [
22+
"primate"
23+
],
24+
"summary": "Find primate by ID",
25+
"description": "Returns a single primate",
26+
"operationId": "getPrimateById",
27+
"parameters": [
28+
{
29+
"name": "id",
30+
"in": "path",
31+
"description": "ID of primate to return",
32+
"required": true,
33+
"schema": {
34+
"type": "integer",
35+
"format": "int64"
36+
}
37+
}
38+
],
39+
"responses": {
40+
"200": {
41+
"description": "successful operation",
42+
"content": {
43+
"application/json": {
44+
"schema": {
45+
"$ref": "#/components/schemas/Primate"
46+
}
47+
}
48+
}
49+
},
50+
"400": {
51+
"description": "Invalid ID supplied"
52+
},
53+
"404": {
54+
"description": "Primate not found"
55+
}
56+
}
57+
}
58+
}
59+
},
60+
"components": {
61+
"schemas": {
62+
"Animal": {
63+
"type": "object",
64+
"properties": {
65+
"born": {
66+
"type": "string",
67+
"description": "Dated Base extension.",
68+
"format": "date-time"
69+
},
70+
"deceased": {
71+
"type": "string",
72+
"description": "Dated Base extension.",
73+
"format": "date-time"
74+
}
75+
},
76+
"xml": {
77+
"name": "animal"
78+
}
79+
},
80+
"Mammal": {
81+
"type": "object",
82+
"allOf": [ {
83+
"$ref": "#/components/schemas/Animal"
84+
} ],
85+
"properties": {
86+
"gender": {
87+
"type": "string",
88+
"enum": [
89+
"female",
90+
"male"
91+
]
92+
}
93+
},
94+
"xml": {
95+
"name": "mammal"
96+
}
97+
},
98+
"Primate": {
99+
"required": [
100+
"name"
101+
],
102+
"type": "object",
103+
"allOf": [
104+
{
105+
"$ref": "#/components/schemas/Mammal"
106+
}
107+
],
108+
"properties": {
109+
"id": {
110+
"type": "integer",
111+
"format": "int64",
112+
"example": 10
113+
},
114+
"name": {
115+
"type": "string",
116+
"example": "jane doe"
117+
}
118+
},
119+
"xml": {
120+
"name": "primate"
121+
}
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)