Skip to content

Commit 773177c

Browse files
committed
Added so equals and hashcode is generated in models.
1 parent 715f256 commit 773177c

File tree

14 files changed

+524
-1
lines changed

14 files changed

+524
-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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{@org.openapitools.codegen.CodegenModel m}
22
import com.fasterxml.jackson.annotation.JsonProperty;
3+
import java.util.Objects;
34

45
{#if m.description}
56
/**
@@ -134,6 +135,48 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if serializabl
134135
sb.append("}");
135136
return sb.toString();
136137
}
138+
{#if equals-hashcode}
139+
{#if m.vars.size > 0}
140+
@Override
141+
public boolean equals(Object o) {
142+
if (this == o) return true;
143+
if (o == null || getClass() != o.getClass()) return false;
144+
145+
{m.classname} model = ({m.classname}) o;
146+
147+
{#if m.vars.size == 1}
148+
return Objects.equals({m.vars.0.name}, model.{m.vars.0.name});
149+
{#else}
150+
{#for v in m.vars}
151+
{#if v_isFirst}
152+
return Objects.equals({v.name}, model.{v.name}) &&
153+
{#else if v_isLast}
154+
Objects.equals({v.name}, model.{v.name});
155+
{#else}
156+
Objects.equals({v.name}, model.{v.name}) &&
157+
{/if}
158+
{/for}
159+
{/if}
160+
}
161+
162+
@Override
163+
public int hashCode() {
164+
{#if m.vars.size == 1}
165+
return Objects.hash({m.vars.0.name});
166+
{#else}
167+
{#for v in m.vars}
168+
{#if v_isFirst}
169+
return Objects.hash({v.name},
170+
{#else if v_isLast}
171+
{v.name});
172+
{#else}
173+
{v.name},
174+
{/if}
175+
{/for}
176+
{/if}
177+
}
178+
{/if}
179+
{/if}
137180

138181
/**
139182
* Convert the given object to string with each line indented by 4 spaces
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)