-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Incorrect sink population #2627
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
Comments
Before I commit this change, can you please check if the test I wrote covers your case - I'd say so, but better have a secoond look: @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);
}
This change then makes the test pass. |
When applying your test case to my class layout I experience the issue as expected. Adding the |
I will merge and backport this later this evening |
Problem
When indexing an entity with a complex field containing multiple nested objects, we experience an exception during the population of the elasticsearch document.
Exception
I assume this Exception raises, because there is no custom writer attached to convert an object of type
ProcessFile
.Steps to reproduce the issue
We use the
MappingElasticsearchConverter
from theElasticsearchRestTemplate
instance to populate aDocument
with ourProcessCommitEntity
. That document is passed to anUpdateQuery.Builder
used to invoke theelasticsearchOperations.update
method.Class layout
Integration test
There is an
ProcessCommitEntity
about to be saved using the corresponding elasticsearch data repository.Population of the update builder
Issue
Issue is not the update operation itself, but the previously created
Document
still containing aProcessFile
Objekt instead of aMapDocument
representing theProcessFile
. The incorrect conversion is enforced byMappingElasticsearchConverter.writeCollectionInternal
:The outcoming

Document
looks like:Expectation
When indexing less nested data the
ProcessFile
is automatically being converted to aMapDocument
. TheMap
/List
/Map
/ProcessFile
construct breaks the conversion. I expect theProcessFile
to be converted to aMapDocument
regardless of the structure containing it.Quick fix
Registering a custom conversion class adding the missing check for
Map
just likeMappingElasticsearchConverter.Writer.isSimpleType
does, fixes the issue.In this scenario the

Map
inside of theList
is not threaten as simple type, therefore the usual conversion routine kicks in. The outcomingDocument
looks like:Possible spring data elasticsearch fix?
MappingElasticsearchConverter.writeCollectionInternal
does useconversions.isSimpleType
to determine if the contained type is written as simple value, as collection or as map. However, incase of aMap
residing in theList
it considers theMap
to be written as simple value. Therefore no further conversion is taken into account. The original value is written to the sink. IfMappingElasticsearchConverter.Writer.isSimpleType
would be used instead ofconversions.isSimpleType
, it would behave correctly.The text was updated successfully, but these errors were encountered: