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

RecordCodec fails to decode into a component of type java.util.Set

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.6.0
    • Component/s: Codecs
    • None

      Summary

      I have a record that looks like this:

       

      package com.github.bottomlessarchive.loa.document.repository.domain;
      
      import org.bson.codecs.pojo.annotations.BsonId;
      
      import java.time.Instant;
      import java.util.Collections;
      import java.util.Set;
      import java.util.UUID;
      
      public record DocumentDatabaseEntity(
      
              @BsonId
              UUID id,
      
              String vault,
              String type,
              String status,
              String compression,
      
              String source,
              Set<byte[]> sourceLocations,
      
              byte[] checksum,
              long fileSize,
      
              int downloaderVersion,
              Instant downloadDate
      ) {
      
      
          public Set<byte[]> sourceLocations() {
              // Older documents doesn't have sourceLocations populated
              return sourceLocations == null ? Collections.emptySet() : sourceLocations;
          }
      }

      The record is successfully inserted into the database, however, when I want to read it back, I get a java.lang.ClassCastException:

       

       

      java.lang.IllegalStateException: Failed to execute CommandLineRunner
          at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:780) ~[spring-boot-2.6.7.jar:2.6.7]
          at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:761) ~[spring-boot-2.6.7.jar:2.6.7]
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-2.6.7.jar:2.6.7]
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.7.jar:2.6.7]
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.7.jar:2.6.7]
          at com.github.bottomlessarchive.loa.indexer.LibraryIndexerApplication.main(LibraryIndexerApplication.java:13) ~[main/:na]
      Caused by: java.lang.IllegalArgumentException: argument type mismatch
          at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:70) ~[na:na]
          at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
          at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483) ~[na:na]
          at org.bson.codecs.record.RecordCodec.decode(RecordCodec.java:136) ~[bson-record-codec-4.6.0.jar:na]
          at org.bson.codecs.record.RecordCodec.decode(RecordCodec.java:48) ~[bson-record-codec-4.6.0.jar:na]
          at com.mongodb.internal.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60) ~[mongodb-driver-core-4.6.0.jar:na]
          at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:87) ~[bson-4.6.0.jar:na]
          at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:42) ~[bson-4.6.0.jar:na]
          at org.bson.internal.LazyCodec.decode(LazyCodec.java:48) ~[bson-4.6.0.jar:na]
          at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:104) ~[bson-4.6.0.jar:na]
          at com.mongodb.internal.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63) ~[mongodb-driver-core-4.6.0.jar:na]
          at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:87) ~[bson-4.6.0.jar:na]
          at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:42) ~[bson-4.6.0.jar:na]
          at com.mongodb.internal.connection.ReplyMessage.<init>(ReplyMessage.java:51) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.InternalStreamConnection.getCommandResult(InternalStreamConnection.java:535) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:420) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:337) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:116) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:644) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:240) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:226) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:126) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:116) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.connection.DefaultServer$OperationCountTrackingConnection.command(DefaultServer.java:345) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.CommandOperationHelper.createReadCommandAndExecute(CommandOperationHelper.java:232) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.FindOperation.lambda$execute$1(FindOperation.java:695) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$2(OperationHelper.java:575) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:600) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$3(OperationHelper.java:574) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:600) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.OperationHelper.withSourceAndConnection(OperationHelper.java:573) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.FindOperation.lambda$execute$2(FindOperation.java:690) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.async.function.RetryingSyncSupplier.get(RetryingSyncSupplier.java:65) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:722) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:86) ~[mongodb-driver-core-4.6.0.jar:na]
          at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:191) ~[mongodb-driver-sync-4.6.0.jar:na]
          at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135) ~[mongodb-driver-sync-4.6.0.jar:na]
          at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92) ~[mongodb-driver-sync-4.6.0.jar:na]
          at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:39) ~[mongodb-driver-sync-4.6.0.jar:na]
          at java.base/java.lang.Iterable.spliterator(Iterable.java:101) ~[na:na]
          at com.github.bottomlessarchive.loa.document.service.entity.factory.DocumentEntityFactory.getDocumentEntity(DocumentEntityFactory.java:57) ~[main/:na]
          at com.github.bottomlessarchive.loa.indexer.command.IndexerCommand.run(IndexerCommand.java:33) ~[main/:na]
          at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:777) ~[spring-boot-2.6.7.jar:2.6.7]
          ... 5 common frames omitted
      Caused by: java.lang.ClassCastException: Cannot cast java.util.ArrayList to java.util.Set
          at java.base/java.lang.Class.cast(Class.java:3921) ~[na:na]
          at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67) ~[na:na]
          ... 49 common frames omitted
      

       

      I'm in the process of migrating the existing code to use records. The previously used codec (PojoCodec by default?) supported this functionality.

      How to Reproduce

      • Have a record with a variable that has Set as its type.
      • Insert a record into the database.
      • Try to read the inserted record back in the application.

            Assignee:
            Unassigned Unassigned
            Reporter:
            laxika91@gmail.com Gyula Lakatos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: