Skip to content
Merged
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 @@ -82,7 +82,8 @@ public class MappingElasticsearchConverter
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
private final GenericConversionService conversionService;

private CustomConversions conversions = new ElasticsearchCustomConversions(Collections.emptyList());
// don't access directly, use getConversions(). to prevent null access
@Nullable private CustomConversions conversions = null;
private final EntityInstantiators instantiators = new EntityInstantiators();

private final ElasticsearchTypeMapper typeMapper;
Expand Down Expand Up @@ -133,14 +134,22 @@ public void setConversions(CustomConversions conversions) {
this.conversions = conversions;
}

private CustomConversions getConversions() {

if (conversions == null) {
conversions = new ElasticsearchCustomConversions(Collections.emptyList());
}
return conversions;
}

/*
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() {
DateFormatterRegistrar.addDateConverters(conversionService);
conversions.registerConvertersIn(conversionService);
getConversions().registerConvertersIn(conversionService);
}

// region read
Expand All @@ -151,7 +160,7 @@ public <R> R read(Class<R> type, Document source) {
TypeInformation<R> typeHint = ClassTypeInformation.from((Class<R>) ClassUtils.getUserClass(type));
typeHint = (TypeInformation<R>) typeMapper.readType(source, typeHint);

if (conversions.hasCustomReadTarget(Map.class, typeHint.getType())) {
if (getConversions().hasCustomReadTarget(Map.class, typeHint.getType())) {
R converted = conversionService.convert(source, typeHint.getType());
if (converted == null) {
// EntityReader.read is defined as non nullable , so we cannot return null
Expand All @@ -177,7 +186,7 @@ protected <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String,

EntityInstantiator instantiator = instantiators.getInstantiatorFor(targetEntity);

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "ConstantConditions"})
R instance = (R) instantiator.createInstance(targetEntity,
new PersistentEntityParameterValueProvider<>(targetEntity, propertyValueProvider, null));

Expand Down Expand Up @@ -223,6 +232,7 @@ protected <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String,
if (source instanceof SearchDocument) {
SearchDocument searchDocument = (SearchDocument) source;
if (targetEntity.hasScoreProperty()) {
//noinspection ConstantConditions
targetEntity.getPropertyAccessor(result) //
.setProperty(targetEntity.getScoreProperty(), searchDocument.getScore());
}
Expand Down Expand Up @@ -276,7 +286,7 @@ protected <R> R readValue(@Nullable Object source, ElasticsearchPersistentProper
if (property.hasPropertyConverter()) {
source = propertyConverterRead(property, source);
} else if (TemporalAccessor.class.isAssignableFrom(property.getType())
&& !conversions.hasCustomReadTarget(source.getClass(), rawType)) {
&& !getConversions().hasCustomReadTarget(source.getClass(), rawType)) {

// log at most 5 times
String propertyName = property.getOwner().getType().getSimpleName() + '.' + property.getName();
Expand All @@ -291,7 +301,7 @@ protected <R> R readValue(@Nullable Object source, ElasticsearchPersistentProper
}
}

if (conversions.hasCustomReadTarget(source.getClass(), rawType)) {
if (getConversions().hasCustomReadTarget(source.getClass(), rawType)) {
return rawType.cast(conversionService.convert(source, rawType));
} else if (source instanceof List) {
return readCollectionValue((List<?>) source, property, targetType);
Expand Down Expand Up @@ -356,8 +366,6 @@ && isSimpleType(componentType.getType())) {
} else if (value instanceof Map) {
target
.add(readMapValue((Map<String, Object>) value, property, property.getTypeInformation().getActualType()));
} else {
target.add(readEntity(computeGenericValueTypeForRead(property, value), (Map<String, Object>) value));
}
}
}
Expand Down Expand Up @@ -423,7 +431,7 @@ private Object readSimpleValue(@Nullable Object value, TypeInformation<?> target
return value;
}

if (conversions.hasCustomReadTarget(value.getClass(), target)) {
if (getConversions().hasCustomReadTarget(value.getClass(), target)) {
return conversionService.convert(value, target);
}

Expand Down Expand Up @@ -477,7 +485,7 @@ public void write(Object source, Document sink) {
typeMapper.writeType(source.getClass(), sink);
}

Optional<Class<?>> customTarget = conversions.getCustomWriteTarget(entityType, Map.class);
Optional<Class<?>> customTarget = getConversions().getCustomWriteTarget(entityType, Map.class);

if (customTarget.isPresent()) {
sink.putAll(conversionService.convert(source, Map.class));
Expand Down Expand Up @@ -526,7 +534,7 @@ protected void writeProperties(ElasticsearchPersistentEntity<?> entity, Persiste
if (property.hasPropertyConverter()) {
value = propertyConverterWrite(property, value);
} else if (TemporalAccessor.class.isAssignableFrom(property.getActualType())
&& !conversions.hasCustomWriteTarget(value.getClass())) {
&& !getConversions().hasCustomWriteTarget(value.getClass())) {

// log at most 5 times
String propertyName = entity.getType().getSimpleName() + '.' + property.getName();
Expand Down Expand Up @@ -568,7 +576,7 @@ private Object propertyConverterWrite(ElasticsearchPersistentProperty property,

protected void writeProperty(ElasticsearchPersistentProperty property, Object value, MapValueAccessor sink) {

Optional<Class<?>> customWriteTarget = conversions.getCustomWriteTarget(value.getClass());
Optional<Class<?>> customWriteTarget = getConversions().getCustomWriteTarget(value.getClass());

if (customWriteTarget.isPresent()) {
Class<?> writeTarget = customWriteTarget.get();
Expand All @@ -595,7 +603,7 @@ protected void writeProperty(ElasticsearchPersistentProperty property, Object va

@Nullable
protected Object getWriteSimpleValue(Object value) {
Optional<Class<?>> customTarget = conversions.getCustomWriteTarget(value.getClass());
Optional<Class<?>> customTarget = getConversions().getCustomWriteTarget(value.getClass());

if (customTarget.isPresent()) {
return conversionService.convert(value, customTarget.get());
Expand Down Expand Up @@ -759,8 +767,8 @@ private boolean requiresTypeHint(TypeInformation<?> type, Class<?> actualType,
}
}

return !conversions.isSimpleType(type.getType()) && !type.isCollectionLike()
&& !conversions.hasCustomWriteTarget(type.getType());
return !getConversions().isSimpleType(type.getType()) && !type.isCollectionLike()
&& !getConversions().hasCustomWriteTarget(type.getType());
}

/**
Expand Down Expand Up @@ -789,7 +797,7 @@ private boolean isSimpleType(Object value) {
}

private boolean isSimpleType(Class<?> type) {
return conversions.isSimpleType(type);
return getConversions().isSimpleType(type);
}
// endregion

Expand Down Expand Up @@ -819,7 +827,7 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
field.setName(property.getFieldName());

if (property.hasPropertyConverter()) {
ElasticsearchPersistentPropertyConverter propertyConverter = property.getPropertyConverter();
ElasticsearchPersistentPropertyConverter propertyConverter = Objects.requireNonNull(property.getPropertyConverter());
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
Object value = criteriaEntry.getValue();
if (value.getClass().isArray()) {
Expand Down