micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
464 stars 195 forks source link

Fix mongo aggregate projection #2950

Closed radovanradic closed 4 months ago

radovanradic commented 4 months ago

This workaround/fix resolves the issue There is logic to read @Introspected object from Mongo but document does not have values on the document root as mapper expects it in the case of this issue. So, catching error and falling back to this block

else if (result.size() == 2) {
            Optional<Map.Entry<String, BsonValue>> id = result.entrySet().stream().filter(f -> !f.getKey().equals("_id")).findFirst();
            if (id.isPresent()) {
                value = id.get().getValue();
            } else {
                value = result.values().iterator().next();
            }
        }

in this method resolves the issue. Maybe not the best solution, but resolves this case with minimal code changes that could cause other regression.

sonarcloud[bot] commented 4 months ago

Quality Gate Failed Quality Gate failed

Failed conditions
48.8% Coverage on New Code (required ≥ 70%)

See analysis details on SonarCloud

dstepanov commented 4 months ago

We should probably store the projected property name

radovanradic commented 4 months ago

We should probably store the projected property name

Not sure what you mean. Projected property is stored in mongoPreparedQuery.getAggregation().getPipeline().get(...) and I tried this approach first https://github.com/micronaut-projects/micronaut-data/pull/2950/commits/07ba513861ccf3e975ba9db0a2df411241755848#diff-a9e8459c8b432459acc2ee2c85b420f0e99af0c8e6614c86d15567a897f83f39R167 but current one looks more safe regarding performance and potential risk of regression.