[JAVA-2613] CustomMatchers#getFieldNames ignores superclass fields Created: 26/Sep/17  Updated: 29/Oct/23  Resolved: 16/Oct/17

Status: Closed
Project: Java Driver
Component/s: Test Coverage
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The bug in the matcher is due to a misunderstanding of how Groovy appends elements to a list: specifically, the += operator returns a new list rather than modifying the existing one.



 Comments   
Comment by Githook User [ 16/Oct/17 ]

Author:

{'email': 'jeff.yemin@10gen.com', 'name': 'Jeff Yemin', 'username': 'jyemin'}

Message: JAVA-2613: Fix issues in the CustomMatcher

  • include fields declared in all superclasses
  • do not include static fields
  • ensure that a field without a public accessor method can be compared

Also fix one test bug that was exposed by this fix
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/df3a40009f14b20c1066be47e36b75d38548f196

Comment by Jeffrey Yemin [ 26/Sep/17 ]

This will fix it:

    static List<String> getFieldNames(Class curClass, List<String> names) {
        if (curClass == Object) {
            return names
        }
        getFieldNames(curClass.getSuperclass(), names)
        def declaredFieldNames = curClass.declaredFields.findAll { !it.synthetic }*.name
        names.addAll(declaredFieldNames)
        return names
    }

When testing this fix, also found that static fields need to be excluded from the declared fields, and that DefaultServerConnectionSpecification tests are failing when comparing the commandListener property (not clear why).

Generated at Thu Feb 08 08:57:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.