Skip to content

Commit b09a77b

Browse files
committed
chore: act upon SonarQube warnings
#3307
1 parent cc3d86d commit b09a77b

10 files changed

Lines changed: 84 additions & 56 deletions

File tree

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
@@ -197,11 +197,13 @@ public String getParameterName() {
197197
}
198198

199199
@Override
200+
@Nullable
200201
public Method getMethod() {
201202
return delegate.getMethod();
202203
}
203204

204205
@Override
206+
@Nullable
205207
public Constructor<?> getConstructor() {
206208
return delegate.getConstructor();
207209
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.lang.reflect.ParameterizedType;
3535
import java.lang.reflect.Type;
3636
import java.lang.reflect.WildcardType;
37+
import java.util.ArrayList;
3738
import java.util.Arrays;
3839
import java.util.LinkedHashMap;
3940
import java.util.List;
@@ -384,7 +385,7 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
384385
Annotation[] paramAnnotations = getParameterAnnotations(methodParameter);
385386
TypeAndTypeAnnotations resolved = resolveTypeAndTypeAnnotationsForParameter(methodParameter);
386387
Type type = resolved.type();
387-
Annotation[] typeAnnotations = resolved.typeAnnotations();
388+
Annotation[] typeAnnotations = resolved.typeAnnotations().toArray(Annotation[]::new);
388389
Annotation[] mergedAnnotations =
389390
Stream.concat(
390391
Arrays.stream(paramAnnotations),
@@ -433,7 +434,7 @@ private TypeAndTypeAnnotations resolveTypeAndTypeAnnotationsForParameter(MethodP
433434
&& delegatingMethodParameter.getField() != null) {
434435
AnnotatedType annotated = delegatingMethodParameter.getField().getAnnotatedType();
435436
Type type = GenericTypeResolver.resolveType(annotated.getType(), methodParameter.getContainingClass());
436-
return new TypeAndTypeAnnotations(type, annotationsFromAnnotatedTypeArguments(annotated));
437+
return new TypeAndTypeAnnotations(type, Arrays.asList(annotationsFromAnnotatedTypeArguments(annotated)));
437438
}
438439

439440
Type type = GenericTypeResolver.resolveType(methodParameter.getGenericParameterType(), methodParameter.getContainingClass());
@@ -443,17 +444,17 @@ private TypeAndTypeAnnotations resolveTypeAndTypeAnnotationsForParameter(MethodP
443444
&& type == String.class) {
444445
Class<?> restored = KotlinInlineParameterResolver.resolveInlineType(methodParameter, type);
445446
return restored != null
446-
? new TypeAndTypeAnnotations(restored, restored.getAnnotations())
447-
: new TypeAndTypeAnnotations(type, new Annotation[0]);
447+
? new TypeAndTypeAnnotations(restored, Arrays.asList(restored.getAnnotations()))
448+
: new TypeAndTypeAnnotations(type, new ArrayList<>());
448449
}
449450

450-
return new TypeAndTypeAnnotations(type, methodParameter.getParameterType().getAnnotations());
451+
return new TypeAndTypeAnnotations(type, Arrays.asList(methodParameter.getParameterType().getAnnotations()));
451452
}
452453

453454
/**
454455
* Pair of resolved Java type and type annotations merged with parameter annotations for {@code extractSchema}.
455456
*/
456-
private record TypeAndTypeAnnotations(Type type, Annotation[] typeAnnotations) {
457+
private record TypeAndTypeAnnotations(Type type, List<Annotation> typeAnnotations) {
457458
}
458459

459460
/**

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: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,17 @@ public boolean fieldNullable(Field field) {
172172
}
173173

174174
// @Nullable/@NotNull
175-
Boolean ann = nullableFromAnnotations(field);
176-
if (ann != null) return ann;
175+
Optional<Boolean> ann = nullableFromAnnotations(field);
176+
if (ann.isPresent()) {
177+
return ann.get();
178+
}
177179

178180
// Kotlin nullability
179181
if (kotlinUtilsOptional.isPresent() && isKotlinDeclaringClass(field)) {
180-
Boolean kotlin = kotlinNullability(field);
181-
if (kotlin != null) return kotlin;
182+
Optional<Boolean> kotlin = kotlinNullability(field);
183+
if (kotlin.isPresent()) {
184+
return kotlin.get();
185+
}
182186
}
183187

184188
return JAVA_FIELD_NULLABLE_DEFAULT;
@@ -213,16 +217,14 @@ public boolean fieldRequired(Field field, io.swagger.v3.oas.annotations.media.Sc
213217
// Kotlin logic
214218
if (kotlinUtilsOptional.isPresent() && isKotlinDeclaringClass(field)) {
215219
if (fieldNullable(field)) return false;
216-
Boolean hasDefault = kotlinConstructorParamIsOptional(field);
217-
if (Boolean.TRUE.equals(hasDefault)) return false;
218-
return true;
220+
return kotlinConstructorParamIsOptional(field)
221+
.map(isOptional -> !isOptional)
222+
.orElse(true);
219223
}
220224

221225
// Jackson @JsonProperty(required = true)
222226
JsonProperty jp = getJsonProperty(field);
223-
if (jp != null && jp.required()) return true;
224-
225-
return false;
227+
return jp != null && jp.required();
226228
}
227229

228230
/**
@@ -385,15 +387,15 @@ private static List<Class<? extends Annotation>> findOverrides(Annotation annota
385387
* @param field the field
386388
* @return the boolean
387389
*/
388-
private static Boolean nullableFromAnnotations(Field field) {
389-
if (hasNullableAnnotation(field)) return true;
390-
if (hasNotNullAnnotation(field)) return false;
390+
private static Optional<Boolean> nullableFromAnnotations(Field field) {
391+
if (hasNullableAnnotation(field)) return Optional.of(true);
392+
if (hasNotNullAnnotation(field)) return Optional.of(false);
391393
Method getter = findGetter(field);
392394
if (getter != null) {
393-
if (hasNullableAnnotation(getter)) return true;
394-
if (hasNotNullAnnotation(getter)) return false;
395+
if (hasNullableAnnotation(getter)) return Optional.of(true);
396+
if (hasNotNullAnnotation(getter)) return Optional.of(false);
395397
}
396-
return null;
398+
return Optional.empty();
397399
}
398400

399401
/**
@@ -588,15 +590,13 @@ public static void fixOAS31ExclusiveConstraints(Schema<?> schema) {
588590
return;
589591
}
590592
if (schema.getSpecVersion().equals(SpecVersion.V31)) {
591-
if (schema.getExclusiveMaximumValue() != null && schema.getMaximum() != null) {
592-
if (schema.getMaximum().compareTo(schema.getExclusiveMaximumValue()) == 0) {
593-
schema.setMaximum(null);
594-
}
593+
if (schema.getExclusiveMaximumValue() != null && schema.getMaximum() != null
594+
&& schema.getMaximum().compareTo(schema.getExclusiveMaximumValue()) == 0) {
595+
schema.setMaximum(null);
595596
}
596-
if (schema.getExclusiveMinimumValue() != null && schema.getMinimum() != null) {
597-
if (schema.getMinimum().compareTo(schema.getExclusiveMinimumValue()) == 0) {
598-
schema.setMinimum(null);
599-
}
597+
if (schema.getExclusiveMinimumValue() != null && schema.getMinimum() != null &&
598+
schema.getMinimum().compareTo(schema.getExclusiveMinimumValue()) == 0) {
599+
schema.setMinimum(null);
600600
}
601601
}
602602
}

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
@@ -6,7 +6,9 @@
66
import org.springframework.web.util.pattern.PathPatternParser;
77
import org.springframework.web.util.pattern.PatternParseException;
88

9+
import java.util.ArrayList;
910
import java.util.Arrays;
11+
import java.util.List;
1012

1113
import static org.springdoc.core.utils.Constants.ALL_PATTERN;
1214
import static org.springdoc.core.utils.Constants.INDEX_PAGE_PATTERN;
@@ -157,10 +159,10 @@ private PathPattern parsePattern(String pattern) {
157159
* @param patterns the patterns to match.
158160
* @param locations the locations to use.
159161
*/
160-
protected record SwaggerResourceHandlerConfig(boolean cacheResources, String[] patterns, String[] locations) {
162+
protected record SwaggerResourceHandlerConfig(boolean cacheResources, List<String> patterns, List<String> locations) {
161163

162164
private SwaggerResourceHandlerConfig(boolean cacheResources) {
163-
this(cacheResources, new String[]{}, new String[]{});
165+
this(cacheResources, new ArrayList<>(), new ArrayList<>());
164166
}
165167

166168
/**
@@ -171,7 +173,15 @@ private SwaggerResourceHandlerConfig(boolean cacheResources) {
171173
* @return the updated config.
172174
*/
173175
public SwaggerResourceHandlerConfig setPatterns(String... patterns) {
174-
return new SwaggerResourceHandlerConfig(cacheResources, patterns, locations);
176+
return new SwaggerResourceHandlerConfig(cacheResources, Arrays.asList(patterns), locations);
177+
}
178+
179+
/**
180+
*
181+
* @return String array of patterns
182+
*/
183+
public String[] patternsArray() {
184+
return patterns.toArray(new String[0]);
175185
}
176186

177187
/**
@@ -182,7 +192,15 @@ public SwaggerResourceHandlerConfig setPatterns(String... patterns) {
182192
* @return the updated config.
183193
*/
184194
public SwaggerResourceHandlerConfig setLocations(String... locations) {
185-
return new SwaggerResourceHandlerConfig(cacheResources, patterns, locations);
195+
return new SwaggerResourceHandlerConfig(cacheResources, patterns, Arrays.asList(locations));
196+
}
197+
198+
/**
199+
*
200+
* @return String array of locations
201+
*/
202+
public String[] locationsArray() {
203+
return locations.toArray(new String[0]);
186204
}
187205

188206
/**

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()) {

springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWebMvcConfigurer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ protected void addSwaggerResourceHandlers(ResourceHandlerRegistry registry, Swag
145145
* @param handlerConfig the swagger handler config.
146146
*/
147147
protected void addSwaggerResourceHandler(ResourceHandlerRegistry registry, SwaggerResourceHandlerConfig handlerConfig) {
148-
ResourceHandlerRegistration handlerRegistration = registry.addResourceHandler(handlerConfig.patterns());
149-
handlerRegistration.addResourceLocations(handlerConfig.locations());
148+
ResourceHandlerRegistration handlerRegistration = registry.addResourceHandler(handlerConfig.patternsArray());
149+
handlerRegistration.addResourceLocations(handlerConfig.locationsArray());
150150

151151
ResourceChainRegistration chainRegistration;
152152
if (handlerConfig.cacheResources()) {

0 commit comments

Comments
 (0)