Skip to content

Commit c045a8a

Browse files
committed
Fix MappingElasticsearchConverter.
Original Pull Request #2637 Closes #2627 (cherry picked from commit d9bb991)
1 parent 11c87a1 commit c045a8a

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
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

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

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

@@ -1953,12 +2002,12 @@ void shouldWriteEntityWithDottedFieldName() throws JSONException {
19532002

19542003
@Language("JSON")
19552004
var expected = """
1956-
{
1957-
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$FieldNameDotsEntity",
1958-
"id": "42",
1959-
"dotted.field": "dotted field"
1960-
}
1961-
""";
2005+
{
2006+
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$FieldNameDotsEntity",
2007+
"id": "42",
2008+
"dotted.field": "dotted field"
2009+
}
2010+
""";
19622011
var entity = new FieldNameDotsEntity();
19632012
entity.setId("42");
19642013
entity.setDottedField("dotted field");
@@ -3192,6 +3241,7 @@ public void setMapToNotWriteWhenEmpty(@Nullable Map<String, String> mapToNotWrit
31923241
this.mapToNotWriteWhenEmpty = mapToNotWriteWhenEmpty;
31933242
}
31943243
}
3244+
31953245
static class FieldNameDotsEntity {
31963246
@Id
31973247
@Nullable private String id;

0 commit comments

Comments
 (0)