Skip to content

Commit bbf896e

Browse files
authored
Merge pull request #10 from bsayli/feature/openapi-custom-type-mapping
🧹 refactor(client): remove redundant DTOs & clean generated imports (#9)
2 parents afea9b2 + 9e06db0 commit bbf896e

File tree

17 files changed

+173
-52
lines changed

17 files changed

+173
-52
lines changed

customer-service-client/.openapi-generator-ignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# existing patterns
12
**/.github/**
23
**/gradle/**
34
**/.gradle/**
@@ -15,4 +16,12 @@
1516
**/git_push.sh
1617
**/README.md
1718
**/.openapi-generator/**
18-
!src/gen/java/main/**
19+
!src/gen/java/**
20+
21+
# --- Custom additions for generated DTO cleanup ---
22+
# ignore these generated models regardless of location
23+
**/src/gen/java/**/generated/dto/Page*.java
24+
**/src/gen/java/**/generated/dto/ServiceResponse.java
25+
**/src/gen/java/**/generated/dto/ServiceResponseVoid.java
26+
**/src/gen/java/**/generated/dto/Meta.java
27+
**/src/gen/java/**/generated/dto/Sort.java

customer-service-client/README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ public class CustomerClientAdapterImpl implements CustomerClientAdapter {
178178
@Override
179179
public ServiceClientResponse<Page<CustomerDto>> getCustomers(
180180
String name, String email, Integer page, Integer size,
181-
SortField sortBy, SortDirection direction) {
181+
ClientSortField sortBy, ClientSortDirection direction) {
182182
return api.getCustomers(
183183
name, email, page, size,
184-
sortBy != null ? sortBy.value() : SortField.CUSTOMER_ID.value(),
185-
direction != null ? direction.value() : SortDirection.ASC.value());
184+
sortBy != null ? sortBy.value() : ClientSortField.CUSTOMER_ID.value(),
185+
direction != null ? direction.value() : ClientSortDirection.ASC.value());
186186
}
187187
}
188188
```
@@ -252,6 +252,22 @@ try {
252252

253253
---
254254

255+
### 🧹 Ignoring Redundant Generated DTOs
256+
257+
The following patterns in [`.openapi-generator-ignore`](.openapi-generator-ignore) prevent redundant DTOs from being regenerated.
258+
These classes already exist in the shared `common` package and are excluded from code generation.
259+
260+
```bash
261+
# --- Custom additions for generated DTO cleanup ---
262+
**/src/gen/java/**/generated/dto/Page*.java
263+
**/src/gen/java/**/generated/dto/ServiceResponse.java
264+
**/src/gen/java/**/generated/dto/ServiceResponseVoid.java
265+
**/src/gen/java/**/generated/dto/Meta.java
266+
**/src/gen/java/**/generated/dto/Sort.java
267+
```
268+
269+
---
270+
255271
## 📚 Notes
256272

257273
* **Toolchain:** Java 21, Spring Boot 3.4.10, OpenAPI Generator 7.16.0

customer-service-client/pom.xml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.bsayli</groupId>
88
<artifactId>customer-service-client</artifactId>
9-
<version>0.7.0</version>
9+
<version>0.7.2</version>
1010
<name>customer-service-client</name>
1111
<description>Generated client (RestClient) using generics-aware OpenAPI templates</description>
1212
<packaging>jar</packaging>
@@ -20,7 +20,7 @@
2020
<openapi.generator.version>7.16.0</openapi.generator.version>
2121

2222
<jakarta.validation.version>3.1.1</jakarta.validation.version>
23-
<jakarta.annotation-api.version>3.0.0</jakarta.annotation-api.version>
23+
<jakarta.annotation-api.version>${spotless-maven-plugin.version}</jakarta.annotation-api.version>
2424
<mockwebserver.version>5.1.0</mockwebserver.version>
2525
<httpclient5.version>5.5</httpclient5.version>
2626
<junit-jupiter.version>5.13.4</junit-jupiter.version>
@@ -32,9 +32,11 @@
3232
<build.helper.plugin.version>3.6.0</build.helper.plugin.version>
3333
<maven.resources.plugin.version>3.3.1</maven.resources.plugin.version>
3434
<maven.dependency.plugin.version>3.8.1</maven.dependency.plugin.version>
35+
<spotless-maven-plugin.version>3.0.0</spotless-maven-plugin.version>
3536

3637
<openapi.templates.upstream>${project.build.directory}/upstream-templates</openapi.templates.upstream>
3738
<openapi.templates.effective>${project.build.directory}/effective-templates</openapi.templates.effective>
39+
3840
</properties>
3941

4042
<dependencies>
@@ -217,6 +219,7 @@
217219
<additionalProperty>commonPackage=io.github.bsayli.openapi.client.common
218220
</additionalProperty>
219221
</additionalProperties>
222+
<ignoreFileOverride>${project.basedir}/.openapi-generator-ignore</ignoreFileOverride>
220223
</configuration>
221224
</execution>
222225
</executions>
@@ -241,7 +244,31 @@
241244
</execution>
242245
</executions>
243246
</plugin>
247+
<plugin>
248+
<groupId>com.diffplug.spotless</groupId>
249+
<artifactId>spotless-maven-plugin</artifactId>
250+
<version>${spotless-maven-plugin.version}</version>
244251

252+
<configuration>
253+
<java>
254+
<includes>
255+
<include>target/generated-sources/openapi/src/gen/java/**/*.java</include>
256+
</includes>
257+
<removeUnusedImports>
258+
<engine>cleanthat-javaparser-unnecessaryimport</engine>
259+
</removeUnusedImports>
260+
</java>
261+
</configuration>
262+
<executions>
263+
<execution>
264+
<id>spotless-apply-generated</id>
265+
<phase>process-sources</phase>
266+
<goals>
267+
<goal>apply</goal>
268+
</goals>
269+
</execution>
270+
</executions>
271+
</plugin>
245272
<plugin>
246273
<groupId>org.apache.maven.plugins</groupId>
247274
<artifactId>maven-compiler-plugin</artifactId>

customer-service-client/src/main/java/io/github/bsayli/openapi/client/adapter/CustomerClientAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import io.github.bsayli.openapi.client.common.Page;
44
import io.github.bsayli.openapi.client.common.ServiceClientResponse;
5-
import io.github.bsayli.openapi.client.common.sort.SortDirection;
6-
import io.github.bsayli.openapi.client.common.sort.SortField;
5+
import io.github.bsayli.openapi.client.common.sort.ClientSortDirection;
6+
import io.github.bsayli.openapi.client.common.sort.ClientSortField;
77
import io.github.bsayli.openapi.client.generated.dto.CustomerCreateRequest;
88
import io.github.bsayli.openapi.client.generated.dto.CustomerDeleteResponse;
99
import io.github.bsayli.openapi.client.generated.dto.CustomerDto;
@@ -22,8 +22,8 @@ ServiceClientResponse<Page<CustomerDto>> getCustomers(
2222
String email,
2323
Integer page,
2424
Integer size,
25-
SortField sortBy,
26-
SortDirection direction);
25+
ClientSortField sortBy,
26+
ClientSortDirection direction);
2727

2828
ServiceClientResponse<CustomerDto> updateCustomer(
2929
Integer customerId, CustomerUpdateRequest request);

customer-service-client/src/main/java/io/github/bsayli/openapi/client/adapter/config/CustomerApiClientConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
@Configuration
2323
public class CustomerApiClientConfig {
24-
2524

2625
@Bean
2726
RestClientCustomizer problemDetailStatusHandler(ObjectMapper om) {

customer-service-client/src/main/java/io/github/bsayli/openapi/client/adapter/impl/CustomerClientAdapterImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import io.github.bsayli.openapi.client.adapter.CustomerClientAdapter;
44
import io.github.bsayli.openapi.client.common.Page;
55
import io.github.bsayli.openapi.client.common.ServiceClientResponse;
6-
import io.github.bsayli.openapi.client.common.sort.SortDirection;
7-
import io.github.bsayli.openapi.client.common.sort.SortField;
6+
import io.github.bsayli.openapi.client.common.sort.ClientSortDirection;
7+
import io.github.bsayli.openapi.client.common.sort.ClientSortField;
88
import io.github.bsayli.openapi.client.generated.api.CustomerControllerApi;
99
import io.github.bsayli.openapi.client.generated.dto.*;
1010
import org.springframework.stereotype.Service;
@@ -30,7 +30,7 @@ public ServiceClientResponse<CustomerDto> getCustomer(Integer customerId) {
3030

3131
@Override
3232
public ServiceClientResponse<Page<CustomerDto>> getCustomers() {
33-
return getCustomers(null, null, 0, 5, SortField.CUSTOMER_ID, SortDirection.ASC);
33+
return getCustomers(null, null, 0, 5, ClientSortField.CUSTOMER_ID, ClientSortDirection.ASC);
3434
}
3535

3636
@Override
@@ -39,16 +39,16 @@ public ServiceClientResponse<Page<CustomerDto>> getCustomers(
3939
String email,
4040
Integer page,
4141
Integer size,
42-
SortField sortBy,
43-
SortDirection direction) {
42+
ClientSortField sortBy,
43+
ClientSortDirection direction) {
4444

4545
return api.getCustomers(
4646
name,
4747
email,
4848
page,
4949
size,
50-
sortBy != null ? sortBy.value() : SortField.CUSTOMER_ID.value(),
51-
direction != null ? direction.value() : SortDirection.ASC.value());
50+
sortBy != null ? sortBy.value() : ClientSortField.CUSTOMER_ID.value(),
51+
direction != null ? direction.value() : ClientSortDirection.ASC.value());
5252
}
5353

5454
@Override

customer-service-client/src/main/java/io/github/bsayli/openapi/client/adapter/support/ProblemDetailSupport.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private ProblemDetailSupport() {}
2020
public static ProblemDetail extract(ObjectMapper om, ClientHttpResponse response) {
2121
ProblemDetail pd;
2222
MediaType contentType =
23-
Optional.ofNullable(response.getHeaders().getContentType()).orElse(MediaType.ALL);
23+
Optional.ofNullable(response.getHeaders().getContentType()).orElse(MediaType.ALL);
2424
HttpStatusCode status;
2525

2626
try {
@@ -39,10 +39,10 @@ public static ProblemDetail extract(ObjectMapper om, ClientHttpResponse response
3939
}
4040
} catch (IOException e) {
4141
log.warn(
42-
"Unable to deserialize ProblemDetail (status={}, contentType={}); using generic fallback",
43-
status,
44-
contentType,
45-
e);
42+
"Unable to deserialize ProblemDetail (status={}, contentType={}); using generic fallback",
43+
status,
44+
contentType,
45+
e);
4646
pd = fallback(status, "Unparseable problem response");
4747
} catch (Exception e) {
4848
log.warn("Unexpected error while parsing ProblemDetail", e);
@@ -59,4 +59,4 @@ private static ProblemDetail fallback(HttpStatusCode status, String detail) {
5959
pd.setDetail(detail);
6060
return pd;
6161
}
62-
}
62+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package io.github.bsayli.openapi.client.common.sort;
22

3-
public record ClientSort(SortField field, SortDirection direction) {
3+
public record ClientSort(ClientSortField field, ClientSortDirection direction) {
44

55
public ClientSort {
66
if (field == null) {
7-
field = SortField.CUSTOMER_ID;
7+
field = ClientSortField.CUSTOMER_ID;
88
}
99
if (direction == null) {
10-
direction = SortDirection.ASC;
10+
direction = ClientSortDirection.ASC;
1111
}
1212
}
1313
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.github.bsayli.openapi.client.common.sort;
22

3-
public enum SortDirection {
3+
public enum ClientSortDirection {
44
ASC("asc"),
55
DESC("desc");
66

77
private final String value;
88

9-
SortDirection(String value) {
9+
ClientSortDirection(String value) {
1010
this.value = value;
1111
}
1212

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package io.github.bsayli.openapi.client.common.sort;
22

3-
public enum SortField {
3+
public enum ClientSortField {
44
CUSTOMER_ID("customerId"),
55
NAME("name"),
66
EMAIL("email");
77

88
private final String value;
99

10-
SortField(String value) {
10+
ClientSortField(String value) {
1111
this.value = value;
1212
}
1313

0 commit comments

Comments
 (0)