title: Handle java.util.Date param in Spring MongoRepository @Query
body: <p>I wrote custom json query for querying data from MongDB:</p>
  <pre><code>public interface LogMessageRepository extends MongoRepository&lt;LogMessage, String&gt; {
  
      @Query(&quot;{ $and: [&quot;
              + &quot;{ $or: [{ $where: ':#{#traceId} == null'}, {'properties.headers.X-B3-TraceId' : :#{#traceId}}]}&quot;
              + &quot;{ $or: [{ $where: ':#{#mappedId} == null'}, {'properties.headers.hash-on' : :#{#mappedId}}]}&quot;
              + &quot;{ $or: [{$or: [{$where: ':#{#startFrom} == null'}, {$where: ':#{#startTo} == null'}]}, {'createTime' : {$gte: {$date: :#{#startFrom}}, $lte: {$date: :#{#startTo}}}}]}&quot;
              + &quot;]}&quot;)
      Page&lt;LogMessage&gt; findByFilter(@Param(&quot;traceId&quot;) String traceId,
              @Param(&quot;mappedId&quot;) String mappedId,
              @Param(&quot;startFrom&quot;) Date startFrom,
              @Param(&quot;startTo&quot;) Date startTo,
              Pageable pageRequest);
  
  }
  </code></pre>
  <p>The pupose here is to support optional params on DB request level.
  Everything works fine until i start testing method call with java.util.Date params.
  here is the test:</p>
  <pre><code>    @Test
  //    @Disabled
      void should_findDocumentByDateInterval() {
          Date from = Date.from(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault())
                  .minusDays(4L).atZone(ZoneId.systemDefault()).toInstant());
          Date to = Date.from(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault())
                  .minusDays(2L).atZone(ZoneId.systemDefault()).toInstant());
          final Page&lt;LogMessage&gt; actual = subject.findByFilter(null, null,
                  from, to, PageRequest.of(0, 3));
  
          assertThat(actual.getContent().get(0)).isEqualTo(savedLogMessage);
          assertThat(actual.getContent().size()).isEqualTo(1);
      }
  </code></pre>
  <p>In debug i see that with Date params everything is ok.
  But i receive Exception on test execution:</p>
  <pre><code>
  Failed to parse string 'java.lang.Object@685783b1' as a date
  org.bson.json.JsonParseException: Failed to parse string 'java.lang.Object@685783b1' as a date
      at app//org.springframework.data.mongodb.util.json.ParameterBindingJsonReader.visitDateTimeExtendedJson(ParameterBindingJsonReader.java:1306)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingJsonReader.visitExtendedJSON(ParameterBindingJsonReader.java:854)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingJsonReader.readBsonType(ParameterBindingJsonReader.java:211)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:227)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:66)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:360)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:229)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:66)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:360)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readList(ParameterBindingDocumentCodec.java:368)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:346)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:229)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:66)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:360)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readList(ParameterBindingDocumentCodec.java:368)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:346)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:229)
      at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.captureExpressionDependencies(ParameterBindingDocumentCodec.java:202)
      at app//org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.getBindingContext(StringBasedMongoQuery.java:136)
      at app//org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.createQuery(StringBasedMongoQuery.java:121)
      at app//org.springframework.data.mongodb.repository.query.AbstractMongoQuery.doExecute(AbstractMongoQuery.java:122)
      at app//org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:107)
      at app//org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
      at app//org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
      at app//org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:159)
      at app//org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138)
      at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at app//org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
      at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at app//org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
      at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at app//org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
      at app/jdk.proxy3/jdk.proxy3.$Proxy86.findByFilter(Unknown Source)
      at app//com.atlas.feed.logs.repository.LogMessageRepositoryTest.should_findDocumentByDateInterval(LogMessageRepositoryTest.java:91)
      at java.base@17.0.1/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base@17.0.1/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
      at java.base@17.0.1/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base@17.0.1/java.lang.reflect.Method.invoke(Method.java:568)
      at app//org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
      at app//org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
      at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
      at app//org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
      at app//org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
      at app//org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
      at app//org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
      at app//org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
      at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
      at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
      at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
      at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
      at app//org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
      at app//org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
      at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
      at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
      at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
      at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
      at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
      at app//org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
      at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
      at java.base@17.0.1/java.util.ArrayList.forEach(ArrayList.java:1511)
      at app//org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
      at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
      at app//org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
      at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
      at java.base@17.0.1/java.util.ArrayList.forEach(ArrayList.java:1511)
      at app//org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
      at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
      at app//org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
      at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
      at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
      at app//org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
      at app//org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
      at app//org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
      at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99)
      at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
      at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
      at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
      at java.base@17.0.1/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base@17.0.1/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
      at java.base@17.0.1/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base@17.0.1/java.lang.reflect.Method.invoke(Method.java:568)
      at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
      at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
      at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
      at jdk.proxy2/jdk.proxy2.$Proxy5.stop(Unknown Source)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker$3.run(TestWorker.java:193)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
      at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
      at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
      at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
      at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
      at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
      Suppressed: org.bson.BsonInvalidOperationException: readStartDocument can only be called when State is VALUE, not when State is END_OF_DOCUMENT.
          at app//org.bson.AbstractBsonReader.throwInvalidState(AbstractBsonReader.java:668)
          at app//org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:686)
          at app//org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:721)
          at app//org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:449)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:223)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:66)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:360)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:235)
          ... 113 more
      Suppressed: org.bson.BsonInvalidOperationException: readStartDocument can only be called when State is VALUE, not when State is END_OF_DOCUMENT.
          at app//org.bson.AbstractBsonReader.throwInvalidState(AbstractBsonReader.java:668)
          at app//org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:686)
          at app//org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:721)
          at app//org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:449)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:223)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:66)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:360)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:235)
          ... 110 more
      Suppressed: org.bson.BsonInvalidOperationException: readStartDocument can only be called when State is VALUE, not when State is END_OF_DOCUMENT.
          at app//org.bson.AbstractBsonReader.throwInvalidState(AbstractBsonReader.java:668)
          at app//org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:686)
          at app//org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:721)
          at app//org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:449)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:223)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:66)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:360)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:235)
          ... 105 more
      Suppressed: org.bson.BsonInvalidOperationException: readStartDocument can only be called when State is VALUE, not when State is END_OF_DOCUMENT.
          at app//org.bson.AbstractBsonReader.throwInvalidState(AbstractBsonReader.java:668)
          at app//org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:686)
          at app//org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:721)
          at app//org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:449)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:223)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:66)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:360)
          at app//org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:235)
          ... 100 more
  Caused by: java.lang.IllegalArgumentException: Text 'java.lang.Object@685783b1' could not be parsed at index 0
      at org.springframework.data.mongodb.util.json.DateTimeFormatter$Java8DateTimeFormatter.parse(DateTimeFormatter.java:161)
      at org.springframework.data.mongodb.util.json.DateTimeFormatter.parse(DateTimeFormatter.java:80)
      at org.springframework.data.mongodb.util.json.ParameterBindingJsonReader.visitDateTimeExtendedJson(ParameterBindingJsonReader.java:1304)
      ... 116 more
  
  </code></pre>
  <p>Please help.</p>