Skip to content

Fix MappingElasticsearchConverter #2637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
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 @@ -919,7 +919,7 @@ private List<Object> writeCollectionInternal(Collection<?> source, @Nullable Typ

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

if (elementType == null || conversions.isSimpleType(elementType)) {
if (elementType == null || isSimpleType(elementType)) {
collection.add(getPotentiallyConvertedSimpleWrite(element,
componentType != null ? componentType.getType() : Object.class));
} else if (element instanceof Collection || elementType.isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,55 @@ void shouldWriteNullValueIfConfigured() throws JSONException {
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
class RangeTests {

Expand Down Expand Up @@ -1953,12 +2002,12 @@ void shouldWriteEntityWithDottedFieldName() throws JSONException {

@Language("JSON")
var expected = """
{
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$FieldNameDotsEntity",
"id": "42",
"dotted.field": "dotted field"
}
""";
{
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$FieldNameDotsEntity",
"id": "42",
"dotted.field": "dotted field"
}
""";
var entity = new FieldNameDotsEntity();
entity.setId("42");
entity.setDottedField("dotted field");
Expand Down Expand Up @@ -3192,6 +3241,7 @@ public void setMapToNotWriteWhenEmpty(@Nullable Map<String, String> mapToNotWrit
this.mapToNotWriteWhenEmpty = mapToNotWriteWhenEmpty;
}
}

static class FieldNameDotsEntity {
@Id
@Nullable private String id;
Expand Down