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

Field existence check for the elements of a collection field #3349

Closed
yfei-z opened this issue Jan 26, 2024 · 1 comment
Closed

Field existence check for the elements of a collection field #3349

yfei-z opened this issue Jan 26, 2024 · 1 comment
Assignees
Labels
theme: recursive comparison An issue related to the recursive comparison
Milestone

Comments

@yfei-z
Copy link

yfei-z commented Jan 26, 2024

Describe the bug
It will report "fields don't exist" if trying to specify the compared fields for the elements of a collection field.

  • assertj core version: 3.25.2
  • java version: 17
  • test framework version: JUnit 5

Test case reproducing the bug

	@Data
	static class Person {
		private final String name, address;
		private List<Person> friends = new ArrayList<>(), enemies = new ArrayList<>();
	}

	@Test
	void listElementFieldComparison() {
		Person sherlock1 = new Person("Sherlock Holmes", null);
		sherlock1.friends.add(new Person("Dr. John Watson", null));
		Person sherlock2 = new Person("Sherlock Holmes", null);
		sherlock2.friends.add(new Person("Dr. John Watson", "Baker Street"));

		assertThat(sherlock1).usingRecursiveComparison(RecursiveComparisonConfiguration.builder()
				.withComparedFields("friends.name")
				.build()).isEqualTo(sherlock2);
	}

The outcome

java.lang.IllegalArgumentException: The following fields don't exist: {name in <friends.name>}

	at org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.checkComparedFieldsExist(RecursiveComparisonConfiguration.java:1092)
	at org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator$ComparisonState.initDualValuesToCompare(RecursiveComparisonDifferenceCalculator.java:146)
	at org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator$ComparisonState.access$100(RecursiveComparisonDifferenceCalculator.java:73)
	at org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.determineDifferences(RecursiveComparisonDifferenceCalculator.java:237)
	at org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.determineDifferences(RecursiveComparisonDifferenceCalculator.java:228)
	at org.assertj.core.api.RecursiveComparisonAssert.determineDifferencesWith(RecursiveComparisonAssert.java:1770)
	at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:157)

Since I only use ComparingProperties as the RecursiveComparisonIntrospectionStrategy, so here is my temporary solution.

public class ComparingProperties extends org.assertj.core.api.recursive.comparison.ComparingProperties {

	@Override
	public Set<String> getChildrenNodeNamesOf(Object node) {
		if (node instanceof Collection<?> c) {
			Iterator<?> i = c.iterator(); if (!i.hasNext()) return Collections.emptySet();
			return getChildrenNodeNamesOf(i.next());
		}
		return super.getChildrenNodeNamesOf(node);
	}

	@Override
	public Object getChildNodeValue(String childNodeName, Object instance) {
		if (instance instanceof Collection<?> c) {
			Iterator<?> i = c.iterator(); if (!i.hasNext()) return null;
			return getChildNodeValue(childNodeName, i.next());
		}
		Object v = super.getChildNodeValue(childNodeName, instance);
		if (v instanceof Collection<?> c && c.isEmpty()) return null;
		return v;
	}
}
@joel-costigliola
Copy link
Member

Thanks for reporting this @yfei-z ! I'll take a look this week end

@joel-costigliola joel-costigliola modified the milestones: 3.25.2, 3.25.3 Jan 26, 2024
@joel-costigliola joel-costigliola self-assigned this Jan 26, 2024
@scordio scordio added the theme: recursive comparison An issue related to the recursive comparison label Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: recursive comparison An issue related to the recursive comparison
Projects
None yet
Development

No branches or pull requests

3 participants