Skip to content

Commit

Permalink
Merge pull request #207 from croz-ltd/feature_upgradeSpringBootToLate…
Browse files Browse the repository at this point in the history
…stVersion

Feature upgrade spring boot to latest version
  • Loading branch information
jzrilic authored May 16, 2024
2 parents 5ed93f6 + a648571 commit ee32915
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gradleJgitverPluginVersion=0.10.0-rc03
gradlePublishPluginVersion=1.1.0
modelMapperVersion=3.1.1
reflectionsVersion=0.10.2
springBootVersion=3.1.5
springBootVersion=3.2.5

# POM metadata
developerEmail=[email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.croz.nrich.search.model.AttributeHolder;
import net.croz.nrich.search.util.AttributeResolvingUtil;
import net.croz.nrich.search.util.PathResolvingUtil;
import org.springframework.util.Assert;

Expand All @@ -40,7 +41,7 @@ public AttributeHolder resolveAttributeByPath(String path) {

String[] pathList = PathResolvingUtil.convertToPathList(path);

Attribute<?, ?> attribute = resolveAttributeByName(managedType, pathList[0]);
Attribute<?, ?> attribute = AttributeResolvingUtil.resolveAttributeByName(managedType, pathList[0]);
boolean isPlural = attribute instanceof PluralAttribute;
ManagedType<?> currentManagedType = resolveManagedTypeFromAttribute(attribute);

Expand All @@ -52,7 +53,7 @@ public AttributeHolder resolveAttributeByPath(String path) {
isPlural = false;
break;
}
attribute = resolveAttributeByName(currentManagedType, currentPath);
attribute = AttributeResolvingUtil.resolveAttributeByName(currentManagedType, currentPath);
currentManagedType = resolveManagedTypeFromAttribute(attribute);
isPlural |= attribute instanceof PluralAttribute;
}
Expand All @@ -73,13 +74,4 @@ else if (attribute instanceof PluralAttribute && ((PluralAttribute<?, ?, ?>) att

return currentManagedType;
}

private Attribute<?, ?> resolveAttributeByName(ManagedType<?> managedType, String attributeName) {
try {
return managedType.getAttribute(attributeName);
}
catch (Exception ignored) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.croz.nrich.search.util;

import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.ManagedType;

import org.hibernate.metamodel.model.domain.AbstractManagedType;

public final class AttributeResolvingUtil {

private AttributeResolvingUtil() {
}

public static Attribute<?, ?> resolveAttributeByName(ManagedType<?> managedType, String attributeName) {
if (managedType instanceof AbstractManagedType<?> abstractManagedType) {
Attribute<?, ?> attribute = abstractManagedType.findAttribute(attributeName);
if (attribute == null) {
return abstractManagedType.findSubTypesAttribute(attributeName);
}

return attribute;
}
else {
try {
return managedType.getAttribute(attributeName);
}
catch (Exception ignored) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ private static boolean shouldJoinPath(Path<?> calculatedPath, String currentPath
}

if (calculatedPath.getModel() instanceof EntityType<?> entityType) {
Attribute<?, ?> attribute = entityType.getAttribute(currentPathSegment);
Attribute<?, ?> attribute = AttributeResolvingUtil.resolveAttributeByName(entityType, currentPathSegment);

return attribute.isCollection() || attribute.isAssociation();
if (attribute != null) {
return attribute.isCollection() || attribute.isAssociation();
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.croz.nrich.search.util;

import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.ManagedType;

import org.hibernate.metamodel.model.domain.AbstractManagedType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

class AttributeResolvingUtilTest {

private static final String ATTRIBUTE_NAME = "attributeName";

private static final Attribute<?, ?> ATTRIBUTE_VALUE = mock(Attribute.class);

private static final PersistentAttribute<?, ?> PERSISTENT_ATTRIBUTE_VALUE = mock(PersistentAttribute.class);

@Test
void shouldResolveAttributeFromManagedType() {
// given
ManagedType<?> managedType = mock(ManagedType.class);
doReturn(ATTRIBUTE_VALUE).when(managedType).getAttribute(ATTRIBUTE_NAME);

// when
Attribute<?, ?> result = AttributeResolvingUtil.resolveAttributeByName(managedType, ATTRIBUTE_NAME);

// then
assertThat(result).isEqualTo(ATTRIBUTE_VALUE);
}

@Test
void shouldReturnNullIfAttributeDoesNotExist() {
// given
ManagedType<?> managedType = mock(ManagedType.class);
doThrow(IllegalArgumentException.class).when(managedType).getAttribute(ATTRIBUTE_NAME);

// when
Attribute<?, ?> result = AttributeResolvingUtil.resolveAttributeByName(managedType, ATTRIBUTE_NAME);

// then
assertThat(result).isNull();
}

@Test
void shouldResolveAttributeFromAbstractManagedType() {
// given
AbstractManagedType<?> managedType = mock(AbstractManagedType.class);
doReturn(PERSISTENT_ATTRIBUTE_VALUE).when(managedType).findAttribute(ATTRIBUTE_NAME);

// when
Attribute<?, ?> result = AttributeResolvingUtil.resolveAttributeByName(managedType, ATTRIBUTE_NAME);

// then
assertThat(result).isEqualTo(PERSISTENT_ATTRIBUTE_VALUE);
}

@Test
void shouldResolveSubclassAttributeFromAbstractManagedType() {
// given
AbstractManagedType<?> managedType = mock(AbstractManagedType.class);
doReturn(PERSISTENT_ATTRIBUTE_VALUE).when(managedType).findSubTypesAttribute(ATTRIBUTE_NAME);

// when
Attribute<?, ?> result = AttributeResolvingUtil.resolveAttributeByName(managedType, ATTRIBUTE_NAME);

// then
assertThat(result).isEqualTo(PERSISTENT_ATTRIBUTE_VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ public void initBinder(WebDataBinder binder) {
}

if (ignoreTransientFields) {
Object target = binder.getTarget();
Class<?> targetType = null;
if (binder.getTarget() != null) {
targetType = binder.getTarget().getClass();
}
else if (binder.getTargetType() != null) {
targetType = binder.getTargetType().resolve();
}

if (target == null) {
if (targetType == null) {
return;
}

List<String> transientPropertyList = transientPropertyResolverService.resolveTransientPropertyList(target.getClass());
List<String> transientPropertyList = transientPropertyResolverService.resolveTransientPropertyList(targetType);

binder.setDisallowedFields(transientPropertyList.toArray(new String[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ void shouldConvertEmptyStringsToNull() throws Exception {
void shouldNotBindTransientFields() throws Exception {
// given
String requestUrl = fullUrl("transient-properties-serialization");
String value = "value";
Map<String, String> request = createMap("transientProperty", "transient", "property", "nonTransient");

// when
Expand Down

0 comments on commit ee32915

Please sign in to comment.