mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 13:12:10 +00:00
Fix MappingElasticsearchConverter.
Original Pull Request #2637 Closes #2627 (cherry picked from commit d9bb9911f91f2da8723e579fb3962ecf13a5a651)
This commit is contained in:
parent
11c87a1251
commit
c045a8ae29
@ -919,7 +919,7 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
Class<?> elementType = element == null ? null : element.getClass();
|
Class<?> elementType = element == null ? null : element.getClass();
|
||||||
|
|
||||||
if (elementType == null || conversions.isSimpleType(elementType)) {
|
if (elementType == null || isSimpleType(elementType)) {
|
||||||
collection.add(getPotentiallyConvertedSimpleWrite(element,
|
collection.add(getPotentiallyConvertedSimpleWrite(element,
|
||||||
componentType != null ? componentType.getType() : Object.class));
|
componentType != null ? componentType.getType() : Object.class));
|
||||||
} else if (element instanceof Collection || elementType.isArray()) {
|
} else if (element instanceof Collection || elementType.isArray()) {
|
||||||
|
@ -909,6 +909,55 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertEquals(expected, document.toJson(), false);
|
assertEquals(expected, document.toJson(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2627
|
||||||
|
@DisplayName("should write Map containing collection containing map")
|
||||||
|
void shouldWriteMapContainingCollectionContainingMap() throws JSONException {
|
||||||
|
|
||||||
|
class EntityWithMapCollectionMap {
|
||||||
|
Map<String, Object> map;
|
||||||
|
}
|
||||||
|
class InnerEntity {
|
||||||
|
String prop1;
|
||||||
|
|
||||||
|
String prop2;
|
||||||
|
|
||||||
|
public InnerEntity() {}
|
||||||
|
|
||||||
|
public InnerEntity(String prop1, String prop2) {
|
||||||
|
this.prop1 = prop1;
|
||||||
|
this.prop2 = prop2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var entity = new EntityWithMapCollectionMap();
|
||||||
|
entity.map = Collections.singletonMap("collection",
|
||||||
|
Collections.singletonList(Collections.singletonMap("destination", new InnerEntity("prop1", "prop2"))));
|
||||||
|
|
||||||
|
var expected = """
|
||||||
|
{
|
||||||
|
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$1EntityWithMapCollectionMap",
|
||||||
|
"map": {
|
||||||
|
"collection": [
|
||||||
|
{
|
||||||
|
"destination": {
|
||||||
|
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$1InnerEntity",
|
||||||
|
"prop1": "prop1",
|
||||||
|
"prop2": "prop2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
Document document = Document.create();
|
||||||
|
|
||||||
|
mappingElasticsearchConverter.write(entity, document);
|
||||||
|
|
||||||
|
assertEquals(expected, document.toJson(), false);
|
||||||
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class RangeTests {
|
class RangeTests {
|
||||||
|
|
||||||
@ -1953,12 +2002,12 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
|
|
||||||
@Language("JSON")
|
@Language("JSON")
|
||||||
var expected = """
|
var expected = """
|
||||||
{
|
{
|
||||||
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$FieldNameDotsEntity",
|
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$FieldNameDotsEntity",
|
||||||
"id": "42",
|
"id": "42",
|
||||||
"dotted.field": "dotted field"
|
"dotted.field": "dotted field"
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
var entity = new FieldNameDotsEntity();
|
var entity = new FieldNameDotsEntity();
|
||||||
entity.setId("42");
|
entity.setId("42");
|
||||||
entity.setDottedField("dotted field");
|
entity.setDottedField("dotted field");
|
||||||
@ -3192,6 +3241,7 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
this.mapToNotWriteWhenEmpty = mapToNotWriteWhenEmpty;
|
this.mapToNotWriteWhenEmpty = mapToNotWriteWhenEmpty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class FieldNameDotsEntity {
|
static class FieldNameDotsEntity {
|
||||||
@Id
|
@Id
|
||||||
@Nullable private String id;
|
@Nullable private String id;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user