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

FMWK-628 Support null classKey #801

Merged
merged 2 commits into from
Dec 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
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ public MappingAerospikeConverter mappingAerospikeConverter(AerospikeMappingConte

@Bean(name = "aerospikeTypeAliasAccessor")
public AerospikeTypeAliasAccessor aerospikeTypeAliasAccessor(AerospikeDataSettings dataSettings) {
String classKey = dataSettings.getClassKey();
return StringUtils.hasText(classKey)
? new AerospikeTypeAliasAccessor(classKey)
: new AerospikeTypeAliasAccessor();
return new AerospikeTypeAliasAccessor(dataSettings.getClassKey());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@

import org.springframework.data.convert.TypeAliasAccessor;
import org.springframework.data.mapping.Alias;
import org.springframework.util.StringUtils;

import java.util.Map;

import static org.springframework.data.aerospike.convert.AerospikeConverter.CLASS_KEY_DEFAULT;

public class AerospikeTypeAliasAccessor implements TypeAliasAccessor<Map<String, Object>> {

private final String classKey;

public AerospikeTypeAliasAccessor(String classKey) {
this.classKey = classKey;
}

public AerospikeTypeAliasAccessor() {
this.classKey = CLASS_KEY_DEFAULT;
this.classKey = StringUtils.hasText(classKey) ? classKey : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected Map<Object, Object> convertMap(final Map<Object, Object> source, final
}

private Map<String, Object> convertCustomType(Object source, TypeInformation<?> type) {
Assert.notNull(source, "Given map must not be null!");
Assert.notNull(source, "Given source must not be null!");
Assert.notNull(type, "Given type must not be null!");

AerospikePersistentEntity<?> entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.stream.Stream;

import static java.util.function.Predicate.not;
import static org.springframework.data.aerospike.convert.AerospikeConverter.CLASS_KEY_DEFAULT;
import static org.springframework.data.aerospike.repository.query.CriteriaDefinition.AerospikeNullQueryCriterion;
import static org.springframework.data.aerospike.repository.query.CriteriaDefinition.AerospikeNullQueryCriterion.NULL_PARAM;
import static org.springframework.util.ClassUtils.isAssignable;
Expand Down Expand Up @@ -233,6 +232,10 @@ protected static void validateTypes(MappingAerospikeConverter converter, Class<?
&& queryParameters.get(0) instanceof Collection<?>) {
params = ((Collection<Object>) queryParameters.get(0)).stream();
}

// skipping further validations as they depend on using classKey
if (!StringUtils.hasText(converter.getAerospikeDataSettings().getClassKey())) return;

if (!params.allMatch(param -> isAssignableValueOrConverted(clazz, param, converter))) {
String validTypes = propertyType.getSimpleName();
if (alternativeTypes.length > 0) {
Expand Down Expand Up @@ -265,12 +268,13 @@ protected static void validateQueryIn(List<Object> queryParameters, String query
}
}

// works only when classKey configuration property is not null
protected static boolean isAssignableValueOrConverted(Class<?> propertyType, Object obj,
MappingAerospikeConverter converter) {
return isAssignableValue(propertyType, obj)
|| converter.getCustomConversions().hasCustomReadTarget(obj.getClass(), propertyType)
// POJOs and enums got converted to Strings when query parameters were set
|| isPojoMap(obj, propertyType)
|| isPojoMap(obj, propertyType, converter.getAerospikeDataSettings().getClassKey())
|| (propertyType.isEnum() && obj instanceof String);
}

Expand All @@ -279,12 +283,13 @@ protected static boolean isAssignableValueOrConverted(Class<?> propertyType, Obj
*
* @param object Instance to be compared
* @param propertyType Class for comparing
* @param classKey Name of the bin to store entity's class
* @return Whether the object is a converted POJO of the given class
*/
protected static boolean isPojoMap(Object object, Class<?> propertyType) {
protected static boolean isPojoMap(Object object, Class<?> propertyType, String classKey) {
if (object instanceof TreeMap<?, ?> treeMap) {
Object classKey = treeMap.get(CLASS_KEY_DEFAULT);
return classKey != null && classKey.equals(propertyType.getName());
Object className = treeMap.get(classKey);
return className != null && className.equals(propertyType.getName());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -95,6 +96,9 @@ private void validateMapQueryContaining(String queryPartDescription) {
"expecting two");
}

// skipping further validations as they depend on using classKey
if (!StringUtils.hasText(converter.getAerospikeDataSettings().getClassKey())) return;

if (!(isValidMapKeyTypeOrUnresolved(part.getProperty().getTypeInformation(), param2))) {
throw new IllegalArgumentException(queryPartDescription + ": invalid map key type at position 2");
}
Expand All @@ -105,6 +109,9 @@ private void validateMapQueryContaining(String queryPartDescription) {
"expecting two");
}

// skipping further validations as they depend on using classKey
if (!StringUtils.hasText(converter.getAerospikeDataSettings().getClassKey())) return;

if (!(isValidMapValueTypeOrUnresolved(part.getProperty().getTypeInformation(), param2))) {
throw new IllegalArgumentException(queryPartDescription + ": invalid map value type at position 2");
}
Expand All @@ -115,6 +122,9 @@ private void validateMapQueryContaining(String queryPartDescription) {
"expecting three");
}

// skipping further validations as they depend on using classKey
if (!StringUtils.hasText(converter.getAerospikeDataSettings().getClassKey())) return;

if (!(isValidMapKeyTypeOrUnresolved(part.getProperty().getTypeInformation(), param2))) {
throw new IllegalArgumentException(queryPartDescription + ": invalid map key type at position 2");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ protected MappingAerospikeConverter getAerospikeMappingConverterByOption(int con

protected MappingAerospikeConverter getMappingAerospikeConverter(AerospikeDataSettings settings,
Converter<?, ?>... customConverters) {
return getMappingAerospikeConverter(settings, new AerospikeTypeAliasAccessor(), customConverters);
return getMappingAerospikeConverter(settings, new AerospikeTypeAliasAccessor(settings.getClassKey()),
customConverters);
}

protected MappingAerospikeConverter getMappingAerospikeConverter(AerospikeDataSettings settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public void shouldReadObjectWithByteArrayFieldWithOneValueInData(int converterOp
@Test
public void getConversionService() {
MappingAerospikeConverter mappingAerospikeConverter =
getMappingAerospikeConverter(settings, new AerospikeTypeAliasAccessor());
getMappingAerospikeConverter(settings, new AerospikeTypeAliasAccessor(settings.getClassKey()));
assertThat(mappingAerospikeConverter.getConversionService()).isNotNull()
.isInstanceOf(DefaultConversionService.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ void shouldWriteAsByteArrayAndReadAsArrayList() {
}

@Test
void shouldWriteAndReadNestedPOJOs() {
void shouldWriteAndReadNestedPOJOsWithNullClassKey() {
MappingAerospikeConverter converter =
getMappingAerospikeConverter(settings, new AerospikeTypeAliasAccessor(null));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Collections;
import java.util.List;

import static org.springframework.data.aerospike.convert.AerospikeConverter.CLASS_KEY_DEFAULT;

/**
* @author Peter Milne
* @author Jean Mercier
Expand Down Expand Up @@ -66,7 +68,7 @@ tree1, new StubParameterAccessor(

private MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomConversions conversions) {
MappingAerospikeConverter converter = new MappingAerospikeConverter(new AerospikeMappingContext(),
conversions, new AerospikeTypeAliasAccessor(), new AerospikeDataSettings(null));
conversions, new AerospikeTypeAliasAccessor(CLASS_KEY_DEFAULT), new AerospikeDataSettings(null));
converter.afterPropertiesSet();
return converter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Map;
import java.util.stream.Stream;

import static org.springframework.data.aerospike.convert.AerospikeConverter.CLASS_KEY_DEFAULT;

public class QueryUtils {

private static final Map<Class<?>, Class<?>> WRAPPERS_TO_PRIMITIVES
Expand Down Expand Up @@ -107,7 +109,7 @@ private static Class<?> checkForPageable(Class<?> argType) {
private static MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomConversions conversions)
{
MappingAerospikeConverter converter = new MappingAerospikeConverter(new AerospikeMappingContext(),
conversions, new AerospikeTypeAliasAccessor(), new AerospikeDataSettings(null));
conversions, new AerospikeTypeAliasAccessor(CLASS_KEY_DEFAULT), new AerospikeDataSettings(null));
converter.afterPropertiesSet();
return converter;
}
Expand Down
Loading