Skip to content

Commit 6e14cb4

Browse files
committed
Fix sonarqube errors
1 parent eae2056 commit 6e14cb4

10 files changed

Lines changed: 51 additions & 33 deletions

File tree

‎springdoc-openapi-starter-common-mcp/src/main/java/org/springdoc/ai/dashboard/McpSyncServerDashboardToolSource.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,8 @@ private Map<String, Type> scanToolReturnTypes(ApplicationContext applicationCont
210210
}
211211
}
212212
}
213-
catch (ClassNotFoundException ex) {
214-
// @McpTool annotation not on classpath — no return types available
215-
}
216213
catch (Exception ex) {
217-
// Scanning failed — ignore
214+
// @McpTool annotation not on classpath, or scanning failed — no return types available
218215
}
219216
return returnTypes;
220217
}

‎springdoc-openapi-starter-common-mcp/src/main/java/org/springdoc/ai/mcp/McpAuditLogger.java‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.time.format.DateTimeFormatter;
3232
import java.util.ArrayList;
3333
import java.util.List;
34+
import java.util.concurrent.atomic.AtomicReference;
3435
import java.util.function.Consumer;
3536

3637
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -103,7 +104,7 @@ public class McpAuditLogger {
103104
* Optional in-memory sink set by {@link org.springdoc.ai.dashboard.McpAuditEventStore}
104105
* when the dashboard is active. {@code null} when the dashboard is not in use.
105106
*/
106-
private static volatile Consumer<String> eventSink;
107+
private static final AtomicReference<Consumer<String>> EVENT_SINK = new AtomicReference<>();
107108

108109
private McpAuditLogger() {
109110
}
@@ -115,7 +116,7 @@ private McpAuditLogger() {
115116
* @param sink the consumer to register, or {@code null} to unregister
116117
*/
117118
public static void setEventSink(Consumer<String> sink) {
118-
eventSink = sink;
119+
EVENT_SINK.set(sink);
119120
}
120121

121122
/**
@@ -218,7 +219,7 @@ static void log(AuditRecord record) {
218219

219220
String json = MAPPER.writeValueAsString(root);
220221
AUDIT_LOGGER.info(json);
221-
Consumer<String> sink = eventSink;
222+
Consumer<String> sink = EVENT_SINK.get();
222223
if (sink != null) {
223224
sink.accept(json);
224225
}
@@ -275,11 +276,8 @@ private static SecurityInfo extractSecurityInfo() {
275276
}
276277
}
277278
}
278-
catch (ClassNotFoundException ignored) {
279-
// Spring Security not on classpath
280-
}
281279
catch (Exception ignored) {
282-
// No authentication context or reflection failure
280+
// Spring Security not on classpath, or no authentication context / reflection failure
283281
}
284282
return info;
285283
}

‎springdoc-openapi-starter-common-mcp/src/main/java/org/springdoc/ai/mcp/McpCommunityToolAuditAspect.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private String extractToolName(ProceedingJoinPoint pjp) {
111111
return (name != null && !name.isBlank()) ? name : sig.getMethod().getName();
112112
}
113113
catch (Exception ignored) {
114+
// Fall back to the method name if the annotation's name() cannot be read
114115
}
115116
}
116117
}

‎springdoc-openapi-starter-common-mcp/src/main/java/org/springdoc/ai/mcp/OpenApiMcpToolCallbackProvider.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Locale;
3232
import java.util.Map;
3333
import java.util.Optional;
34+
import java.util.concurrent.atomic.AtomicReference;
3435

3536
import io.swagger.v3.oas.models.OpenAPI;
3637
import io.swagger.v3.oas.models.PathItem;
@@ -95,7 +96,7 @@ public class OpenApiMcpToolCallbackProvider implements ToolCallbackProvider {
9596
/**
9697
* Cached tool callbacks, built lazily from the OpenAPI specification.
9798
*/
98-
private volatile ToolCallback[] cachedToolCallbacks;
99+
private final AtomicReference<ToolCallback[]> cachedToolCallbacks = new AtomicReference<>();
99100

100101
/**
101102
* Constructs a new OpenApiMcpToolCallbackProvider.
@@ -117,14 +118,14 @@ public OpenApiMcpToolCallbackProvider(OpenAPIService openAPIService,
117118

118119
@Override
119120
public ToolCallback[] getToolCallbacks() {
120-
ToolCallback[] cached = this.cachedToolCallbacks;
121+
ToolCallback[] cached = this.cachedToolCallbacks.get();
121122
if (cached != null) {
122123
return cached;
123124
}
124125
OpenAPI openAPI = openAPIService.getCachedOpenAPI(Locale.getDefault());
125126
if (openAPI != null) {
126127
cached = buildToolCallbacks(openAPI);
127-
this.cachedToolCallbacks = cached;
128+
this.cachedToolCallbacks.set(cached);
128129
return cached;
129130
}
130131
return new ToolCallback[0];
@@ -184,7 +185,7 @@ ToolCallback[] buildToolCallbacks(OpenAPI openAPI) {
184185
* {@link #getToolCallbacks()} rebuilds them.
185186
*/
186187
void resetCache() {
187-
this.cachedToolCallbacks = null;
188+
this.cachedToolCallbacks.set(null);
188189
}
189190

190191
/**

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

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

2929
import java.util.List;
3030
import java.util.Map;
31-
import java.util.Optional;
3231
import java.util.stream.Collectors;
3332

3433
import org.springdoc.core.customizers.SpringDocCustomizers;

‎springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocWebServerConfiguration.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.springdoc.core.configuration;
22

3+
import java.util.concurrent.atomic.AtomicReference;
34
import java.util.function.Supplier;
45

56
import org.apache.commons.lang3.StringUtils;
@@ -48,9 +49,9 @@ static final class SpringDocWebServerPortListener
4849

4950
private final String contextPath;
5051

51-
private volatile Supplier<Integer> applicationPortSupplier;
52-
private volatile Supplier<Integer> actuatorPortSupplier;
53-
private volatile Supplier<ApplicationContext> managementContextSupplier;
52+
private final AtomicReference<Supplier<Integer>> applicationPortSupplier = new AtomicReference<>();
53+
private final AtomicReference<Supplier<Integer>> actuatorPortSupplier = new AtomicReference<>();
54+
private final AtomicReference<Supplier<ApplicationContext>> managementContextSupplier = new AtomicReference<>();
5455

5556
public SpringDocWebServerPortListener(ObjectProvider<ServerProperties> serverPropertiesProvider) {
5657
ServerProperties serverProperties = serverPropertiesProvider.getIfAvailable();
@@ -63,27 +64,27 @@ public SpringDocWebServerPortListener(ObjectProvider<ServerProperties> serverPro
6364
public void onApplicationEvent(WebServerInitializedEvent event) {
6465
final WebServer webServer = event.getWebServer();
6566
if (WebServerApplicationContext.hasServerNamespace(event.getApplicationContext(), "management")) {
66-
this.actuatorPortSupplier = webServer::getPort;
67-
this.managementContextSupplier = event::getApplicationContext;
67+
this.actuatorPortSupplier.set(webServer::getPort);
68+
this.managementContextSupplier.set(event::getApplicationContext);
6869
}
6970
else {
70-
this.applicationPortSupplier = webServer::getPort;
71+
this.applicationPortSupplier.set(webServer::getPort);
7172
}
7273
}
7374

7475
@Override
7576
public Supplier<Integer> getApplicationPort() {
76-
return applicationPortSupplier;
77+
return applicationPortSupplier.get();
7778
}
7879

7980
@Override
8081
public Supplier<Integer> getActuatorPort() {
81-
return actuatorPortSupplier;
82+
return actuatorPortSupplier.get();
8283
}
8384

8485
@Override
8586
public Supplier<ApplicationContext> getManagementApplicationContext() {
86-
return this.managementContextSupplier;
87+
return this.managementContextSupplier.get();
8788
}
8889

8990
@Override

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,10 @@ private Type getTypeForCollectionModel(Class returnedEntityType, boolean pagingR
295295
private Type getTypeForWildcardType(RequestMethod requestMethod, DataRestRepository dataRestRepository, Class returnedEntityType, ParameterizedType parameterizedType) {
296296
WildcardType wildcardType = (WildcardType) parameterizedType.getActualTypeArguments()[0];
297297
Class<?> type = findType(requestMethod, dataRestRepository);
298-
if (wildcardType.getUpperBounds()[0] instanceof ParameterizedType wildcardTypeUpperBound) {
299-
if (RepresentationModel.class.equals(wildcardTypeUpperBound.getRawType())) {
300-
if (MapModel.class.equals(type))
301-
return ResolvableType.forClassWithGenerics(ResponseEntity.class, type).getType();
302-
}
298+
if (wildcardType.getUpperBounds()[0] instanceof ParameterizedType wildcardTypeUpperBound
299+
&& RepresentationModel.class.equals(wildcardTypeUpperBound.getRawType())
300+
&& MapModel.class.equals(type)) {
301+
return ResolvableType.forClassWithGenerics(ResponseEntity.class, type).getType();
303302
}
304303
return resolveGenericType(ResponseEntity.class, type, returnedEntityType);
305304
}

‎springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/SpringWebProvider.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
package org.springdoc.core.providers;
2727

28-
import java.util.HashMap;
28+
import java.util.EnumMap;
2929
import java.util.Map;
3030
import java.util.Set;
3131

@@ -66,7 +66,7 @@ public abstract class SpringWebProvider implements ApplicationContextAware {
6666
/**
6767
* The Spring doc version strategy map.
6868
*/
69-
protected final Map<SpringDocApiVersionType, SpringDocVersionStrategy> springDocVersionStrategyMap = new HashMap<>();
69+
protected final Map<SpringDocApiVersionType, SpringDocVersionStrategy> springDocVersionStrategyMap = new EnumMap<>(SpringDocApiVersionType.class);
7070

7171
/**
7272
* Gets handler methods.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
*/
3939
public final class SpringSecurityUtils {
4040

41+
private SpringSecurityUtils() {
42+
}
43+
4144
/**
4245
* Gets path.
4346
*

‎springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/core/visitor/RouterFunctionVisitor.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@
5252
*/
5353
public class RouterFunctionVisitor extends AbstractRouterFunctionVisitor implements RouterFunctions.Visitor, RequestPredicates.Visitor {
5454

55+
/**
56+
* The package-private {@code PathResourceLookupFunction} type, resolved reflectively
57+
* since it is not accessible for a direct {@code instanceof} check.
58+
*/
59+
private static final Class<?> PATH_RESOURCE_LOOKUP_FUNCTION = resolvePathResourceLookupFunction();
60+
61+
/**
62+
* Resolves the package-private {@code PathResourceLookupFunction} class.
63+
* @return the class, or {@code null} if it cannot be found
64+
*/
65+
private static Class<?> resolvePathResourceLookupFunction() {
66+
try {
67+
return Class.forName("org.springframework.web.reactive.function.server.PathResourceLookupFunction");
68+
}
69+
catch (ClassNotFoundException ex) {
70+
return null;
71+
}
72+
}
73+
5574
@Override
5675
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
5776
this.currentRouterFunctionDatas = new ArrayList<>();
@@ -72,7 +91,7 @@ public void endNested(RequestPredicate predicate) {
7291

7392
@Override
7493
public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
75-
if ("PathResourceLookupFunction".equals(lookupFunction.getClass().getSimpleName())) {
94+
if (PATH_RESOURCE_LOOKUP_FUNCTION != null && PATH_RESOURCE_LOOKUP_FUNCTION.isInstance(lookupFunction)) {
7695
Field patternField = ReflectionUtils.findField(lookupFunction.getClass(), "pattern");
7796
if (patternField != null) {
7897
ReflectionUtils.makeAccessible(patternField);

0 commit comments

Comments
 (0)