Skip to content

Commit 0922635

Browse files
committed
code review
1 parent 55fa901 commit 0922635

File tree

10 files changed

+75
-38
lines changed

10 files changed

+75
-38
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public abstract class AbstractOpenApiResource extends SpecFilter {
223223
/**
224224
* The Path pattern.
225225
*/
226-
private final Pattern pathPattern = Pattern.compile("\\{(.*?)}");
226+
private static final Pattern PATH_PATTERN = Pattern.compile("\\{(.*?)}");
227227

228228
/**
229229
* The resolved group config.
@@ -690,7 +690,7 @@ protected void calculatePath(HandlerMethod handlerMethod, RouterOperation router
690690
operation = customizeOperation(operation, components, handlerMethod);
691691

692692
if (StringUtils.contains(operationPath, "*")) {
693-
Matcher matcher = pathPattern.matcher(operationPath);
693+
Matcher matcher = PATH_PATTERN.matcher(operationPath);
694694
while (matcher.find()) {
695695
String pathParam = matcher.group(1);
696696
String newPathParam = pathParam.replace("*", "");

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

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626

2727
package org.springdoc.core.converters;
2828

29-
import java.util.ArrayList;
3029
import java.util.Arrays;
31-
import java.util.Collections;
3230
import java.util.List;
3331
import java.util.concurrent.Callable;
3432
import java.util.concurrent.CompletionStage;
33+
import java.util.concurrent.CopyOnWriteArrayList;
3534

3635
import org.springframework.http.HttpEntity;
3736
import org.springframework.http.ResponseEntity;
@@ -46,22 +45,22 @@ public class ConverterUtils {
4645
/**
4746
* The constant RESULT_WRAPPERS_TO_IGNORE.
4847
*/
49-
private static final List<Class<?>> RESULT_WRAPPERS_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
48+
private static final List<Class<?>> RESULT_WRAPPERS_TO_IGNORE = new CopyOnWriteArrayList<>();
5049

5150
/**
5251
* The constant RESPONSE_TYPES_TO_IGNORE.
5352
*/
54-
private static final List<Class<?>> RESPONSE_TYPES_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
53+
private static final List<Class<?>> RESPONSE_TYPES_TO_IGNORE = new CopyOnWriteArrayList<>();
5554

5655
/**
5756
* The constant FLUX_WRAPPERS_TO_IGNORE.
5857
*/
59-
private static final List<Class<?>> FLUX_WRAPPERS_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
58+
private static final List<Class<?>> FLUX_WRAPPERS_TO_IGNORE = new CopyOnWriteArrayList<>();
6059

6160
/**
6261
* The constant JAVA_TYPE_TO_IGNORE.
6362
*/
64-
private static final List<Class<?>> JAVA_TYPE_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
63+
private static final List<Class<?>> JAVA_TYPE_TO_IGNORE = new CopyOnWriteArrayList<>();
6564

6665
static {
6766
RESULT_WRAPPERS_TO_IGNORE.add(Callable.class);
@@ -101,7 +100,11 @@ public static void addResponseTypeToIgnore(Class<?> cls) {
101100
* @return the boolean
102101
*/
103102
public static boolean isResponseTypeWrapper(Class<?> rawClass) {
104-
return RESULT_WRAPPERS_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
103+
for (Class<?> clazz : RESULT_WRAPPERS_TO_IGNORE) {
104+
if (clazz.isAssignableFrom(rawClass))
105+
return true;
106+
}
107+
return false;
105108
}
106109

107110
/**
@@ -111,7 +114,11 @@ public static boolean isResponseTypeWrapper(Class<?> rawClass) {
111114
* @return the boolean
112115
*/
113116
public static boolean isResponseTypeToIgnore(Class<?> rawClass) {
114-
return RESPONSE_TYPES_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
117+
for (Class<?> clazz : RESPONSE_TYPES_TO_IGNORE) {
118+
if (clazz.isAssignableFrom(rawClass))
119+
return true;
120+
}
121+
return false;
115122
}
116123

117124
/**
@@ -122,7 +129,7 @@ public static boolean isResponseTypeToIgnore(Class<?> rawClass) {
122129
public static void removeResponseWrapperToIgnore(Class<?> classes) {
123130
List classesToIgnore = Arrays.asList(classes);
124131
if (RESULT_WRAPPERS_TO_IGNORE.containsAll(classesToIgnore))
125-
RESULT_WRAPPERS_TO_IGNORE.removeAll(Arrays.asList(classes));
132+
RESULT_WRAPPERS_TO_IGNORE.removeAll(classesToIgnore);
126133
}
127134

128135
/**
@@ -133,7 +140,7 @@ public static void removeResponseWrapperToIgnore(Class<?> classes) {
133140
public static void removeResponseTypeToIgnore(Class<?> classes) {
134141
List classesToIgnore = Arrays.asList(classes);
135142
if (RESPONSE_TYPES_TO_IGNORE.containsAll(classesToIgnore))
136-
RESPONSE_TYPES_TO_IGNORE.removeAll(Arrays.asList(classes));
143+
RESPONSE_TYPES_TO_IGNORE.removeAll(classesToIgnore);
137144
}
138145

139146
/**
@@ -143,7 +150,11 @@ public static void removeResponseTypeToIgnore(Class<?> classes) {
143150
* @return the boolean
144151
*/
145152
public static boolean isFluxTypeWrapper(Class<?> rawClass) {
146-
return FLUX_WRAPPERS_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
153+
for (Class<?> clazz : FLUX_WRAPPERS_TO_IGNORE) {
154+
if (clazz.isAssignableFrom(rawClass))
155+
return true;
156+
}
157+
return false;
147158
}
148159

149160
/**
@@ -154,7 +165,7 @@ public static boolean isFluxTypeWrapper(Class<?> rawClass) {
154165
public static void removeFluxWrapperToIgnore(Class<?> classes) {
155166
List classesToIgnore = Arrays.asList(classes);
156167
if (FLUX_WRAPPERS_TO_IGNORE.containsAll(classesToIgnore))
157-
FLUX_WRAPPERS_TO_IGNORE.removeAll(Arrays.asList(classes));
168+
FLUX_WRAPPERS_TO_IGNORE.removeAll(classesToIgnore);
158169
}
159170

160171
/**
@@ -183,7 +194,7 @@ public static void addJavaTypeToIgnore(Class<?> cls) {
183194
public static void removeJavaTypeToIgnore(Class<?> classes) {
184195
List classesToIgnore = Arrays.asList(classes);
185196
if (JAVA_TYPE_TO_IGNORE.containsAll(classesToIgnore))
186-
JAVA_TYPE_TO_IGNORE.removeAll(Arrays.asList(classes));
197+
JAVA_TYPE_TO_IGNORE.removeAll(classesToIgnore);
187198
}
188199

189200
/**
@@ -193,7 +204,11 @@ public static void removeJavaTypeToIgnore(Class<?> classes) {
193204
* @return the boolean
194205
*/
195206
public static boolean isJavaTypeToIgnore(Class<?> rawClass) {
196-
return JAVA_TYPE_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
207+
for (Class<?> clazz : JAVA_TYPE_TO_IGNORE) {
208+
if (clazz.isAssignableFrom(rawClass))
209+
return true;
210+
}
211+
return false;
197212
}
198213

199214
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ public static void addParentType(String... parentTypes) {
109109
*/
110110
private Schema<?> getResolvedSchema(JavaType javaType, Schema<?> resolvedSchema) {
111111
if (resolvedSchema instanceof ObjectSchema && resolvedSchema.getProperties() != null) {
112-
if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getName())) {
113-
resolvedSchema = resolvedSchema.getProperties().get(javaType.getRawClass().getName());
112+
Schema<?> found = resolvedSchema.getProperties().get(javaType.getRawClass().getName());
113+
if (found != null) {
114+
resolvedSchema = found;
114115
}
115-
else if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSimpleName())) {
116-
resolvedSchema = resolvedSchema.getProperties().get(javaType.getRawClass().getSimpleName());
116+
else {
117+
found = resolvedSchema.getProperties().get(javaType.getRawClass().getSimpleName());
118+
if (found != null) {
119+
resolvedSchema = found;
120+
}
117121
}
118122
}
119123
return resolvedSchema;
@@ -206,7 +210,7 @@ private Schema composePolymorphicSchema(AnnotatedType type, Schema schema, Colle
206210
if (isConcreteClass(type)) result.addOneOfItem(schema);
207211
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
208212
Class<?> clazz = javaType.getRawClass();
209-
if (TYPES_TO_SKIP.stream().noneMatch(typeToSkip -> typeToSkip.equals(clazz.getSimpleName())))
213+
if (!TYPES_TO_SKIP.contains(clazz.getSimpleName()))
210214
composedSchemas.forEach(result::addOneOfItem);
211215

212216
// Remove _links from result (composed schema) to prevent duplication

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void addQueryParams(String name, String value) {
160160
* @return the string [ ]
161161
*/
162162
public String[] getHeaders() {
163-
return headers.toArray(new String[headers.size()]);
163+
return headers.toArray(new String[0]);
164164
}
165165

166166
/**
@@ -197,7 +197,7 @@ public void setMethods(Set<HttpMethod> methods) {
197197
* @return the string [ ]
198198
*/
199199
public String[] getConsumes() {
200-
return consumes.toArray(new String[consumes.size()]);
200+
return consumes.toArray(new String[0]);
201201
}
202202

203203
/**
@@ -206,7 +206,7 @@ public String[] getConsumes() {
206206
* @return the string [ ]
207207
*/
208208
public String[] getParams() {
209-
return params.toArray(new String[params.size()]);
209+
return params.toArray(new String[0]);
210210
}
211211

212212
/**

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.ArrayList;
4040
import java.util.Arrays;
4141
import java.util.Collection;
42-
import java.util.Collections;
4342
import java.util.HashMap;
4443
import java.util.Iterator;
4544
import java.util.LinkedHashMap;
@@ -51,6 +50,7 @@
5150
import java.util.Optional;
5251
import java.util.Set;
5352
import java.util.TimeZone;
53+
import java.util.concurrent.CopyOnWriteArrayList;
5454
import java.util.stream.Collectors;
5555
import java.util.stream.Stream;
5656

@@ -137,7 +137,7 @@ public abstract class AbstractRequestService {
137137
/**
138138
* The constant PARAM_TYPES_TO_IGNORE.
139139
*/
140-
private static final List<Class<?>> PARAM_TYPES_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
140+
private static final List<Class<?>> PARAM_TYPES_TO_IGNORE = new CopyOnWriteArrayList<>();
141141

142142
static {
143143
PARAM_TYPES_TO_IGNORE.add(WebRequest.class);
@@ -253,7 +253,11 @@ public static void removeRequestWrapperToIgnore(Class<?>... classes) {
253253
* @return the boolean
254254
*/
255255
public static boolean isRequestTypeToIgnore(Class<?> rawClass) {
256-
return PARAM_TYPES_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
256+
for (Class<?> clazz : PARAM_TYPES_TO_IGNORE) {
257+
if (clazz.isAssignableFrom(rawClass))
258+
return true;
259+
}
260+
return false;
257261
}
258262

259263
/**
@@ -735,7 +739,7 @@ public void applyBeanValidatorAnnotations(final RequestBody requestBody, final L
735739
.filter(annotation -> io.swagger.v3.oas.annotations.parameters.RequestBody.class.equals(annotation.annotationType()))
736740
.anyMatch(annotation -> ((io.swagger.v3.oas.annotations.parameters.RequestBody) annotation).required());
737741
}
738-
boolean validationExists = SchemaUtils.annotatedNotNull(annos.values().stream().toList());
742+
boolean validationExists = SchemaUtils.annotatedNotNull(new ArrayList<>(annos.values()));
739743

740744
if (validationExists || (!isOptional && (springRequestBodyRequired || swaggerRequestBodyRequired)))
741745
requestBody.setRequired(true);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232
import java.lang.reflect.ParameterizedType;
3333
import java.lang.reflect.Type;
3434
import java.lang.reflect.WildcardType;
35-
import java.util.ArrayList;
3635
import java.util.Arrays;
37-
import java.util.Collections;
3836
import java.util.LinkedHashMap;
3937
import java.util.List;
4038
import java.util.Locale;
4139
import java.util.Map;
4240
import java.util.Objects;
4341
import java.util.Optional;
42+
import java.util.concurrent.CopyOnWriteArrayList;
4443
import java.util.stream.Stream;
4544

4645
import com.fasterxml.jackson.annotation.JsonView;
@@ -107,7 +106,7 @@ public class GenericParameterService {
107106
/**
108107
* The constant FILE_TYPES.
109108
*/
110-
private static final List<Class<?>> FILE_TYPES = Collections.synchronizedList(new ArrayList<>());
109+
private static final List<Class<?>> FILE_TYPES = new CopyOnWriteArrayList<>();
111110

112111
/**
113112
* The constant LOGGER.
@@ -184,7 +183,11 @@ public static void addFileType(Class<?>... classes) {
184183
* @return the boolean
185184
*/
186185
public static boolean isFile(Class type) {
187-
return FILE_TYPES.stream().anyMatch(clazz -> clazz.isAssignableFrom(type));
186+
for (Class<?> clazz : FILE_TYPES) {
187+
if (clazz.isAssignableFrom(type))
188+
return true;
189+
}
190+
return false;
188191
}
189192

190193
/**
@@ -243,7 +246,7 @@ public static void mergeParameter(Parameter paramCalcul, Parameter paramDoc) {
243246
paramDoc.setAllowReserved(paramCalcul.getAllowReserved());
244247

245248
if (StringUtils.isBlank(paramDoc.get$ref()))
246-
paramDoc.set$ref(paramDoc.get$ref());
249+
paramDoc.set$ref(paramCalcul.get$ref());
247250

248251
if (paramDoc.getSchema() == null && paramDoc.getContent() == null)
249252
paramDoc.setSchema(paramCalcul.getSchema());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,12 @@ else if (calculatedOpenAPI != null && calculatedOpenAPI.getInfo() == null) {
278278
private void initializeHiddenRestController() {
279279
if (basicErrorController != null)
280280
getConfig().addHiddenRestControllers(basicErrorController);
281-
List<Class<?>> hiddenRestControllers = this.mappingsMap.entrySet().parallelStream()
281+
List<Class<?>> hiddenRestControllers = this.mappingsMap.entrySet().stream()
282282
.filter(controller -> (AnnotationUtils.findAnnotation(controller.getValue().getClass(),
283283
Hidden.class) != null)).map(controller -> controller.getValue().getClass())
284284
.collect(Collectors.toList());
285285
if (!CollectionUtils.isEmpty(hiddenRestControllers))
286-
getConfig().addHiddenRestControllers(hiddenRestControllers.toArray(new Class<?>[hiddenRestControllers.size()]));
286+
getConfig().addHiddenRestControllers(hiddenRestControllers.toArray(new Class<?>[0]));
287287
}
288288

289289
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.HashMap;
3131
import java.util.Locale;
3232
import java.util.Map;
33+
import java.util.regex.Pattern;
3334
import java.util.stream.Collectors;
3435

3536
import io.swagger.v3.oas.models.SpecVersion;
@@ -54,6 +55,11 @@ public class PropertyResolverUtils {
5455
*/
5556
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyResolverUtils.class);
5657

58+
/**
59+
* The constant NEWLINE_PATTERN.
60+
*/
61+
private static final Pattern NEWLINE_PATTERN = Pattern.compile("\\r?\\n");
62+
5763
/**
5864
* The Factory.
5965
*/
@@ -124,7 +130,7 @@ public String trimIndent(String text) {
124130
return null;
125131
}
126132
final String newLine = "\n";
127-
String[] lines = text.split("\\r?\\n");
133+
String[] lines = NEWLINE_PATTERN.split(text);
128134
int minIndent = resolveMinIndent(lines);
129135
try {
130136
return Arrays.stream(lines)

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/versions/MediaTypeVersionStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public String getParameterName() {
8484
public String[] buildProduces() {
8585
String type = mediaType.getType();
8686
String subtype = mediaType.getSubtype();
87-
String produces = String.format("%s/%s;%s=%s", type, subtype, parameterName, version);
87+
String produces = type + "/" + subtype + ";" + parameterName + "=" + version;
8888
return new String[] { produces };
8989
}
9090

springdoc-openapi-starter-common/src/main/java/org/springdoc/scalar/AbstractScalarController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.io.IOException;
3030
import java.util.List;
31+
import java.util.regex.Pattern;
3132

3233
import com.scalar.maven.core.ScalarHtmlRenderer;
3334
import com.scalar.maven.core.ScalarProperties;
@@ -51,6 +52,10 @@
5152
*/
5253
public abstract class AbstractScalarController {
5354

55+
/**
56+
* The constant SCRIPT_SRC_PATTERN.
57+
*/
58+
private static final Pattern SCRIPT_SRC_PATTERN = Pattern.compile("(<script[^>]*\\s+src\\s*=\\s*\")([^\"]*)(\")" );
5459

5560
/**
5661
* The Spring doc config properties.
@@ -116,7 +121,7 @@ protected ResponseEntity<String> getDocs(String requestUrl, String apiDocsPath,
116121

117122
String html = ScalarHtmlRenderer.render(configuredProperties);
118123
String bundleUrl = buildJsBundleUrl(requestUrl, scalarPath);
119-
html = html.replaceAll("(<script[^>]*\\s+src\\s*=\\s*\")([^\"]*)(\")", "$1"+bundleUrl+"$3");
124+
html = SCRIPT_SRC_PATTERN.matcher(html).replaceAll("$1" + bundleUrl + "$3");
120125
return ResponseEntity.ok()
121126
.contentType(MediaType.TEXT_HTML)
122127
.body(html);

0 commit comments

Comments
 (0)