Fix NPE issues in RSQLUtility (#959)

* Fix Sonar NPE issues in RSQLUtility
* Fix RSQL test to not result in null path

Signed-off-by: Alexander Dobler <alexander.dobler3@bosch-si.com>
This commit is contained in:
Alexander Dobler
2020-04-16 13:51:12 +02:00
committed by GitHub
parent 21a36a8bce
commit bff2c97e1f
2 changed files with 15 additions and 15 deletions

View File

@@ -374,10 +374,9 @@ public final class RSQLUtility {
* dot notated field path * dot notated field path
* @return the Path for a field * @return the Path for a field
*/ */
private Path<Object> getFieldPath(final A enumField, final String finalProperty) { private Path<Object> getFieldPath(final A enumField, final String finalProperty) {
return (Path<Object>) getFieldPath(root, getSubAttributesFrom(finalProperty), enumField.isMap(), return (Path<Object>) getFieldPath(root, getSubAttributesFrom(finalProperty), enumField.isMap(),
this::getJoinFieldPath); this::getJoinFieldPath).orElseThrow(() -> new RSQLParameterUnsupportedFieldException("RSQL field path cannot be empty", null));
} }
private Path<?> getJoinFieldPath(final Path<?> fieldPath, final String fieldNameSplit) { private Path<?> getJoinFieldPath(final Path<?> fieldPath, final String fieldNameSplit) {
@@ -398,18 +397,17 @@ public final class RSQLUtility {
return fieldPath; return fieldPath;
} }
private static Path<?> getFieldPath(final Root<?> root, final String[] split, final boolean isMapKeyField, private static Optional<Path<?>> getFieldPath(final Root<?> root, final String[] split, final boolean isMapKeyField,
final BiFunction<Path<?>, String, Path<?>> joinFieldPathProvider) { final BiFunction<Path<?>, String, Path<?>> joinFieldPathProvider) {
Path<?> fieldPath = null; Path<?> fieldPath = null;
for (int i = 0; i < split.length; i++) { for (int i = 0; i < split.length; i++) {
if (isMapKeyField && i == (split.length - 1)) { if (!(isMapKeyField && i == (split.length - 1))) {
return fieldPath; final String fieldNameSplit = split[i];
fieldPath = (fieldPath != null) ? fieldPath.get(fieldNameSplit) : root.get(fieldNameSplit);
fieldPath = joinFieldPathProvider.apply(fieldPath, fieldNameSplit);
} }
final String fieldNameSplit = split[i];
fieldPath = (fieldPath != null) ? fieldPath.get(fieldNameSplit) : root.get(fieldNameSplit);
fieldPath = joinFieldPathProvider.apply(fieldPath, fieldNameSplit);
} }
return fieldPath; return Optional.ofNullable(fieldPath);
} }
@Override @Override
@@ -800,9 +798,9 @@ public final class RSQLUtility {
} }
private static Path<?> getInnerFieldPath(final Root<?> subqueryRoot, final String[] split, private static Path<?> getInnerFieldPath(final Root<?> subqueryRoot, final String[] split,
final boolean isMapKeyField) { final boolean isMapKeyField) {
return getFieldPath(subqueryRoot, split, isMapKeyField, return getFieldPath(subqueryRoot, split, isMapKeyField,
(fieldPath, fieldNameSplit) -> getInnerJoinFieldPath(subqueryRoot, fieldPath, fieldNameSplit)); (fieldPath, fieldNameSplit) -> getInnerJoinFieldPath(subqueryRoot, fieldPath, fieldNameSplit)).orElseThrow(() -> new RSQLParameterUnsupportedFieldException("RSQL field path cannot be empty", null));
} }
private static Path<?> getInnerJoinFieldPath(final Root<?> subqueryRoot, final Path<?> fieldPath, private static Path<?> getInnerJoinFieldPath(final Root<?> subqueryRoot, final Path<?> fieldPath,

View File

@@ -239,7 +239,9 @@ public class RSQLUtilityTest {
when(baseSoftwareModuleRootMock.get(anyString())).thenReturn(baseSoftwareModuleRootMock); when(baseSoftwareModuleRootMock.get(anyString())).thenReturn(baseSoftwareModuleRootMock);
when(baseSoftwareModuleRootMock.getJavaType()).thenReturn((Class) SoftwareModule.class); when(baseSoftwareModuleRootMock.getJavaType()).thenReturn((Class) SoftwareModule.class);
when(subqueryRootMock.get(anyString())).thenReturn(mock(Path.class)); final Path pathMock = mock(Path.class);
when(pathMock.get(anyString())).thenReturn(pathMock);
when(subqueryRootMock.get(anyString())).thenReturn(pathMock);
when(criteriaBuilderMock.and(any(), any())).thenReturn(mock(Predicate.class)); when(criteriaBuilderMock.and(any(), any())).thenReturn(mock(Predicate.class));