Skip to content

Commit 3446185

Browse files
committed
fix nullability issues
1 parent e60b291 commit 3446185

16 files changed

Lines changed: 50 additions & 29 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public JsonMapperBuilderCustomizer jsonCustomizer(CustomTypeScanner configuratio
4848

4949
// dynamic deserializers
5050
configuration.getRelevantTypes()
51-
.forEach(type ->
52-
module.addDeserializer(type, new CustomTypeDeserializer(type))
51+
.forEach(
52+
type -> module.addDeserializer(type, new CustomTypeDeserializer(type))
5353
);
5454

5555
// type-based deserializer

src/main/java/it/aboutbits/springboot/toolbox/jackson/CustomTypeSerializer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public void serialize(
2828
case String stringValue -> jsonGenerator.writeString(stringValue);
2929
case UUID uuidValue -> jsonGenerator.writeString(uuidValue.toString());
3030
case Enum<?> enumValue -> jsonGenerator.writeString(enumValue.name());
31-
case null -> jsonGenerator.writeNull();
3231
default -> jsonGenerator.writeRawValue(String.valueOf(value));
3332
}
3433
}

src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypePropertyCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
3636
}
3737

3838
if (CustomType.class.isAssignableFrom(rawClass)) {
39-
Class<?> wrappedType = null;
39+
Class<?> wrappedType;
4040
if (rawClass.equals(EntityId.class)) {
4141
wrappedType = simpleType.getBindings()
4242
.getTypeParameters()

src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/default_not_null/NullableCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private static boolean isNullable(Schema<?> property) {
7070

7171
if (property.getItems() != null && property.getItems().get$ref() != null && property.getItems()
7272
.get$ref()
73-
.endsWith("?nullable=true")) {
73+
.endsWith(NULLABLE_MARKER)) {
7474
return true;
7575
}
7676

src/main/java/it/aboutbits/springboot/toolbox/util/CollectUtil.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package it.aboutbits.springboot.toolbox.util;
22

33
import org.jspecify.annotations.NullMarked;
4+
import org.jspecify.annotations.Nullable;
45
import org.springframework.data.util.Streamable;
56

67
import java.util.Collection;
@@ -23,68 +24,77 @@ public final class CollectUtil {
2324
private CollectUtil() {
2425
}
2526

26-
public static <T, R> Set<R> collectToSet(Collection<T> items, Function<T, R> mapper) {
27+
public static <T, R> Set<R> collectToSet(Collection<T> items, Function<T, @Nullable R> mapper) {
2728
return items.stream()
2829
.map(mapper)
2930
.filter(Objects::nonNull)
3031
.collect(Collectors.toUnmodifiableSet());
3132
}
3233

33-
public static <T, R> Set<R> collectToSet(Streamable<T> items, Function<T, R> mapper) {
34+
public static <T, R> Set<R> collectToSet(Streamable<T> items, Function<T, @Nullable R> mapper) {
3435
return items.stream()
3536
.map(mapper)
3637
.filter(Objects::nonNull)
3738
.collect(Collectors.toUnmodifiableSet());
3839
}
3940

40-
public static <T, R> Set<R> collectToSet(Stream<T> items, Function<T, R> mapper) {
41+
public static <T, R> Set<R> collectToSet(Stream<T> items, Function<T, @Nullable R> mapper) {
4142
return items
4243
.map(mapper)
4344
.filter(Objects::nonNull)
4445
.collect(Collectors.toUnmodifiableSet());
4546
}
4647

47-
public static <T, R> List<R> collectToList(Collection<T> items, Function<T, R> mapper) {
48+
@SuppressWarnings("java:S6204")
49+
public static <T, R> List<R> collectToList(Collection<T> items, Function<T, @Nullable R> mapper) {
50+
//noinspection SimplifyStreamApiCallChains
4851
return items.stream()
4952
.map(mapper)
5053
.filter(Objects::nonNull)
51-
.toList();
54+
.collect(Collectors.toUnmodifiableList());
5255
}
5356

54-
public static <T, R> List<R> collectToList(Streamable<T> items, Function<T, R> mapper) {
57+
@SuppressWarnings("java:S6204")
58+
public static <T, R> List<R> collectToList(Streamable<T> items, Function<T, @Nullable R> mapper) {
59+
//noinspection SimplifyStreamApiCallChains
5560
return items.stream()
5661
.map(mapper)
5762
.filter(Objects::nonNull)
58-
.toList();
63+
.collect(Collectors.toUnmodifiableList());
5964
}
6065

61-
public static <T, R> List<R> collectToList(Stream<T> items, Function<T, R> mapper) {
66+
@SuppressWarnings("java:S6204")
67+
public static <T, R> List<R> collectToList(Stream<T> items, Function<T, @Nullable R> mapper) {
68+
//noinspection SimplifyStreamApiCallChains
6269
return items
6370
.map(mapper)
6471
.filter(Objects::nonNull)
65-
.toList();
72+
.collect(Collectors.toUnmodifiableList());
6673
}
6774

68-
public static <T, R> Stream<R> collectToStream(Collection<T> items, Function<T, R> mapper) {
75+
public static <T, R> Stream<R> collectToStream(Collection<T> items, Function<T, @Nullable R> mapper) {
76+
//noinspection NullableProblems
6977
return items.stream()
7078
.map(mapper)
7179
.filter(Objects::nonNull);
7280
}
7381

74-
public static <T, R> Stream<R> collectToStream(Streamable<T> items, Function<T, R> mapper) {
82+
public static <T, R> Stream<R> collectToStream(Streamable<T> items, Function<T, @Nullable R> mapper) {
83+
//noinspection NullableProblems
7584
return items.stream()
7685
.map(mapper)
7786
.filter(Objects::nonNull);
7887
}
7988

80-
public static <T, R> Stream<R> collectToStream(Stream<T> items, Function<T, R> mapper) {
89+
public static <T, R> Stream<R> collectToStream(Stream<T> items, Function<T, @Nullable R> mapper) {
90+
//noinspection NullableProblems
8191
return items
8292
.map(mapper)
8393
.filter(Objects::nonNull);
8494
}
8595

8696
public static <T, K, R> Map<K, R> collectToMap(
87-
Collection<T> items,
97+
Collection<@Nullable T> items,
8898
Function<T, K> keyMapper,
8999
Function<T, R> valueMapper
90100
) {
@@ -99,12 +109,11 @@ public static <T, K, R> Map<K, R> collectToMap(
99109
Function<T, R> valueMapper
100110
) {
101111
return items.stream()
102-
.filter(Objects::nonNull)
103112
.collect(Collectors.toMap(keyMapper, valueMapper));
104113
}
105114

106115
public static <T, K, R> Map<K, R> collectToMap(
107-
Stream<T> items,
116+
Stream<@Nullable T> items,
108117
Function<T, K> keyMapper,
109118
Function<T, R> valueMapper
110119
) {

src/main/java/it/aboutbits/springboot/toolbox/web/EntityIdPropertyEditor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public String getAsText() {
1919

2020
@Override
2121
public void setAsText(String text) throws IllegalArgumentException {
22-
EntityId<?> value = null;
22+
EntityId<?> value;
2323
try {
2424
var theValue = Long.parseLong(text);
2525
value = new EntityId<Long>() {

src/test/java/it/aboutbits/springboot/toolbox/TestApp.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
@RegisterCustomTypesWithJacksonAndMvc
1111
@NullMarked
1212
public class TestApp {
13-
14-
public static void main(String[] args) {
13+
static void main(String[] args) {
1514
SpringApplication.run(TestApp.class, args);
1615
}
1716
}

src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import lombok.Getter;
1111
import lombok.Setter;
1212
import org.hibernate.annotations.JavaType;
13+
import org.jspecify.annotations.NonNull;
1314
import org.jspecify.annotations.NullMarked;
1415
import org.jspecify.annotations.NullUnmarked;
1516

@@ -18,7 +19,7 @@
1819
@Setter
1920
@Table(name = "custom_type_enum_test_model")
2021
@NullUnmarked
21-
public class CustomTypeEnumTestModel implements Identified<CustomTypeEnumTestModel.ID> {
22+
public class CustomTypeEnumTestModel implements Identified<CustomTypeEnumTestModel.@NonNull ID> {
2223
@Id
2324
@JavaType(CustomTypeEnumTestModel.ID.JavaType.class)
2425
private ID id;

src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeTestModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import lombok.Getter;
2121
import lombok.Setter;
2222
import org.hibernate.annotations.JavaType;
23+
import org.jspecify.annotations.NonNull;
2324
import org.jspecify.annotations.NullMarked;
2425
import org.jspecify.annotations.NullUnmarked;
2526

@@ -30,7 +31,7 @@
3031
@Setter
3132
@Table(name = "custom_type_test_model")
3233
@NullUnmarked
33-
public class CustomTypeTestModel implements Identified<CustomTypeTestModel.ID> {
34+
public class CustomTypeTestModel implements Identified<CustomTypeTestModel.@NonNull ID> {
3435
@Id
3536
@GeneratedValue(strategy = GenerationType.IDENTITY)
3637
@JavaType(CustomTypeTestModel.ID.JavaType.class)

src/test/java/it/aboutbits/springboot/toolbox/persistence/javatype/impl/jpa/WrapperTypesModel.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@
7171
import org.hibernate.annotations.JdbcType;
7272
import org.hibernate.type.descriptor.jdbc.CharJdbcType;
7373
import org.hibernate.type.descriptor.jdbc.UUIDJdbcType;
74+
import org.jspecify.annotations.NonNull;
75+
import org.jspecify.annotations.NullMarked;
7476
import org.jspecify.annotations.NullUnmarked;
7577

7678
@Entity
7779
@Getter
7880
@Setter
7981
@Table(name = "wrapper_type_test_model")
8082
@NullUnmarked
81-
public class WrapperTypesModel implements Identified<WrapperTypesModel.ID> {
83+
public class WrapperTypesModel implements Identified<WrapperTypesModel.@NonNull ID> {
8284
@Id
8385
@GeneratedValue(strategy = GenerationType.IDENTITY)
8486
@JavaType(ID.JavaType.class)
@@ -208,6 +210,7 @@ public class WrapperTypesModel implements Identified<WrapperTypesModel.ID> {
208210
@JavaType(WrapEnumClassJavaType.class)
209211
private WrapEnumClass enumValueClass;
210212

213+
@NullMarked
211214
public record ID(
212215
Long value
213216
) implements EntityId<Long> {

0 commit comments

Comments
 (0)