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

@BsonId and @BsonProperty are disregarded for 'get_id()' accessor

    • Type: Icon: New Feature New Feature
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.1.1
    • Component/s: Codecs
    • Labels:
      None

      I'm evaluating steps required to migrate some legacy code to using of current mongodb java driver with embedded POJO mappgin and have problems with POJOs having '_id' field exposed via 'get_id()' accessor.

      I tried adding @BsonId annotation with no help. After digging through sources I've found the problem - @BsonId and @BsonProperty annotations are not used properly during introspection.

      The problem is here: https://github.com/mongodb/mongo-java-driver/blob/r4.1.1/bson/src/main/org/bson/codecs/pojo/PropertyReflectionUtils.java#L33

      Code excludes all methods that do not follow getters/setters naming conventions for java beans before annotations processing comes into place - this way user has no chance to override non-standard cases via @BsonId and @BsonProperty annotations.

      Here is the portion of code I used locally to fix the problem. Please apply if it seems appropriate:

      final class PropertyReflectionUtils {
          private PropertyReflectionUtils() {}
      
          private static final String IS_PREFIX = "is";
          private static final String GET_PREFIX = "get";
          private static final String SET_PREFIX = "set";
      
          static boolean isGetter(final Method method) {
              if (method.getParameterCount() > 0)
                  return false;
      
              if (isPropertyAccessor(method, GET_PREFIX) || isPropertyAccessor(method, SET_PREFIX))
                  return true;
      
              return false;
          }
      
          static boolean isSetter(final Method method) {
              if (method.getParameterCount() != 1)
                  return false;
      
              if (isPropertyAccessor(method, SET_PREFIX))
                  return true;
      
              return false;
          }
      
          static boolean isPropertyAccessor(Method method, String prefix) {
              String name = method.getName();
              return name.length() > prefix.length()
                  && name.startsWith(prefix)
                  && (Character.isUpperCase(name.charAt(prefix.length()))
                      || method.isAnnotationPresent(BsonId.class)
                      || method.isAnnotationPresent(BsonProperty.class));
          }
      
      

       

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            xtracoder@gmail.com Xtra Coder
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: