Skip to content
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

Criteria.regex is converted to String when applying to ID fields #4674

Open
spekdrum opened this issue Mar 26, 2024 · 7 comments
Open

Criteria.regex is converted to String when applying to ID fields #4674

spekdrum opened this issue Mar 26, 2024 · 7 comments
Assignees
Labels
type: regression A regression from a previous release

Comments

@spekdrum
Copy link

spekdrum commented Mar 26, 2024

Hi, this issue #4649 is still happening in 4.2.4 with ID fields.

In QueryMapper#getMappedValue method, applyFiledTargetTypeHitToValue() returns the Pattern object correctly after the fix, but later ends calling QueryMapper#convertId() method, which ends calling getConversionService().canConvert() in MongoConverter.convertId() method. This is returning true, so Pattern is converted to String anyway.

image

image

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 26, 2024
@christophstrobl
Copy link
Member

Thank you for getting in touch. It would help us investigating the issue if you could take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

@christophstrobl christophstrobl added the status: waiting-for-feedback We need additional information before we can continue label Apr 4, 2024
@christophstrobl christophstrobl self-assigned this Apr 4, 2024
@spring-projects-issues
Copy link

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 Apr 11, 2024
@spekdrum
Copy link
Author

spekdrum commented Apr 11, 2024

Hi, I don't have the time right now for a full example, maybe next week.

Just create a query with a regexp on the key column, i.e.:

	final Query query = new Query();
	query.addCriteria(where(ID_FIELD).regex("^3517-"));			
	return this.mongoTemplate.find(query, RateRoomPricingData.class, RATES_COLLECTION);

This wont return any value because the regexp expression is lost due to conversion.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and 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 Apr 11, 2024
@spekdrum
Copy link
Author

spekdrum commented May 13, 2024

I built an example reproducing the problem:
mongo-demo.zip

In summary, the issue arises when filtering a collection by the _id field with a regexp. It doesn't return results because the resulting Pattern is being converted to a String. I showed the key lines involved in the conversion during a debugging session in my initial post.

Using spring boot 3.2.0+ the bug is reproduced, but using 3.1.11 it works ok.

I tried to enforce the spring-data-mongodb dependency version only, to 4.1.x, which is the working one, but the problem persists, so maybe the problem it's not fully up to this lib.

Code overview:

@SpringBootApplication
public class MongoDemoApplication {

	public static void main(final String[] args) {

		SpringApplication.run(MongoDemoApplication.class, args);
	}

	@Bean
	public CommandLineRunner commandLineRunner(final MongoTemplate mongoTemplate) {
		return args -> {
			final Query query = new Query();

			query.addCriteria(Criteria.where("_id").regex("^3517-"));

			final List<DocumentExample> documents = mongoTemplate.find(query, DocumentExample.class, "mycollection");

			documents.forEach(doc -> System.out.println(doc.getName()));
		};
	}
}

This should be the output when working ok:

test1
test2

but actually it prints nothing because this returns an empty list.

The attached example will be working as soon as you change spring boot parent to 3.1.11.

The collection is initialized this way in a init-mongo.js file:

db = db.getSiblingDB('testdb');
db.mycollection.insert([
	{_id: '3517-abv1', name: 'test1'},
	{_id: '3517-abz1', name: 'test2'},
	{_id: '2127-aswx', name: 'test3'}
]);
db.createUser({
	user: 'root',
	pwd: 'example',
	roles: [
		{role: 'readWrite', db: 'testdb'}
	]
});

@nandosangputra
Copy link

nandosangputra commented Jun 7, 2024

Hi, is there any updates on this issue? I also encountering the same issue ever since upgrading to springboot 3.2.0 with spring data mongodb 4.3.0

@christophstrobl
Copy link
Member

@nandosangputra thanks for the ping.

@christophstrobl christophstrobl added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Jun 7, 2024
@christophstrobl
Copy link
Member

thank you @spekdrum for the reproducer - I do see the error now which comes in when there's a combination of @Id and @Field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

4 participants