mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-01 08:12:11 +00:00
Introduce MappingConversionException.
Original Pull Request #2882 Closes #2879
This commit is contained in:
parent
0a51dbab01
commit
6d51e67948
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.data.elasticsearch.core.convert;
|
||||||
|
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 5.3
|
||||||
|
* @author Peter-Josef Meisch
|
||||||
|
*/
|
||||||
|
public class MappingConversionException extends RuntimeException {
|
||||||
|
private final String documentId;
|
||||||
|
|
||||||
|
public MappingConversionException(@Nullable String documentId, Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
this.documentId = documentId != null ? documentId : "\"null\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDocumentId() {
|
||||||
|
return documentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return "Conversion exception when converting document id " + documentId;
|
||||||
|
}
|
||||||
|
}
|
@ -65,6 +65,8 @@ import org.springframework.util.ClassUtils;
|
|||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import javax.print.Doc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Elasticsearch specific {@link org.springframework.data.convert.EntityConverter} implementation based on domain type
|
* Elasticsearch specific {@link org.springframework.data.convert.EntityConverter} implementation based on domain type
|
||||||
* {@link ElasticsearchPersistentEntity metadata}.
|
* {@link ElasticsearchPersistentEntity metadata}.
|
||||||
@ -333,10 +335,13 @@ public class MappingElasticsearchConverter
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Document document = (source instanceof Document) ? (Document) source : null;
|
||||||
|
|
||||||
ElasticsearchPropertyValueProvider valueProvider = new ElasticsearchPropertyValueProvider(accessor, evaluator);
|
ElasticsearchPropertyValueProvider valueProvider = new ElasticsearchPropertyValueProvider(accessor, evaluator);
|
||||||
|
try {
|
||||||
R result = readProperties(targetEntity, instance, valueProvider);
|
R result = readProperties(targetEntity, instance, valueProvider);
|
||||||
|
|
||||||
if (source instanceof Document document) {
|
if (document != null) {
|
||||||
if (document.hasId()) {
|
if (document.hasId()) {
|
||||||
ElasticsearchPersistentProperty idProperty = targetEntity.getIdProperty();
|
ElasticsearchPersistentProperty idProperty = targetEntity.getIdProperty();
|
||||||
PersistentPropertyAccessor<R> propertyAccessor = new ConvertingPropertyAccessor<>(
|
PersistentPropertyAccessor<R> propertyAccessor = new ConvertingPropertyAccessor<>(
|
||||||
@ -371,8 +376,11 @@ public class MappingElasticsearchConverter
|
|||||||
if (source instanceof SearchDocument searchDocument) {
|
if (source instanceof SearchDocument searchDocument) {
|
||||||
populateScriptFields(targetEntity, result, searchDocument);
|
populateScriptFields(targetEntity, result, searchDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
} catch (ConversionException e) {
|
||||||
|
String documentId = (document != null && document.hasId()) ? document.getId() : null;
|
||||||
|
throw new MappingConversionException(documentId, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ParameterValueProvider<ElasticsearchPersistentProperty> getParameterProvider(
|
private ParameterValueProvider<ElasticsearchPersistentProperty> getParameterProvider(
|
||||||
@ -510,11 +518,9 @@ public class MappingElasticsearchConverter
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (source instanceof List<?> list) {
|
if (source instanceof List<?> list) {
|
||||||
source = list.stream().map(it -> convertOnRead(propertyValueConverter, it))
|
source = list.stream().map(it -> convertOnRead(propertyValueConverter, it)).collect(Collectors.toList());
|
||||||
.collect(Collectors.toList());
|
|
||||||
} else if (source instanceof Set<?> set) {
|
} else if (source instanceof Set<?> set) {
|
||||||
source = set.stream().map(it -> convertOnRead(propertyValueConverter, it))
|
source = set.stream().map(it -> convertOnRead(propertyValueConverter, it)).collect(Collectors.toSet());
|
||||||
.collect(Collectors.toSet());
|
|
||||||
} else {
|
} else {
|
||||||
source = convertOnRead(propertyValueConverter, source);
|
source = convertOnRead(propertyValueConverter, source);
|
||||||
}
|
}
|
||||||
|
@ -228,6 +228,13 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Notification");
|
"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Notification");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> writeToMap(Object source) {
|
||||||
|
|
||||||
|
Document sink = Document.create();
|
||||||
|
mappingElasticsearchConverter.write(source, sink);
|
||||||
|
return sink;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFailToInitializeGivenMappingContextIsNull() {
|
public void shouldFailToInitializeGivenMappingContextIsNull() {
|
||||||
|
|
||||||
@ -1178,11 +1185,13 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
public void setIntegerRangeList(List<Range<Integer>> integerRangeList) {
|
public void setIntegerRangeList(List<Range<Integer>> integerRangeList) {
|
||||||
this.integerRangeList = integerRangeList;
|
this.integerRangeList = integerRangeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class GeoJsonUnitTests {
|
class GeoJsonUnitTests {
|
||||||
|
|
||||||
private GeoJsonEntity entity;
|
private GeoJsonEntity entity;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
@ -1476,6 +1485,7 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
|
|
||||||
assertThat(entity).isEqualTo(mapped);
|
assertThat(entity).isEqualTo(mapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // #1454
|
@Test // #1454
|
||||||
@ -1942,13 +1952,6 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertThat(names).containsExactlyInAnyOrder("child1", "child2");
|
assertThat(names).containsExactlyInAnyOrder("child1", "child2");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> writeToMap(Object source) {
|
|
||||||
|
|
||||||
Document sink = Document.create();
|
|
||||||
mappingElasticsearchConverter.write(source, sink);
|
|
||||||
return sink;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test // #2364
|
@Test // #2364
|
||||||
@DisplayName("should not write id property to document source if configured so")
|
@DisplayName("should not write id property to document source if configured so")
|
||||||
void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo() throws JSONException {
|
void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo() throws JSONException {
|
||||||
@ -2078,6 +2081,25 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertThat(mappedNames).isEqualTo("level-one.level-two.key-word");
|
assertThat(mappedNames).isEqualTo("level-one.level-two.key-word");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2879
|
||||||
|
@DisplayName("should throw MappingConversionException with document id on reading error")
|
||||||
|
void shouldThrowMappingConversionExceptionWithDocumentIdOnReadingError() {
|
||||||
|
|
||||||
|
@Language("JSON")
|
||||||
|
String json = """
|
||||||
|
{
|
||||||
|
"birth-date": "this-is-not-a-local-date"
|
||||||
|
}""";
|
||||||
|
|
||||||
|
Document document = Document.parse(json);
|
||||||
|
document.setId("42");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
mappingElasticsearchConverter.read(Person.class, document);
|
||||||
|
}).isInstanceOf(MappingConversionException.class).hasFieldOrPropertyWithValue("documentId", "42")
|
||||||
|
.hasCauseInstanceOf(ConversionException.class);
|
||||||
|
}
|
||||||
|
|
||||||
// region entities
|
// region entities
|
||||||
public static class Sample {
|
public static class Sample {
|
||||||
@Nullable public @ReadOnlyProperty String readOnly;
|
@Nullable public @ReadOnlyProperty String readOnly;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user