Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature upgrade spring boot to latest version #207

Merged
merged 3 commits into from
May 16, 2024
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
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
[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
Loading