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

Custom MongoConverters not loaded when using @Query Annotations

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 3.9.1
    • Component/s: Codecs
    • Labels:
      None
    • Environment:
      Ubuntu 18.10

      When trying to use @Query Annotation on a mongoRepo, the custom codecs are not loaded, however when using normal findBy.., the codecs are correctly loaded.

      Code for my Repo :

      //@Repository public interface OrderRepo extends MongoRepository<Order, String> { @Query(value = "{ 'createdAt' : {$gte : ?0, $lte: ?1 }}") ArrayList<Order> findbyCreatedAtBetween(ZonedDateTime from, ZonedDateTime to); }
      

      Code for Codecs :

      // public class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
      
          @Override
          public Date convert(ZonedDateTime zonedDateTime) {
      
              if (zonedDateTime != null)
                  return Date.from(zonedDateTime.toInstant());
      
              else
                  return null;
      
      
          }
      }
      
      public class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> { @Override public ZonedDateTime convert(Date date) { if (date != null) return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); else return null; } }
      

      Mongo Configuration :

       

      @Configuration
      public class SpringMongoConfiguration {
      
      
          @Bean
          public MongoCustomConversions mongoCustomConversions() {
              List<Converter<?, ?>> converters = new ArrayList<>();
      
              converters.add(new ZonedDateTimeToDateConverter());
      
              converters.add(new DateToZonedDateTimeConverter());
      
              return new MongoCustomConversions(converters);
          }
      
      
      }
      

      It's also worth saying that I tried changing the query to

      String> { @Query(value = "{ 'createdAt' : {$gt : ?0, $le: ?1 }}")
      
      
      

      Which according to the mongo documentation is the query executed when using findByFieldBetween(), however it does not seem to be able to load the codecs, while deleting the @Query annotation does lead to the codecs being loaded.

       

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            sherifneamatalla Sherif Neamatalla
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: