Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ build/

### Local Development ###
application-local.yml
/.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public String[] getScannedPackages() {

@SuppressWarnings("unchecked")
public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> clazz) {
return scanResult.getClassesImplementing(clazz).loadClasses()
var classInfoList = clazz.isInterface()
? scanResult.getClassesImplementing(clazz)
: scanResult.getSubclasses(clazz);
return classInfoList.loadClasses()
.stream()
.map(item -> (Class<? extends T>) item)
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public class SwaggerMeta {

@Nullable
private String mapKeyTypeFqn = null;

@Nullable
@JsonProperty("isNullable")
private Boolean isNullable = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public static String setIsNestedStructure(@Nullable String currentMeta, boolean
return OBJECT_MAPPER.writeValueAsString(meta);
}

@SneakyThrows(JsonProcessingException.class)
public static String setIsNullable(@Nullable String currentMeta, boolean value) {
var meta = getSwaggerMeta(currentMeta);
meta.setIsNullable(!value ? null : true);

return OBJECT_MAPPER.writeValueAsString(meta);
}

private static SwaggerMeta getSwaggerMeta(@Nullable String currentMeta) {
var meta = new SwaggerMeta();
if (currentMeta != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil;
import org.jspecify.annotations.NullMarked;
import org.springdoc.core.customizers.OpenApiCustomizer;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

@NullMarked
Expand Down Expand Up @@ -57,9 +62,98 @@ private static void processProperties(
} else {
requiredProperties.remove(propertyName);
}

// Check for nullable type parameters in collections/arrays
var annotatedType = getAnnotatedType(cls, propertyName);
if (annotatedType != null) {
var nullableDepths = new ArrayList<Integer>();
findNullableDepths(annotatedType, 0, nullableDepths);
// Add "nullable" description at each depth where nullable elements are found
for (var depth : nullableDepths) {
addNullableDescriptionAtDepth(property, depth);
}
}
});
}

@org.jspecify.annotations.Nullable
private static AnnotatedType getAnnotatedType(Class<?> cls, String propertyName) {
var currentClass = cls;
while (currentClass != null) {
try {
var field = currentClass.getDeclaredField(propertyName);
return field.getAnnotatedType();
} catch (NoSuchFieldException _) {
}

for (var method : currentClass.getDeclaredMethods()) {
if (method.getName().equals(propertyName)
|| method.getName().equals("get" + capitalize(propertyName))
|| method.getName().equals("is" + capitalize(propertyName))) {
return method.getAnnotatedReturnType();
}
}

currentClass = currentClass.getSuperclass();
}
return null;
}

private static void findNullableDepths(AnnotatedType annotatedType, int depth, ArrayList<Integer> nullableDepths) {
if (annotatedType instanceof AnnotatedParameterizedType parameterizedType) {
var rawType = parameterizedType.getType();
if (rawType instanceof ParameterizedType pt) {
var rawClass = pt.getRawType();
if (rawClass instanceof Class<?> clazz && isCollectionType(clazz)) {
var typeArgs = parameterizedType.getAnnotatedActualTypeArguments();
for (var typeArg : typeArgs) {
if (hasNullableAnnotation(typeArg)) {
nullableDepths.add(depth);
}
// Recursively check nested type parameters
findNullableDepths(typeArg, depth + 1, nullableDepths);
}
}
}
} else if (annotatedType instanceof AnnotatedArrayType arrayType) {
var componentType = arrayType.getAnnotatedGenericComponentType();
if (hasNullableAnnotation(componentType)) {
nullableDepths.add(depth);
}
// Recursively check nested array types
findNullableDepths(componentType, depth + 1, nullableDepths);
}
}

@SuppressWarnings("rawtypes")
private static void addNullableDescriptionAtDepth(Schema<?> schema, int depth) {
Schema currentSchema = schema;
for (int i = 0; i <= depth; i++) {
var items = currentSchema.getItems();
if (items == null) {
return; // Schema structure doesn't match expected depth
}
currentSchema = items;
}
currentSchema.setDescription(SwaggerMetaUtil.setIsNullable(
currentSchema.getDescription(),
true
));
}

private static boolean isCollectionType(Class<?> clazz) {
return Collection.class.isAssignableFrom(clazz) || clazz.isArray();
}

private static boolean hasNullableAnnotation(AnnotatedType annotatedType) {
for (var annotation : annotatedType.getAnnotations()) {
if (annotation.annotationType().getSimpleName().equals("Nullable")) {
return true;
}
}
return false;
}

@org.jspecify.annotations.Nullable
private static Class<?> loadClass(String fqn) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public EmailAddress(@Nullable String value) {
this.value = value.toLowerCase();
}

@SuppressWarnings("unused")
EmailAddress(EmailAddress other) {
this(other.value);
}

@Override
public String toString() {
return value;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/it/aboutbits/springboot/toolbox/type/Iban.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public Iban(@Nullable String value) {
this.value = value.toUpperCase();
}

@SuppressWarnings("unused")
Iban(Iban other) {
this(other.value);
}

@Override
public String toString() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public record ScaledBigDecimal(
public static final ScaledBigDecimal TWO = new ScaledBigDecimal(2);
public static final ScaledBigDecimal TEN = new ScaledBigDecimal(10);

@SuppressWarnings("unused")
ScaledBigDecimal(ScaledBigDecimal other) {
this(other.value);
}

public ScaledBigDecimal(BigDecimal value) {
this.value = value.setScale(MATH_CONTEXT.getPrecision(), MATH_CONTEXT.getRoundingMode());
}
Expand Down
Loading