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

Byte array encoding/decoding missmatch

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • Codecs
    • None

    Description

      The following test fails with AssertionError: expected:<class [B> but was:<class org.bson.types.Binary>

          @Test
          public void testByteArray() {
              MongoClient mongo = new MongoClient();
              MongoDatabase db = mongo.getDatabase("mydb");
              MongoCollection<Document> collection = db.getCollection("test");
              try {
                  collection.insertOne(new Document("image", "MongoDB".getBytes()));
                  assertEquals(byte[].class, collection.find().first().get("image").getClass());
              } finally {
                  collection.drop();
                  mongo.close();
              }
          }
      

      It appears, byte array is encoded properly with ByteArrayCodec, but is decoded to BsonBinary, by BinaryCodec.

      Probably BinaryCodec should be obsoleted, and removed from ValueCodecProvider.addCodecs, and BsonTypeClassMap.addDefault() should have map.put(BsonType.BINARY, byte[].class) instead of map.put(BsonType.BINARY, Binary.class)

      Attachments

        Activity

          People

            ross@mongodb.com Ross Lawley
            andriy.zhdanov Andriy Zhdanov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: