Skip to content

Commit 2b00bab

Browse files
authored
[Spring] Optimize and omit unnecessary imports on Api in ApiController templates (#22164)
* Optimize imports * remove unnecessary blank line * revert unintended indentation
1 parent ccd43a5 commit 2b00bab

File tree

195 files changed

+24
-1346
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+24
-1346
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,8 @@ public void processOpts() {
568568
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiUtil.java"));
569569
}
570570

571-
if (!delegatePattern || delegateMethod) {
572-
additionalProperties.put("jdk8-no-delegate", true);
573-
}
574-
575571
if (delegatePattern && !delegateMethod) {
576-
additionalProperties.put("isDelegate", "true");
572+
additionalProperties.put("isDelegate", true);
577573
apiTemplateFiles.put("apiDelegate.mustache", "Delegate.java");
578574
}
579575

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ import io.swagger.v3.oas.annotations.media.ExampleObject;
2424
{{#swagger1AnnotationLibrary}}
2525
import io.swagger.annotations.*;
2626
{{/swagger1AnnotationLibrary}}
27-
{{#jdk8-no-delegate}}
27+
{{^isDelegate}}
28+
{{#jdk8-default-interface}}
2829
{{#virtualService}}
2930
import io.virtualan.annotation.ApiVirtual;
3031
import io.virtualan.annotation.VirtualService;
3132
{{/virtualService}}
3233
import org.springframework.http.HttpStatus;
3334
import org.springframework.http.MediaType;
34-
{{/jdk8-no-delegate}}
35+
{{/jdk8-default-interface}}
36+
{{/isDelegate}}
3537
{{^useResponseEntity}}
3638
import org.springframework.http.HttpStatus;
3739
{{/useResponseEntity}}
@@ -48,11 +50,13 @@ import org.springframework.web.bind.annotation.RestController;
4850
{{/useResponseEntity}}
4951
{{/useSpringController}}
5052
import org.springframework.web.bind.annotation.*;
51-
{{#jdk8-no-delegate}}
52-
{{^reactive}}
53+
{{#jdk8-default-interface}}
54+
{{^isDelegate}}
55+
{{^reactive}}
5356
import org.springframework.web.context.request.NativeWebRequest;
54-
{{/reactive}}
55-
{{/jdk8-no-delegate}}
57+
{{/reactive}}
58+
{{/isDelegate}}
59+
{{/jdk8-default-interface}}
5660
import org.springframework.web.multipart.MultipartFile;
5761
{{#reactive}}
5862
import org.springframework.web.server.ServerWebExchange;
@@ -67,14 +71,14 @@ import {{javaxPackage}}.validation.constraints.*;
6771
{{/useBeanValidation}}
6872
import java.util.List;
6973
import java.util.Map;
70-
{{#jdk8-no-delegate}}
74+
{{^isDelegate}}
7175
import java.util.Optional;
72-
{{/jdk8-no-delegate}}
73-
{{^jdk8-no-delegate}}
76+
{{/isDelegate}}
77+
{{#isDelegate}}
7478
{{#useOptional}}
7579
import java.util.Optional;
7680
{{/useOptional}}
77-
{{/jdk8-no-delegate}}
81+
{{/isDelegate}}
7882
{{#async}}
7983
import java.util.concurrent.CompletableFuture;
8084
{{/async}}
@@ -261,7 +265,9 @@ public interface {{classname}} {
261265
{{#vendorExtensions.x-operation-extra-annotation}}
262266
{{{.}}}
263267
{{/vendorExtensions.x-operation-extra-annotation}}
264-
{{#vendorExtensions.x-sse}}@ResponseBody{{/vendorExtensions.x-sse}}
268+
{{#vendorExtensions.x-sse}}
269+
@ResponseBody
270+
{{/vendorExtensions.x-sse}}
265271
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{>responseType}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}(
266272
{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},
267273
{{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}},

modules/openapi-generator/src/main/resources/java-pkmst/apiController.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package {{package}};
22

3-
{{^jdk8-no-delegate}}
3+
{{^isDelegate}}
44
{{#imports}}import {{import}};
55
{{/imports}}
66

77
import io.swagger.annotations.*;
88

99
import org.springframework.http.HttpStatus;
1010
import org.springframework.http.ResponseEntity;
11-
{{/jdk8-no-delegate}}
11+
{{/isDelegate}}
1212
import org.springframework.stereotype.Controller;
13-
{{^jdk8-no-delegate}}
13+
{{^isDelegate}}
1414
import org.springframework.web.bind.annotation.PathVariable;
1515
import org.springframework.web.bind.annotation.RequestBody;
1616
import org.springframework.web.bind.annotation.RequestHeader;
@@ -26,7 +26,7 @@ import java.util.Optional;
2626
{{#async}}
2727
import java.util.concurrent.Callable;
2828
{{/async}}
29-
{{/jdk8-no-delegate}}
29+
{{/isDelegate}}
3030
{{^useSpringCloudClient}}
3131
import com.fasterxml.jackson.databind.ObjectMapper;
3232
import java.io.IOException;
@@ -60,7 +60,7 @@ public class {{classname}}Controller implements {{classname}} {
6060
}
6161

6262
{{/isDelegate}}
63-
{{^jdk8-no-delegate}}
63+
{{^isDelegate}}
6464
{{#operation}}
6565
public {{#async}}Callable<{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}},
6666
{{/allParams}}@RequestHeader(value = "Accept", required = false) String accept) throws Exception {
@@ -111,6 +111,6 @@ public class {{classname}}Controller implements {{classname}} {
111111
}
112112

113113
{{/operation}}
114-
{{/jdk8-no-delegate}}
114+
{{/isDelegate}}
115115
}
116116
{{/operations}}

samples/client/petstore/spring-cloud-auth/src/main/java/org/openapitools/api/SomeApi.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
*/
66
package org.openapitools.api;
77

8-
import org.springframework.http.HttpStatus;
9-
import org.springframework.http.MediaType;
108
import org.springframework.http.ResponseEntity;
119
import org.springframework.validation.annotation.Validated;
1210
import org.springframework.web.bind.annotation.*;
13-
import org.springframework.web.context.request.NativeWebRequest;
1411
import org.springframework.web.multipart.MultipartFile;
1512

1613
import javax.validation.Valid;
@@ -34,7 +31,6 @@ public interface SomeApi {
3431
method = RequestMethod.GET,
3532
value = SomeApi.PATH_SOME_ENDPOINT_GET
3633
)
37-
3834
ResponseEntity<Void> someEndpointGet(
3935

4036
);

samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/api/DefaultApi.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
import org.springframework.lang.Nullable;
1111
import java.time.OffsetDateTime;
1212
import io.swagger.annotations.*;
13-
import org.springframework.http.HttpStatus;
14-
import org.springframework.http.MediaType;
1513
import org.springframework.http.ResponseEntity;
1614
import org.springframework.validation.annotation.Validated;
1715
import org.springframework.web.bind.annotation.*;
18-
import org.springframework.web.context.request.NativeWebRequest;
1916
import org.springframework.web.multipart.MultipartFile;
2017

2118
import javax.validation.Valid;
@@ -52,7 +49,6 @@ public interface DefaultApi {
5249
method = RequestMethod.GET,
5350
value = DefaultApi.PATH_GET
5451
)
55-
5652
ResponseEntity<Void> get(
5753
@NotNull @ApiParam(value = "A date path parameter", required = true, defaultValue = "1972-01-01") @PathVariable("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
5854
@NotNull @ApiParam(value = "A date-time query parameter", required = true, defaultValue = "1973-12-19T03:39:57-08:00") @Valid @RequestParam(value = "dateTime", required = true, defaultValue = "1973-12-19T03:39:57-08:00") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) OffsetDateTime dateTime,
@@ -83,7 +79,6 @@ ResponseEntity<Void> get(
8379
value = DefaultApi.PATH_UPDATE_PET_WITH_FORM,
8480
consumes = "application/x-www-form-urlencoded"
8581
)
86-
8782
ResponseEntity<Void> updatePetWithForm(
8883
@NotNull @ApiParam(value = "A date path parameter", required = true, defaultValue = "1970-01-01") @PathVariable("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
8984
@ApiParam(value = "Updated last visit timestamp", defaultValue = "1971-12-19T03:39:57-08:00") @Valid @RequestParam(value = "visitDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) OffsetDateTime visitDate

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
import io.swagger.v3.oas.annotations.tags.Tag;
2121
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2222
import io.swagger.v3.oas.annotations.media.ExampleObject;
23-
import org.springframework.http.HttpStatus;
24-
import org.springframework.http.MediaType;
2523
import org.springframework.http.ResponseEntity;
2624
import org.springframework.validation.annotation.Validated;
2725
import org.springframework.web.bind.annotation.*;
28-
import org.springframework.web.context.request.NativeWebRequest;
2926
import org.springframework.web.multipart.MultipartFile;
3027

3128
import javax.validation.Valid;
@@ -65,7 +62,6 @@ public interface PetApi {
6562
value = PetApi.PATH_ADD_PET,
6663
consumes = "application/json"
6764
)
68-
6965
ResponseEntity<Void> addPet(
7066
@Parameter(name = "Pet", description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody Pet pet
7167
);
@@ -96,7 +92,6 @@ ResponseEntity<Void> addPet(
9692
method = RequestMethod.DELETE,
9793
value = PetApi.PATH_DELETE_PET
9894
)
99-
10095
ResponseEntity<Void> deletePet(
10196
@NotNull @Parameter(name = "petId", description = "Pet id to delete", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
10297
@Parameter(name = "api_key", description = "", in = ParameterIn.HEADER) @RequestHeader(value = "api_key", required = false) @Nullable String apiKey
@@ -133,7 +128,6 @@ ResponseEntity<Void> deletePet(
133128
value = PetApi.PATH_FIND_PETS_BY_STATUS,
134129
produces = { "application/json", "application/xml" }
135130
)
136-
137131
ResponseEntity<List<Pet>> findPetsByStatus(
138132
@NotNull @Parameter(name = "status", description = "Status values that need to be considered for filter", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "status", required = true) List<String> status
139133
);
@@ -172,7 +166,6 @@ ResponseEntity<List<Pet>> findPetsByStatus(
172166
value = PetApi.PATH_FIND_PETS_BY_TAGS,
173167
produces = { "application/json", "application/xml" }
174168
)
175-
176169
ResponseEntity<List<Pet>> findPetsByTags(
177170
@NotNull @Parameter(name = "tags", description = "Tags to filter by", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "tags", required = true) List<String> tags
178171
);
@@ -210,7 +203,6 @@ ResponseEntity<List<Pet>> findPetsByTags(
210203
value = PetApi.PATH_GET_PET_BY_ID,
211204
produces = { "application/json", "application/xml" }
212205
)
213-
214206
ResponseEntity<Pet> getPetById(
215207
@NotNull @Parameter(name = "petId", deprecated = true, description = "ID of pet to return", required = true, in = ParameterIn.PATH) @PathVariable("petId") @Deprecated Long petId
216208
);
@@ -245,7 +237,6 @@ ResponseEntity<Pet> getPetById(
245237
value = PetApi.PATH_UPDATE_PET,
246238
consumes = "application/json"
247239
)
248-
249240
ResponseEntity<Void> updatePet(
250241
@Parameter(name = "Pet", description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody Pet pet
251242
);
@@ -278,7 +269,6 @@ ResponseEntity<Void> updatePet(
278269
value = PetApi.PATH_UPDATE_PET_WITH_FORM,
279270
consumes = "application/x-www-form-urlencoded"
280271
)
281-
282272
ResponseEntity<Void> updatePetWithForm(
283273
@NotNull @Parameter(name = "petId", description = "ID of pet that needs to be updated", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
284274
@Parameter(name = "name", description = "Updated name of the pet") @Valid @RequestParam(value = "name", required = false) String name,
@@ -316,7 +306,6 @@ ResponseEntity<Void> updatePetWithForm(
316306
produces = { "application/json" },
317307
consumes = "multipart/form-data"
318308
)
319-
320309
ResponseEntity<ModelApiResponse> uploadFile(
321310
@NotNull @Parameter(name = "petId", description = "ID of pet to update", required = true, in = ParameterIn.PATH) @PathVariable("petId") Long petId,
322311
@Parameter(name = "additionalMetadata", description = "Additional data to pass to server") @Valid @RequestParam(value = "additionalMetadata", required = false) String additionalMetadata,

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/StoreApi.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
import io.swagger.v3.oas.annotations.tags.Tag;
2020
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2121
import io.swagger.v3.oas.annotations.media.ExampleObject;
22-
import org.springframework.http.HttpStatus;
23-
import org.springframework.http.MediaType;
2422
import org.springframework.http.ResponseEntity;
2523
import org.springframework.validation.annotation.Validated;
2624
import org.springframework.web.bind.annotation.*;
27-
import org.springframework.web.context.request.NativeWebRequest;
2825
import org.springframework.web.multipart.MultipartFile;
2926

3027
import javax.validation.Valid;
@@ -62,7 +59,6 @@ public interface StoreApi {
6259
method = RequestMethod.DELETE,
6360
value = StoreApi.PATH_DELETE_ORDER
6461
)
65-
6662
ResponseEntity<Void> deleteOrder(
6763
@NotNull @Parameter(name = "orderId", description = "ID of the order that needs to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("orderId") String orderId
6864
);
@@ -94,7 +90,6 @@ ResponseEntity<Void> deleteOrder(
9490
value = StoreApi.PATH_GET_INVENTORY,
9591
produces = { "application/json" }
9692
)
97-
9893
ResponseEntity<Map<String, Integer>> getInventory(
9994

10095
);
@@ -129,7 +124,6 @@ ResponseEntity<Map<String, Integer>> getInventory(
129124
value = StoreApi.PATH_GET_ORDER_BY_ID,
130125
produces = { "application/json", "application/xml" }
131126
)
132-
133127
ResponseEntity<Order> getOrderById(
134128
@NotNull @Min(1L) @Max(5L) @Parameter(name = "orderId", description = "ID of pet that needs to be fetched", required = true, in = ParameterIn.PATH) @PathVariable("orderId") Long orderId
135129
);
@@ -163,7 +157,6 @@ ResponseEntity<Order> getOrderById(
163157
produces = { "application/json", "application/xml" },
164158
consumes = "application/json"
165159
)
166-
167160
ResponseEntity<Order> placeOrder(
168161
@Parameter(name = "Order", description = "order placed for purchasing the pet", required = true) @Valid @RequestBody Order order
169162
);

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/UserApi.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
import io.swagger.v3.oas.annotations.tags.Tag;
2020
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2121
import io.swagger.v3.oas.annotations.media.ExampleObject;
22-
import org.springframework.http.HttpStatus;
23-
import org.springframework.http.MediaType;
2422
import org.springframework.http.ResponseEntity;
2523
import org.springframework.validation.annotation.Validated;
2624
import org.springframework.web.bind.annotation.*;
27-
import org.springframework.web.context.request.NativeWebRequest;
2825
import org.springframework.web.multipart.MultipartFile;
2926

3027
import javax.validation.Valid;
@@ -64,7 +61,6 @@ public interface UserApi {
6461
value = UserApi.PATH_CREATE_USER,
6562
consumes = "application/json"
6663
)
67-
6864
ResponseEntity<Void> createUser(
6965
@Parameter(name = "User", description = "Created user object", required = true) @Valid @RequestBody User user
7066
);
@@ -95,7 +91,6 @@ ResponseEntity<Void> createUser(
9591
value = UserApi.PATH_CREATE_USERS_WITH_ARRAY_INPUT,
9692
consumes = "application/json"
9793
)
98-
9994
ResponseEntity<Void> createUsersWithArrayInput(
10095
@Parameter(name = "User", description = "List of user object", required = true) @Valid @RequestBody List<@Valid User> user
10196
);
@@ -126,7 +121,6 @@ ResponseEntity<Void> createUsersWithArrayInput(
126121
value = UserApi.PATH_CREATE_USERS_WITH_LIST_INPUT,
127122
consumes = "application/json"
128123
)
129-
130124
ResponseEntity<Void> createUsersWithListInput(
131125
@Parameter(name = "User", description = "List of user object", required = true) @Valid @RequestBody List<@Valid User> user
132126
);
@@ -158,7 +152,6 @@ ResponseEntity<Void> createUsersWithListInput(
158152
method = RequestMethod.DELETE,
159153
value = UserApi.PATH_DELETE_USER
160154
)
161-
162155
ResponseEntity<Void> deleteUser(
163156
@NotNull @Parameter(name = "username", description = "The name that needs to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("username") String username
164157
);
@@ -193,7 +186,6 @@ ResponseEntity<Void> deleteUser(
193186
value = UserApi.PATH_GET_USER_BY_NAME,
194187
produces = { "application/json", "application/xml" }
195188
)
196-
197189
ResponseEntity<User> getUserByName(
198190
@NotNull @Parameter(name = "username", description = "The name that needs to be fetched. Use user1 for testing.", required = true, in = ParameterIn.PATH) @PathVariable("username") String username
199191
);
@@ -227,7 +219,6 @@ ResponseEntity<User> getUserByName(
227219
value = UserApi.PATH_LOGIN_USER,
228220
produces = { "application/json", "application/xml" }
229221
)
230-
231222
ResponseEntity<String> loginUser(
232223
@NotNull @Pattern(regexp = "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$") @Parameter(name = "username", description = "The user name for login", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "username", required = true) String username,
233224
@NotNull @Parameter(name = "password", deprecated = true, description = "The password for login in clear text", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "password", required = true) @Deprecated String password
@@ -257,7 +248,6 @@ ResponseEntity<String> loginUser(
257248
method = RequestMethod.GET,
258249
value = UserApi.PATH_LOGOUT_USER
259250
)
260-
261251
ResponseEntity<Void> logoutUser(
262252

263253
);
@@ -291,7 +281,6 @@ ResponseEntity<Void> logoutUser(
291281
value = UserApi.PATH_UPDATE_USER,
292282
consumes = "application/json"
293283
)
294-
295284
ResponseEntity<Void> updateUser(
296285
@NotNull @Parameter(name = "username", description = "name that need to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("username") String username,
297286
@Parameter(name = "User", description = "Updated user object", required = true) @Valid @RequestBody User user

0 commit comments

Comments
 (0)