Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
This issue is proposing separating the recursive logic of QueryMappergetMappedObject() from the the public method it is invoked by. This allows to handle the outer Document query and the nested levels of Document query separately.
Detail
We (Cisco Defense Orchestrator) are overriding MongoTemplate to provide some restrictions on database access. Specifically, injecting a Criteria to all queries, setting a field on all objects stored in the database, and preventing that field being updated.
To inject the Criteria we’ve set MongoTemplate.queryMapper() to an overridden implementation of QueryMapper through reflection that modifies the Document query parameter of QueryMapper.getMappedObject().
An issue we’ve encountered is that QueryMapper.getMappedObject() is called recursively inside QueryMapper, so this modification is done at every level.
Summary
This issue is proposing separating the recursive logic of
QueryMappergetMappedObject()
from the the public method it is invoked by. This allows to handle the outerDocument query
and the nested levels ofDocument query
separately.Detail
We (Cisco Defense Orchestrator) are overriding
MongoTemplate
to provide some restrictions on database access. Specifically, injecting aCriteria
to all queries, setting a field on all objects stored in the database, and preventing that field being updated.To inject the
Criteria
we’ve setMongoTemplate.queryMapper()
to an overridden implementation ofQueryMapper
through reflection that modifies theDocument query
parameter ofQueryMapper.getMappedObject()
.An issue we’ve encountered is that
QueryMapper.getMappedObject()
is called recursively insideQueryMapper
, so this modification is done at every level.For example, if we had the query:
We would want:
But we would get:
Which fails with
UncategorizedMongoDbException: Query failed with error code 2 and error message 'extra fields in $text'
.We solved this by setting adding an
isNested
parameter:The
ThreadLocal
is for thread safety.A nicer solution would be provided by introducing a separate protected recurse method called internally in
QueryMapper
, so that the publicgetMappedObject
could be overridden without effecting recursive calls. See the proposed change here: https://github.com/RyanGibb/spring-data-mongodb/commit/19a1f759344715d4b0c6abea59b073cc5319f664Any thoughts or feedback would be much appreciated.