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

Observable<T> toFuture() implicit returns a future with a null value

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Scala

      Background

      While Integrating with the scala mongo driver we discovered https://github.com/mongodb/mongo-java-driver/blob/r4.3.3/driver-scala/src/main/scala/org/mongodb/scala/Observable.scala#L366-L376

      the .head method hides a null underneath, as this is not documented nor is it defended against codebases can become vulnerable to null pointer exceptions or invalid responses in an API service, the fallback is to make use of .toFutureOption however as the risk isn't documented teams may only discover this when they hit it. 

      If this is undefended against the code above continues to pass, Until someone tries to access something in the desired object. 

      Solutions

      1. ** Document the behaviour and offer more guidance in handling 

      2. Throw NoSuchElementException instead of null.asInstanceOf

      3. Create a custom Exception to indicate what the null pointer means (the query returned nothing.)

       

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            daniel.connelly@hotmail.co.uk Daniel Connelly
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: