Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2688

BsonCreator annotation processing depends on arbitrary ordering of constructors and factory methods

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.0
    • Affects Version/s: None
    • Component/s: POJO
    • None

      The org.bson.codecs.pojo.ConventionAnnotationImpl#processPropertyAnnotations method first looks for constructors annotated with @BsonCreator then for static methods annotated with BsonCreator. But if a class contains more than one of either type, the results are unpredictable and depend on the order that the constructors or methods in the lists provided by the reflection API.

      For example, a class like:

      public class Person {
          // oops, missing BsonProperty annotation
          @BsonCreator Person(String name);
      
          // properly annotated
          @BsonCreator Person(@BsonProperty("name") String name, @BsonProperty("age") int age);
          // ...
      }
      

      will result in an exception if the first constructor is first in the list, but not if the second constructor is first in the list.

      If the intention is that at most one constructor or static method annotated with @BsonCreator is permitted, then all of them should be examined rather than breaking out of the loop on the first one.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: