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

org.bson.types.StringRangeSet.contains() throws NumberFormatException when input is not number

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.5.0
    • Affects Version/s: 3.0.0
    • Component/s: BSON
    • Labels:
      None

      When the DBObject.keySet() rerturns a Set<String> that is an instance of StringRangeSet, performing keySet.contains("FOO") on it will throw NumberFormatException.
      Caused due to the contains implementation that assume input is a valid number:

       @Override
          public boolean contains(final Object o) {
              if (!(o instanceof String)) {
                  return false;
              }
              int i = Integer.parseInt((String) o);
              return i >= 0 && i < size();
          }
      

      Using a non-number contains can be a valid use case when recursively iterating through a DBObject, trying to remove all "_class" fields that spring-data-mongo generate:

      private void removeClassFields(DBObject dbObject){
              Set<String> keys = dbObject.keySet();
              if (keys.contains("_class")){
                  dbObject.removeField("_class");
              }
              for (String key : keys) {
                  if (dbObject.get(key) instanceof DBObject){
                      removeClassFields((DBObject) dbObject.get(key));
                  }
              }
          }
      

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            yonatang Yonatan Graber
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: