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

$all on _id leads to Exception if Collection with one element is passed #4736

Closed
YuriiAndreitsev opened this issue Jul 3, 2024 · 3 comments
Assignees
Labels
in: mapping Mapping and conversion infrastructure type: bug A general bug

Comments

@YuriiAndreitsev
Copy link

if one element in Collection is passed in .all() criteria it is not preserved as array in spring-data-mongodb higher that 4.3.0

image

image

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

Thank you for getting in touch. Please do not upload images but take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem. Thank you!

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 3, 2024
@YuriiAndreitsev
Copy link
Author

YuriiAndreitsev commented Jul 3, 2024

okay, sorry for that, i will take a note of that,

i've debugged a little bit, it look like in this method it skips in the first phase it skips if ("$nin".equals(key) || "$in".equals(key)) and in result recursively converts objectId from array with single element into one objectId and of course it becomes invalid for $all operator

`

      private Object convertIdField(Field documentField, Object source) {

	Object value = source;
	if (isDBObject(source)) {
		DBObject valueDbo = (DBObject) source;
		value = new Document(valueDbo.toMap());
	}

	if (!isDocument(value)) {
		return convertId(value, getIdTypeForField(documentField));
	}

	Document valueDbo = (Document) value;
	Document resultDbo = new Document(valueDbo);

	for (Entry<String, Object> entry : valueDbo.entrySet()) {

		String key = entry.getKey();
		if ("$nin".equals(key) || "$in".equals(key)) {
			List<Object> ids = new ArrayList<>();
			for (Object id : (Iterable<?>) valueDbo.get(key)) {
				ids.add(convertId(id, getIdTypeForField(documentField)));
			}
			resultDbo.put(key, ids);
		} else if (isKeyword(key)) {
			resultDbo.put(key, convertIdField(documentField, entry.getValue()));
		} else {
			if (documentField.getProperty() != null && documentField.getProperty().isEntity()) {
				Field propertyField = createPropertyField(documentField.getPropertyEntity(), key, mappingContext);
				resultDbo.put(key, getMappedValue(propertyField, entry.getValue()));
			} else {
				resultDbo.put(key, getMappedValue(documentField, entry.getValue()));
			}
		}
	}

	return resultDbo;
}`

@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 labels Jul 3, 2024
@christophstrobl
Copy link
Member

Thank you @YuriiAndreitsev - I was able to reproduce the issue. We'll have a closer look!

@christophstrobl christophstrobl self-assigned this Jul 5, 2024
@christophstrobl christophstrobl added type: bug A general bug in: mapping Mapping and conversion infrastructure and removed status: feedback-provided Feedback has been provided labels Jul 5, 2024
@mp911de mp911de changed the title $all BadValue Exception if Collection with one element is passed $all on _id leads to Exception if Collection with one element is passed Aug 30, 2024
mp911de pushed a commit that referenced this issue Aug 30, 2024
Fix the id mapping for queries using the $all operator. Prior to this change the collection nature of the id values was not preserved leading to an invalid query.

Original pull request: #4742
Closes #4736
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants