Skip to content

Search by alike() criteria is broken when type alias information is not available [DATAMONGO-2620] #3474

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

Closed
spring-projects-issues opened this issue Sep 4, 2020 · 6 comments
Assignees
Labels
in: core Issues in core support type: documentation A documentation update

Comments

@spring-projects-issues
Copy link

Roman Puchkovskiy opened DATAMONGO-2620 and commented

It is possible to configure MappingMongoConverter in such a way that 'class' attribute is not saved to MongoDB. For example, the following setup

MappingContextTypeInformationMapper typeInformationMapper = new MappingContextTypeInformationMapper(
 mappingContext);
DefaultMongoTypeMapper typeMapper = new DefaultMongoTypeMapper(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY,
 singletonList(typeInformationMapper));

MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
converter.setTypeMapper(typeMapper);

would produce the following behavior: if an entity class is annotated with @TypeAlias, then the corresponding type alias is saved in class attribute to MongoDB, otherwise no such attribute is added.

This setup seems legitimate: one could decide that they do not need class attribute at all if they do not plan to ever use inheritance for some their entity classes.

But this setup makes Criteria.alike(Example) queries useless: they do not find anything. This happens, because DefaultMongoTypeMapper#writeTypeRestrictions() does not check whether restrictedMappedTypes is empty and adds "{'class': {$in: [...]"}} restriction even in such an (empty) case, which produces "{'class': {$in: []"}} which matches nothing as there is simply no class attribute.

This restriction is needed to make sure that only entities of the 'right' type are found. But if we don't have any type alias information, it seems natural to just drop the restriction


Affects: 2.2.9 (Moore SR9), 3.0.3 (Neumann SR3)

Referenced from: pull request #884

@spring-projects-issues
Copy link
Author

Christoph Strobl commented

Thanks for bringing this to our attention.
If you opt out of having type information written, you may do so as well for the Example by using an untyped one, as outlined in the reference documentation.

Looking at the tests in PR#884 the following should work out of the box.

Criteria alikeCriteria = new Criteria().alike(Example.of(savedPerson, UntypedExampleMatcher.matching()));
List<Person> foundPeople = templateSavingNoTypeInfoInDb.find(new Query(alikeCriteria), Person.class);

@spring-projects-issues
Copy link
Author

Roman Puchkovskiy commented

I see, the untyped matched really makes the test pass with the unmodified mapper. Thank you, I overlooked this API feature.

But, on the other hand, does it even make sense to try to make a typed matching when we know that there is no type information available? Also, Example.of(person) does not explicitly state that it will use type-strict check at all times.

Anyway, please let me know about your decision about this. If you feel that the existing behavior is ok, I will close the PR

@spring-projects-issues spring-projects-issues added status: waiting-for-feedback We need additional information before we can continue type: bug A general bug in: core Issues in core support labels Dec 30, 2020
@spring-projects-issues
Copy link
Author

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Jan 6, 2021
@spring-projects-issues
Copy link
Author

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Jan 13, 2021
@rpuch
Copy link
Contributor

rpuch commented Jan 18, 2021

I wonder why I have not received any notification about this issue...

@christophstrobl
Copy link
Member

@rpuch Sorry about that! Might be due to the recent Jira to Github issues migration. The automation added a comment to the ticket in Jira but did not (to avoid noise) try to link authors to GH users when moving tickets over.

We'll be addressing this issue by updating the documentation.

christophstrobl added a commit that referenced this issue Jan 19, 2021
This commit adds a note explaining scenarios suitable for an UntypedExampleMatcher.

Closes: #3474
mp911de pushed a commit that referenced this issue Jan 26, 2021
This commit adds a note explaining scenarios suitable for an UntypedExampleMatcher.

Closes: #3474
Original pull request: #3538.
mp911de pushed a commit that referenced this issue Jan 26, 2021
This commit adds a note explaining scenarios suitable for an UntypedExampleMatcher.

Closes: #3474
Original pull request: #3538.
@mp911de mp911de added type: documentation A documentation update and removed type: bug A general bug labels Jan 26, 2021
@mp911de mp911de added this to the 3.0.7 (Neumann SR7) milestone Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support type: documentation A documentation update
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants