Skip to content

Commit 354265e

Browse files
committed
code refactoring
1 parent 08b428f commit 354265e

File tree

24 files changed

+247
-338
lines changed

24 files changed

+247
-338
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * *
7+
* * * * * * Copyright 2019-2025 the original author or authors.
8+
* * * * * *
9+
* * * * * * Licensed under the Apache License, Version 2.0 (the "License");
10+
* * * * * * you may not use this file except in compliance with the License.
11+
* * * * * * You may obtain a copy of the License at
12+
* * * * * *
13+
* * * * * * https://www.apache.org/licenses/LICENSE-2.0
14+
* * * * * *
15+
* * * * * * Unless required by applicable law or agreed to in writing, software
16+
* * * * * * distributed under the License is distributed on an "AS IS" BASIS,
17+
* * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* * * * * * See the License for the specific language governing permissions and
19+
* * * * * * limitations under the License.
20+
* * * * *
21+
* * * *
22+
* * *
23+
* *
24+
*
25+
*/
26+
27+
package org.springdoc.api;
28+
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Optional;
32+
import java.util.stream.Collectors;
33+
34+
import org.springdoc.core.customizers.SpringDocCustomizers;
35+
import org.springdoc.core.models.GroupedOpenApi;
36+
import org.springdoc.core.properties.SpringDocConfigProperties;
37+
import org.springdoc.core.properties.SpringDocConfigProperties.GroupConfig;
38+
import org.springdoc.core.providers.SpringDocProviders;
39+
import org.springdoc.core.service.AbstractRequestService;
40+
import org.springdoc.core.service.GenericResponseService;
41+
import org.springdoc.core.service.OpenAPIService;
42+
import org.springdoc.core.service.OperationService;
43+
44+
import org.springframework.beans.factory.InitializingBean;
45+
import org.springframework.beans.factory.ObjectFactory;
46+
47+
/**
48+
* The type Abstract multiple open api resource.
49+
*
50+
* @param <R> the type of OpenAPI resource
51+
* @author bnasslahsen
52+
*/
53+
public abstract class AbstractMultipleOpenApiResource<R extends AbstractOpenApiResource> implements InitializingBean {
54+
55+
/**
56+
* The Grouped open apis.
57+
*/
58+
private final List<GroupedOpenApi> groupedOpenApis;
59+
60+
/**
61+
* The Default open api builder.
62+
*/
63+
protected final ObjectFactory<OpenAPIService> defaultOpenAPIBuilder;
64+
65+
/**
66+
* The Request builder.
67+
*/
68+
protected final AbstractRequestService requestBuilder;
69+
70+
/**
71+
* The Response builder.
72+
*/
73+
protected final GenericResponseService responseBuilder;
74+
75+
/**
76+
* The Operation parser.
77+
*/
78+
protected final OperationService operationParser;
79+
80+
/**
81+
* The Spring doc config properties.
82+
*/
83+
protected final SpringDocConfigProperties springDocConfigProperties;
84+
85+
/**
86+
* The Spring doc providers.
87+
*/
88+
protected final SpringDocProviders springDocProviders;
89+
90+
/**
91+
* The Spring doc customizers.
92+
*/
93+
private final SpringDocCustomizers springDocCustomizers;
94+
95+
/**
96+
* The Grouped open api resources.
97+
*/
98+
private Map<String, R> groupedOpenApiResources;
99+
100+
/**
101+
* Instantiates a new Abstract multiple open api resource.
102+
*
103+
* @param groupedOpenApis the grouped open apis
104+
* @param defaultOpenAPIBuilder the default open api builder
105+
* @param requestBuilder the request builder
106+
* @param responseBuilder the response builder
107+
* @param operationParser the operation parser
108+
* @param springDocConfigProperties the spring doc config properties
109+
* @param springDocProviders the spring doc providers
110+
* @param springDocCustomizers the spring doc customizers
111+
*/
112+
protected AbstractMultipleOpenApiResource(List<GroupedOpenApi> groupedOpenApis,
113+
ObjectFactory<OpenAPIService> defaultOpenAPIBuilder, AbstractRequestService requestBuilder,
114+
GenericResponseService responseBuilder, OperationService operationParser,
115+
SpringDocConfigProperties springDocConfigProperties, SpringDocProviders springDocProviders, SpringDocCustomizers springDocCustomizers) {
116+
117+
this.groupedOpenApis = groupedOpenApis;
118+
this.defaultOpenAPIBuilder = defaultOpenAPIBuilder;
119+
this.requestBuilder = requestBuilder;
120+
this.responseBuilder = responseBuilder;
121+
this.operationParser = operationParser;
122+
this.springDocConfigProperties = springDocConfigProperties;
123+
this.springDocProviders = springDocProviders;
124+
this.springDocCustomizers = springDocCustomizers;
125+
}
126+
127+
@Override
128+
public void afterPropertiesSet() {
129+
this.groupedOpenApis.forEach(groupedOpenApi -> {
130+
springDocCustomizers.getGlobalOpenApiCustomizers().ifPresent(groupedOpenApi::addAllOpenApiCustomizer);
131+
springDocCustomizers.getGlobalOperationCustomizers().ifPresent(groupedOpenApi::addAllOperationCustomizer);
132+
springDocCustomizers.getGlobalOpenApiMethodFilters().ifPresent(groupedOpenApi::addAllOpenApiMethodFilter);
133+
}
134+
);
135+
136+
this.groupedOpenApiResources = groupedOpenApis.stream()
137+
.collect(Collectors.toMap(GroupedOpenApi::getGroup, item ->
138+
{
139+
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(), item.getHeadersToMatch(), item.getDisplayName());
140+
springDocConfigProperties.addGroupConfig(groupConfig);
141+
return buildOpenApiResource(item);
142+
},
143+
(existingValue, newValue) -> existingValue // choice to keep the existing value
144+
));
145+
}
146+
147+
/**
148+
* Build open api resource.
149+
*
150+
* @param item the grouped open api
151+
* @return the open api resource
152+
*/
153+
protected abstract R buildOpenApiResource(GroupedOpenApi item);
154+
155+
/**
156+
* Gets open api resource or throw.
157+
*
158+
* @param group the group
159+
* @return the open api resource or throw
160+
*/
161+
protected R getOpenApiResourceOrThrow(String group) {
162+
R openApiResource = groupedOpenApiResources.get(group);
163+
if (openApiResource == null) {
164+
throw new OpenApiResourceNotFoundException("No OpenAPI resource found for group: " + group);
165+
}
166+
return openApiResource;
167+
}
168+
169+
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/converters/ConverterUtils.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
package org.springdoc.core.converters;
2828

29-
import java.util.Arrays;
3029
import java.util.List;
3130
import java.util.concurrent.Callable;
3231
import java.util.concurrent.CompletionStage;
@@ -127,9 +126,7 @@ public static boolean isResponseTypeToIgnore(Class<?> rawClass) {
127126
* @param classes the classes
128127
*/
129128
public static void removeResponseWrapperToIgnore(Class<?> classes) {
130-
List classesToIgnore = Arrays.asList(classes);
131-
if (RESULT_WRAPPERS_TO_IGNORE.containsAll(classesToIgnore))
132-
RESULT_WRAPPERS_TO_IGNORE.removeAll(classesToIgnore);
129+
RESULT_WRAPPERS_TO_IGNORE.remove(classes);
133130
}
134131

135132
/**
@@ -138,9 +135,7 @@ public static void removeResponseWrapperToIgnore(Class<?> classes) {
138135
* @param classes the classes
139136
*/
140137
public static void removeResponseTypeToIgnore(Class<?> classes) {
141-
List classesToIgnore = Arrays.asList(classes);
142-
if (RESPONSE_TYPES_TO_IGNORE.containsAll(classesToIgnore))
143-
RESPONSE_TYPES_TO_IGNORE.removeAll(classesToIgnore);
138+
RESPONSE_TYPES_TO_IGNORE.remove(classes);
144139
}
145140

146141
/**
@@ -163,9 +158,7 @@ public static boolean isFluxTypeWrapper(Class<?> rawClass) {
163158
* @param classes the classes
164159
*/
165160
public static void removeFluxWrapperToIgnore(Class<?> classes) {
166-
List classesToIgnore = Arrays.asList(classes);
167-
if (FLUX_WRAPPERS_TO_IGNORE.containsAll(classesToIgnore))
168-
FLUX_WRAPPERS_TO_IGNORE.removeAll(classesToIgnore);
161+
FLUX_WRAPPERS_TO_IGNORE.remove(classes);
169162
}
170163

171164
/**
@@ -192,9 +185,7 @@ public static void addJavaTypeToIgnore(Class<?> cls) {
192185
* @param classes the classes
193186
*/
194187
public static void removeJavaTypeToIgnore(Class<?> classes) {
195-
List classesToIgnore = Arrays.asList(classes);
196-
if (JAVA_TYPE_TO_IGNORE.containsAll(classesToIgnore))
197-
JAVA_TYPE_TO_IGNORE.removeAll(classesToIgnore);
188+
JAVA_TYPE_TO_IGNORE.remove(classes);
198189
}
199190

200191
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/data/DataRestOperationService.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,23 +325,12 @@ private Operation initOperation(HandlerMethod handlerMethod, Class<?> domainType
325325
*/
326326
private void addOperationDescription(Operation operation, RequestMethod requestMethod, String entity, DataRestRepository dataRestRepository) {
327327
switch (requestMethod) {
328-
case GET:
329-
operation.setDescription(createDescription("get-", entity, dataRestRepository));
330-
break;
331-
case POST:
332-
operation.setDescription(createDescription("create-", entity, dataRestRepository));
333-
break;
334-
case DELETE:
335-
operation.setDescription(createDescription("delete-", entity, dataRestRepository));
336-
break;
337-
case PUT:
338-
operation.setDescription(createDescription("update-", entity, dataRestRepository));
339-
break;
340-
case PATCH:
341-
operation.setDescription(createDescription("patch-", entity, dataRestRepository));
342-
break;
343-
default:
344-
throw new IllegalArgumentException(requestMethod.name());
328+
case GET -> operation.setDescription(createDescription("get-", entity, dataRestRepository));
329+
case POST -> operation.setDescription(createDescription("create-", entity, dataRestRepository));
330+
case DELETE -> operation.setDescription(createDescription("delete-", entity, dataRestRepository));
331+
case PUT -> operation.setDescription(createDescription("update-", entity, dataRestRepository));
332+
case PATCH -> operation.setDescription(createDescription("patch-", entity, dataRestRepository));
333+
default -> throw new IllegalArgumentException(requestMethod.name());
345334
}
346335
}
347336

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/data/DataRestResponseService.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,26 @@ public void buildEntityResponse(Operation operation, HandlerMethod handlerMethod
167167
*/
168168
private void addResponse(RequestMethod requestMethod, String operationPath, ApiResponses apiResponses, ApiResponse apiResponse) {
169169
switch (requestMethod) {
170-
case GET:
170+
case GET -> {
171171
addResponse200(apiResponses, apiResponse);
172172
if (operationPath.contains("/{id}"))
173173
addResponse404(apiResponses);
174-
break;
175-
case POST:
176-
apiResponses.put(String.valueOf(HttpStatus.CREATED.value()), apiResponse.description(HttpStatus.CREATED.getReasonPhrase()));
177-
break;
178-
case DELETE:
174+
}
175+
case POST -> apiResponses.put(String.valueOf(HttpStatus.CREATED.value()), apiResponse.description(HttpStatus.CREATED.getReasonPhrase()));
176+
case DELETE -> {
179177
addResponse204(apiResponses);
180178
addResponse404(apiResponses);
181-
break;
182-
case PUT:
179+
}
180+
case PUT -> {
183181
addResponse200(apiResponses, apiResponse);
184182
apiResponses.put(String.valueOf(HttpStatus.CREATED.value()), new ApiResponse().content(apiResponse.getContent()).description(HttpStatus.CREATED.getReasonPhrase()));
185183
addResponse204(apiResponses);
186-
break;
187-
case PATCH:
184+
}
185+
case PATCH -> {
188186
addResponse200(apiResponses, apiResponse);
189187
addResponse204(apiResponses);
190-
break;
191-
default:
192-
throw new IllegalArgumentException(requestMethod.name());
188+
}
189+
default -> throw new IllegalArgumentException(requestMethod.name());
193190
}
194191
}
195192

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/data/DataRestRouterOperationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
package org.springdoc.core.data;
2828

29-
import java.util.Arrays;
29+
3030
import java.util.LinkedHashMap;
3131
import java.util.LinkedHashSet;
3232
import java.util.List;
@@ -70,7 +70,7 @@ public class DataRestRouterOperationService {
7070
/**
7171
* The constant UNDOCUMENTED_REQUEST_METHODS.
7272
*/
73-
private static final List<RequestMethod> UNDOCUMENTED_REQUEST_METHODS = Arrays.asList(RequestMethod.OPTIONS, RequestMethod.HEAD);
73+
private static final List<RequestMethod> UNDOCUMENTED_REQUEST_METHODS = List.of(RequestMethod.OPTIONS, RequestMethod.HEAD);
7474

7575
/**
7676
* The constant REPOSITORY_PATH.

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/extractor/MethodParameterPojoExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public static void addSimpleTypePredicate(Predicate<Class<?>> predicate) {
330330
* @param classes the classes
331331
*/
332332
public static void addSimpleTypes(Class<?>... classes) {
333-
SIMPLE_TYPES.addAll(Arrays.asList(classes));
333+
SIMPLE_TYPES.addAll(List.of(classes));
334334
}
335335

336336
/**
@@ -339,7 +339,7 @@ public static void addSimpleTypes(Class<?>... classes) {
339339
* @param classes the classes
340340
*/
341341
public static void removeSimpleTypes(Class<?>... classes) {
342-
SIMPLE_TYPES.removeAll(Arrays.asList(classes));
342+
SIMPLE_TYPES.removeAll(List.of(classes));
343343
}
344344

345345
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/fn/RouterFunctionData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private RequestMethod[] getMethod(Set<HttpMethod> methods) {
275275
* @return the string [ ]
276276
*/
277277
public String[] getProduces() {
278-
return produces.toArray(new String[produces.size()]);
278+
return produces.toArray(new String[0]);
279279
}
280280

281281
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ protected AbstractRequestService(GenericParameterService parameterBuilder, Reque
232232
* @param classes the classes
233233
*/
234234
public static void addRequestWrapperToIgnore(Class<?>... classes) {
235-
PARAM_TYPES_TO_IGNORE.addAll(Arrays.asList(classes));
235+
PARAM_TYPES_TO_IGNORE.addAll(List.of(classes));
236236
}
237237

238238
/**
@@ -241,9 +241,7 @@ public static void addRequestWrapperToIgnore(Class<?>... classes) {
241241
* @param classes the classes
242242
*/
243243
public static void removeRequestWrapperToIgnore(Class<?>... classes) {
244-
List<Class<?>> classesToIgnore = Arrays.asList(classes);
245-
if (PARAM_TYPES_TO_IGNORE.containsAll(classesToIgnore))
246-
PARAM_TYPES_TO_IGNORE.removeAll(Arrays.asList(classes));
244+
PARAM_TYPES_TO_IGNORE.removeAll(List.of(classes));
247245
}
248246

249247
/**
@@ -597,7 +595,7 @@ private void setParams(Operation operation, List<Parameter> operationParameters,
597595
* @return the boolean
598596
*/
599597
public boolean isValidParameter(Parameter parameter, MethodAttributes methodAttributes) {
600-
return parameter != null && (parameter.getName() != null || parameter.get$ref() != null) && !(Arrays.asList(methodAttributes.getMethodConsumes()).contains(APPLICATION_FORM_URLENCODED_VALUE) && ParameterIn.QUERY.toString().equals(parameter.getIn()));
598+
return parameter != null && (parameter.getName() != null || parameter.get$ref() != null) && !(List.of(methodAttributes.getMethodConsumes()).contains(APPLICATION_FORM_URLENCODED_VALUE) && ParameterIn.QUERY.toString().equals(parameter.getIn()));
601599
}
602600

603601
/**
@@ -818,7 +816,7 @@ private boolean isRequestBodyParam(RequestMethod requestMethod, ParameterInfo pa
818816
(checkRequestBodyAnnotation(methodParameter)
819817
|| checkOperationRequestBody(methodParameter)
820818
|| checkFile(methodParameter)
821-
|| Arrays.asList(methodAttributes.getMethodConsumes()).contains(MULTIPART_FORM_DATA_VALUE));
819+
|| List.of(methodAttributes.getMethodConsumes()).contains(MULTIPART_FORM_DATA_VALUE));
822820
}
823821

824822
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public GenericParameterService(PropertyResolverUtils propertyResolverUtils,
173173
* @param classes the classes
174174
*/
175175
public static void addFileType(Class<?>... classes) {
176-
FILE_TYPES.addAll(Arrays.asList(classes));
176+
FILE_TYPES.addAll(List.of(classes));
177177
}
178178

179179
/**
@@ -566,8 +566,7 @@ private boolean isExplodable(io.swagger.v3.oas.annotations.Parameter p) {
566566
* @return the boolean
567567
*/
568568
public boolean isFile(MethodParameter methodParameter) {
569-
if (methodParameter.getGenericParameterType() instanceof ParameterizedType) {
570-
ParameterizedType parameterizedType = (ParameterizedType) methodParameter.getGenericParameterType();
569+
if (methodParameter.getGenericParameterType() instanceof ParameterizedType parameterizedType) {
571570
return isFile(parameterizedType);
572571
}
573572
else {

0 commit comments

Comments
 (0)