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

Can't find codec during insertOne on a collection with a document class and codec registry

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 3.0.2
    • Codecs
    • None

    Description

      I have a CodecProvider, attached to a CodecRegistry which gets applied to a MongoClient. The MongoClient is used to create a collection, and the type "MobilePhone" is passed when returning that collection and the codecRegistry is re-applied to the collection itself. When doing an insert using a MobilePhone record I get the below stack trace.

      Looking through the source a bit it looks like RequestMessage will always only ever have the codec's defined in BsonValueCodecProvider. At line 46 it creates a static instance, using fromProviders and nothing makes reference to the current registry defined on MongoClient, or the Collection.

      This bug is resulting in being unable to continue implementation and we are on a tight deadline. I have other collections using this pattern to read objects just fine, but not for insert. So I dont know if its more widespread.

      Here are some snippets of my implementation itself.

      MongoClient

      CodecProvider domainCodecProivder = new CodecProvider() {
                  @SuppressWarnings("unchecked")
                  @Override
                  public <T> Codec<T> get(Class<T> type, CodecRegistry registry) {
                      LOGGER.debug("Looking for codec for {}",type);
                      if( type == Date.class) {
                          return (Codec<T>) new DateCodec();
                      }
                      .... some other codec's.
       
                      if (type == Location.class) {
                          return (Codec<T>) new LocationCodec();
                      }
                      if (type == SMSMessage.class) {
                          return (Codec<T>) new SMSMessageCodec(registry);
                      }
                      if (type == MobilePhone.class) {
                          return (Codec<T>) new MobilePhoneCodec(registry);
                      }
                      LOGGER.debug("Nothing found for {}",type);
                      return null;
                  }
              };
       
              codecRegistry = CodecRegistries.fromRegistries(
                  MongoClient.getDefaultCodecRegistry(),
                  CodecRegistries.fromProviders(domainCodecProivder)
              );
              
              mongoClient = new MongoClient(
                  Arrays.asList(
                      new ServerAddress("server.address", port),
                      new ServerAddress("server.address", port)
                  ), 
                  Arrays.asList(MongoCredential.createCredential("username", "messaging", superSecretPassword.toCharArray())),
                  MongoClientOptions.builder()
                      .codecRegistry(codecRegistry)
                      .readPreference(ReadPreference.nearest())
                      .connectionsPerHost(50)
                      .build()
                  
              );
      

      Getting an instance of the collection.

      public <T> MongoCollection<T> getCollection(String databaseName, String collectionName, Class<T> type) {
              return getClient().getDatabase(databaseName).getCollection(collectionName,type).withCodecRegistry(codecRegistry);
          }
      

      Point of failure, insertOne

      List<SMSMessage> smsLogs = new ArrayList<SMSMessage>();
                  smsLogs.add(smsMessage);
                  
                  MobilePhone newMobilePhone= new MobilePhone();
                  newMobilePhone.setPhoneNumber(phoneNumber);
                  newMobilePhone.setSendingNumber(sendingNumber);
                  newMobilePhone.setSmsLog(smsLogs);
                  
                  mobilePhoneCollection.insertOne(newMobilePhone);
      

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            matt.filion Matt Filion
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: