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

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Minor - P4
    • 3.5.0
    • Affects Version/s: 3.0.0
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • 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:
              Jeffrey Yemin
              Reporter:
              Yonatan Graber
              None
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: