Skip to content

Commit 70d2de7

Browse files
committed
Fix MappingElasticsearchConverter.
Original Pull Request #2637 Closes #2627 (cherry picked from commit d9bb991) (cherry picked from commit c045a8a)
1 parent 1666fce commit 70d2de7

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ private List<Object> writeCollectionInternal(Collection<?> source, @Nullable Typ
919919

920920
Class<?> elementType = element == null ? null : element.getClass();
921921

922-
if (elementType == null || conversions.isSimpleType(elementType)) {
922+
if (elementType == null || isSimpleType(elementType)) {
923923
collection.add(getPotentiallyConvertedSimpleWrite(element,
924924
componentType != null ? componentType.getType() : Object.class));
925925
} else if (element instanceof Collection || elementType.isArray()) {

src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java

+49
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,55 @@ void shouldWriteNullValueIfConfigured() throws JSONException {
908908
assertEquals(expected, document.toJson(), false);
909909
}
910910

911+
@Test // #2627
912+
@DisplayName("should write Map containing collection containing map")
913+
void shouldWriteMapContainingCollectionContainingMap() throws JSONException {
914+
915+
class EntityWithMapCollectionMap {
916+
Map<String, Object> map;
917+
}
918+
class InnerEntity {
919+
String prop1;
920+
921+
String prop2;
922+
923+
public InnerEntity() {}
924+
925+
public InnerEntity(String prop1, String prop2) {
926+
this.prop1 = prop1;
927+
this.prop2 = prop2;
928+
}
929+
930+
}
931+
932+
var entity = new EntityWithMapCollectionMap();
933+
entity.map = Collections.singletonMap("collection",
934+
Collections.singletonList(Collections.singletonMap("destination", new InnerEntity("prop1", "prop2"))));
935+
936+
var expected = """
937+
{
938+
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$1EntityWithMapCollectionMap",
939+
"map": {
940+
"collection": [
941+
{
942+
"destination": {
943+
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$1InnerEntity",
944+
"prop1": "prop1",
945+
"prop2": "prop2"
946+
}
947+
}
948+
]
949+
}
950+
}
951+
""";
952+
953+
Document document = Document.create();
954+
955+
mappingElasticsearchConverter.write(entity, document);
956+
957+
assertEquals(expected, document.toJson(), false);
958+
}
959+
911960
@Nested
912961
class RangeTests {
913962

0 commit comments

Comments
 (0)