Skip to content

Commit f85e00a

Browse files
chore: act upon SonarQube warnings
1 parent 06f8e63 commit f85e00a

11 files changed

Lines changed: 88 additions & 60 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import io.swagger.v3.oas.models.servers.Server;
8383
import org.apache.commons.lang3.ArrayUtils;
8484
import org.apache.commons.lang3.StringUtils;
85+
import org.apache.commons.lang3.Strings;
8586
import org.slf4j.Logger;
8687
import org.slf4j.LoggerFactory;
8788
import org.springdoc.core.annotations.RouterOperations;
@@ -684,7 +685,7 @@ protected void calculatePath(HandlerMethod handlerMethod, RouterOperation router
684685
// allow for customisation
685686
operation = customizeOperation(operation, components, handlerMethod);
686687

687-
if (StringUtils.contains(operationPath, "*")) {
688+
if (Strings.CS.contains(operationPath, "*")) {
688689
Matcher matcher = pathPattern.matcher(operationPath);
689690
while (matcher.find()) {
690691
String pathParam = matcher.group(1);
@@ -1298,7 +1299,7 @@ private PathItem buildPathItem(RequestMethod requestMethod, Operation operation,
12981299
if (ParameterIn.PATH.toString().equals(parameter.getIn())) {
12991300
// check it's present in the path
13001301
String name = parameter.getName();
1301-
if (!StringUtils.containsAny(operationPath, "{" + name + "}", "{*" + name + "}"))
1302+
if (!Strings.CS.containsAny(operationPath, "{" + name + "}", "{*" + name + "}"))
13021303
paramIt.remove();
13031304
}
13041305
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,13 @@ public String getParameterName() {
196196
}
197197

198198
@Override
199+
@Nullable
199200
public Method getMethod() {
200201
return delegate.getMethod();
201202
}
202203

203204
@Override
205+
@Nullable
204206
public Constructor<?> getConstructor() {
205207
return delegate.getConstructor();
206208
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
390390
Annotation[] paramAnnotations = getParameterAnnotations(methodParameter);
391391
TypeAndTypeAnnotations resolved = resolveTypeAndTypeAnnotationsForParameter(methodParameter);
392392
Type type = resolved.type();
393-
Annotation[] typeAnnotations = resolved.typeAnnotations();
393+
Annotation[] typeAnnotations = resolved.typeAnnotations().toArray(Annotation[]::new);
394394
Annotation[] mergedAnnotations =
395395
Stream.concat(
396396
Arrays.stream(paramAnnotations),
@@ -439,7 +439,7 @@ private TypeAndTypeAnnotations resolveTypeAndTypeAnnotationsForParameter(MethodP
439439
&& delegatingMethodParameter.getField() != null) {
440440
AnnotatedType annotated = delegatingMethodParameter.getField().getAnnotatedType();
441441
Type type = GenericTypeResolver.resolveType(annotated.getType(), methodParameter.getContainingClass());
442-
return new TypeAndTypeAnnotations(type, annotationsFromAnnotatedTypeArguments(annotated));
442+
return new TypeAndTypeAnnotations(type, Arrays.asList(annotationsFromAnnotatedTypeArguments(annotated)));
443443
}
444444

445445
Type type = GenericTypeResolver.resolveType(methodParameter.getGenericParameterType(), methodParameter.getContainingClass());
@@ -449,17 +449,17 @@ private TypeAndTypeAnnotations resolveTypeAndTypeAnnotationsForParameter(MethodP
449449
&& type == String.class) {
450450
Class<?> restored = KotlinInlineParameterResolver.resolveInlineType(methodParameter, type);
451451
return restored != null
452-
? new TypeAndTypeAnnotations(restored, restored.getAnnotations())
453-
: new TypeAndTypeAnnotations(type, new Annotation[0]);
452+
? new TypeAndTypeAnnotations(restored, Arrays.asList(restored.getAnnotations()))
453+
: new TypeAndTypeAnnotations(type, new ArrayList<>());
454454
}
455455

456-
return new TypeAndTypeAnnotations(type, methodParameter.getParameterType().getAnnotations());
456+
return new TypeAndTypeAnnotations(type, Arrays.asList(methodParameter.getParameterType().getAnnotations()));
457457
}
458458

459459
/**
460460
* Pair of resolved Java type and type annotations merged with parameter annotations for {@code extractSchema}.
461461
*/
462-
private record TypeAndTypeAnnotations(Type type, Annotation[] typeAnnotations) {
462+
private record TypeAndTypeAnnotations(Type type, List<Annotation> typeAnnotations) {
463463
}
464464

465465
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ private void setPathItemOperation(PathItem pathItemObject, String method, Operat
248248
case TRACE_METHOD -> pathItemObject.trace(operation);
249249
case HEAD_METHOD -> pathItemObject.head(operation);
250250
case OPTIONS_METHOD -> pathItemObject.options(operation);
251-
default -> { }
251+
default -> {
252+
// best effort with regard to supported operations
253+
}
252254
}
253255
}
254256

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,17 @@ public boolean fieldNullable(Field field) {
163163
}
164164

165165
// @Nullable/@NotNull
166-
Boolean ann = nullableFromAnnotations(field);
167-
if (ann != null) return ann;
166+
Optional<Boolean> ann = nullableFromAnnotations(field);
167+
if (ann.isPresent()) {
168+
return ann.get();
169+
}
168170

169171
// Kotlin nullability
170172
if (kotlinUtilsOptional.isPresent() && isKotlinDeclaringClass(field)) {
171-
Boolean kotlin = kotlinNullability(field);
172-
if (kotlin != null) return kotlin;
173+
Optional<Boolean> kotlin = kotlinNullability(field);
174+
if (kotlin.isPresent()) {
175+
return kotlin.get();
176+
}
173177
}
174178

175179
return JAVA_FIELD_NULLABLE_DEFAULT;
@@ -204,17 +208,15 @@ public boolean fieldRequired(Field field, io.swagger.v3.oas.annotations.media.Sc
204208
// Kotlin logic
205209
if (kotlinUtilsOptional.isPresent() && isKotlinDeclaringClass(field)) {
206210
if (fieldNullable(field)) return false;
207-
Boolean hasDefault = kotlinConstructorParamIsOptional(field);
208-
if (Boolean.TRUE.equals(hasDefault)) return false;
209-
return true;
210-
}
211+
return kotlinConstructorParamIsOptional(field)
212+
.map(isOptional -> !isOptional)
213+
.orElse(true);
214+
}
211215

212216
// Jackson @JsonProperty(required = true)
213217
JsonProperty jp = getJsonProperty(field);
214-
if (jp != null && jp.required()) return true;
215-
216-
return false;
217-
}
218+
return jp != null && jp.required();
219+
}
218220

219221
/**
220222
* Apply validations to schema. the annotation order effects the result of the
@@ -376,15 +378,15 @@ private static List<Class<? extends Annotation>> findOverrides(Annotation annota
376378
* @param field the field
377379
* @return the boolean
378380
*/
379-
private static Boolean nullableFromAnnotations(Field field) {
380-
if (hasNullableAnnotation(field)) return true;
381-
if (hasNotNullAnnotation(field)) return false;
381+
private static Optional<Boolean> nullableFromAnnotations(Field field) {
382+
if (hasNullableAnnotation(field)) return Optional.of(true);
383+
if (hasNotNullAnnotation(field)) return Optional.of(false);
382384
Method getter = findGetter(field);
383385
if (getter != null) {
384-
if (hasNullableAnnotation(getter)) return true;
385-
if (hasNotNullAnnotation(getter)) return false;
386+
if (hasNullableAnnotation(getter)) return Optional.of(true);
387+
if (hasNotNullAnnotation(getter)) return Optional.of(false);
386388
}
387-
return null;
389+
return Optional.empty();
388390
}
389391

390392
/**
@@ -579,15 +581,13 @@ public static void fixOAS31ExclusiveConstraints(Schema<?> schema) {
579581
return;
580582
}
581583
if (schema.getSpecVersion().equals(SpecVersion.V31)) {
582-
if (schema.getExclusiveMaximumValue() != null && schema.getMaximum() != null) {
583-
if (schema.getMaximum().compareTo(schema.getExclusiveMaximumValue()) == 0) {
584-
schema.setMaximum(null);
585-
}
584+
if (schema.getExclusiveMaximumValue() != null && schema.getMaximum() != null
585+
&& schema.getMaximum().compareTo(schema.getExclusiveMaximumValue()) == 0) {
586+
schema.setMaximum(null);
586587
}
587-
if (schema.getExclusiveMinimumValue() != null && schema.getMinimum() != null) {
588-
if (schema.getMinimum().compareTo(schema.getExclusiveMinimumValue()) == 0) {
589-
schema.setMinimum(null);
590-
}
588+
if (schema.getExclusiveMinimumValue() != null && schema.getMinimum() != null &&
589+
schema.getMinimum().compareTo(schema.getExclusiveMinimumValue()) == 0) {
590+
schema.setMinimum(null);
591591
}
592592
}
593593
}

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package org.springdoc.core.utils;
2828

2929
import java.lang.reflect.Field;
30+
import java.util.Optional;
3031

3132
import kotlin.jvm.JvmClassMappingKt;
3233
import kotlin.reflect.KClass;
@@ -46,6 +47,9 @@
4647
*/
4748
public class SpringDocKotlinUtils {
4849

50+
public SpringDocKotlinUtils() {
51+
}
52+
4953
/**
5054
* Is kotlin declaring class boolean.
5155
*
@@ -64,19 +68,18 @@ static boolean isKotlinDeclaringClass(Field f) {
6468
* @param f the f
6569
* @return the boolean
6670
*/
67-
private static Boolean kotlinMarkedNullableFallback(Field f) {
71+
private static Optional<Boolean> kotlinMarkedNullableFallback(Field f) {
6872
try {
6973
KClass<?> kClass = JvmClassMappingKt.getKotlinClass(f.getDeclaringClass());
70-
for (Object pObj : KClasses.getMemberProperties((KClass<Object>) kClass)) {
71-
KProperty1<?, ?> p = (KProperty1<?, ?>) pObj;
72-
if (p.getName().equals(f.getName())) {
73-
return p.getReturnType().isMarkedNullable();
74+
for (KProperty1<?, ?> pObj : KClasses.getMemberProperties(kClass)) {
75+
if (pObj.getName().equals(f.getName())) {
76+
return Optional.of(pObj.getReturnType().isMarkedNullable());
7477
}
7578
}
7679
} catch (Exception ignored) {
7780
// best-effort only
7881
}
79-
return null;
82+
return Optional.empty();
8083
}
8184

8285
/**
@@ -85,21 +88,21 @@ private static Boolean kotlinMarkedNullableFallback(Field f) {
8588
* @param f the f
8689
* @return the boolean
8790
*/
88-
static Boolean kotlinConstructorParamIsOptional(Field f) {
91+
static Optional<Boolean> kotlinConstructorParamIsOptional(Field f) {
8992
try {
9093
KClass<?> kClass = JvmClassMappingKt.getKotlinClass(f.getDeclaringClass());
9194
KFunction<?> primary = KClasses.getPrimaryConstructor(kClass);
9295
if (primary != null) {
9396
for (KParameter p : primary.getParameters()) {
9497
if (f.getName().equals(p.getName())) {
95-
return p.isOptional();
98+
return Optional.of(p.isOptional());
9699
}
97100
}
98101
}
99102
} catch (Exception ignored) {
100103
// best-effort only
101104
}
102-
return null;
105+
return Optional.empty();
103106
}
104107

105108
/**
@@ -108,12 +111,12 @@ static Boolean kotlinConstructorParamIsOptional(Field f) {
108111
* @param field the field
109112
* @return the boolean
110113
*/
111-
static Boolean kotlinNullability(Field field) {
112-
if (!isKotlinDeclaringClass(field)) return null;
114+
static Optional<Boolean> kotlinNullability(Field field) {
115+
if (!isKotlinDeclaringClass(field)) return Optional.empty();
113116

114117
KProperty<?> prop = ReflectJvmMapping.getKotlinProperty(field);
115118
if (prop != null) {
116-
return prop.getReturnType().isMarkedNullable();
119+
return Optional.of(prop.getReturnType().isMarkedNullable());
117120
}
118121

119122
return kotlinMarkedNullableFallback(field);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,9 @@ public static void fixNullOnlyAdditionalProperties(Schema<?> schema) {
201201
if (additionalProperties instanceof Schema<?> addPropSchema) {
202202
boolean isNullOnlyType = false;
203203
Set<String> types = addPropSchema.getTypes();
204-
if (types != null && types.size() == 1 && types.contains("null")) {
205-
isNullOnlyType = true;
206-
}
207-
else if (types == null && "null".equals(addPropSchema.getType())) {
204+
boolean onlyNullTypeOAS31 = types != null && types.size() == 1 && types.contains("null");
205+
boolean onlyNullTypeOAS30 = types == null && "null".equals(addPropSchema.getType());
206+
if (onlyNullTypeOAS31 || onlyNullTypeOAS30) {
208207
isNullOnlyType = true;
209208
}
210209
if (isNullOnlyType && addPropSchema.get$ref() == null
@@ -214,7 +213,7 @@ else if (types == null && "null".equals(addPropSchema.getType())) {
214213
}
215214
}
216215
if (schema.getProperties() != null) {
217-
schema.getProperties().values().forEach(prop -> fixNullOnlyAdditionalProperties((Schema<?>) prop));
216+
schema.getProperties().values().forEach(SpringDocUtils::fixNullOnlyAdditionalProperties);
218217
}
219218
}
220219

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
*/
3434
public class ScalarConstants {
3535

36+
private ScalarConstants() {
37+
}
38+
3639
/**
3740
* The constant SCALAR_DEFAULT_PATH.
3841
*/

springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerConfigurer.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
package org.springdoc.ui;
2828

2929

30+
import java.util.ArrayList;
3031
import java.util.Arrays;
32+
import java.util.List;
3133

3234
import org.springdoc.core.properties.SwaggerUiConfigProperties;
3335

@@ -185,10 +187,10 @@ private PathPattern parsePattern(String pattern) {
185187
* @param patterns the patterns to match.
186188
* @param locations the locations to use.
187189
*/
188-
protected record SwaggerResourceHandlerConfig(boolean cacheResources, String[] patterns, String[] locations) {
190+
protected record SwaggerResourceHandlerConfig(boolean cacheResources, List<String> patterns, List<String> locations) {
189191

190192
private SwaggerResourceHandlerConfig(boolean cacheResources) {
191-
this(cacheResources, new String[]{}, new String[]{});
193+
this(cacheResources, new ArrayList<>(), new ArrayList<>());
192194
}
193195

194196
/**
@@ -199,7 +201,15 @@ private SwaggerResourceHandlerConfig(boolean cacheResources) {
199201
* @return the updated config.
200202
*/
201203
public SwaggerResourceHandlerConfig setPatterns(String... patterns) {
202-
return new SwaggerResourceHandlerConfig(cacheResources, patterns, locations);
204+
return new SwaggerResourceHandlerConfig(cacheResources, Arrays.asList(patterns), locations);
205+
}
206+
207+
/**
208+
*
209+
* @return String array of patterns
210+
*/
211+
public String[] patternsArray() {
212+
return patterns.toArray(new String[0]);
203213
}
204214

205215
/**
@@ -210,7 +220,15 @@ public SwaggerResourceHandlerConfig setPatterns(String... patterns) {
210220
* @return the updated config.
211221
*/
212222
public SwaggerResourceHandlerConfig setLocations(String... locations) {
213-
return new SwaggerResourceHandlerConfig(cacheResources, patterns, locations);
223+
return new SwaggerResourceHandlerConfig(cacheResources, patterns, Arrays.asList(locations));
224+
}
225+
226+
/**
227+
*
228+
* @return String array of locations
229+
*/
230+
public String[] locationsArray() {
231+
return locations.toArray(new String[0]);
214232
}
215233

216234
/**

springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ protected void addSwaggerResourceHandlers(ResourceHandlerRegistry registry, Swag
127127
* @param handlerConfig the swagger handler config.
128128
*/
129129
protected void addSwaggerResourceHandler(ResourceHandlerRegistry registry, SwaggerResourceHandlerConfig handlerConfig) {
130-
ResourceHandlerRegistration handlerRegistration = registry.addResourceHandler(handlerConfig.patterns());
131-
handlerRegistration.addResourceLocations(handlerConfig.locations());
130+
ResourceHandlerRegistration handlerRegistration = registry.addResourceHandler(handlerConfig.patternsArray());
131+
handlerRegistration.addResourceLocations(handlerConfig.locationsArray());
132132

133133
ResourceChainRegistration chainRegistration;
134134
if (handlerConfig.cacheResources()) {

0 commit comments

Comments
 (0)