@@ -447,4 +447,58 @@ public void treeToValueWithMissingNode4932() throws Exception {
447447 assertNull (r .treeToValue (MAPPER .nullNode (), Object .class ));
448448 assertNull (r .treeToValue (MAPPER .missingNode (), Object .class ));
449449 }
450+
451+ // [databind#6059]: `byte[]` should convert to `BinaryNode`, not `POJONode`
452+ @ Test
453+ public void byteArrayToTree6059 () {
454+ final byte [] bytes = { 1 , 2 , 3 };
455+ JsonNode node = MAPPER .valueToTree (bytes );
456+
457+ // Must be a `BinaryNode` (not `POJONode`, as it was prior to fix)...
458+ assertInstanceOf (BinaryNode .class , node );
459+ assertTrue (node .isBinary ());
460+ // ... with contents preserved...
461+ assertArrayEquals (bytes , node .binaryValue ());
462+
463+ // ... and, the actual regression reported, equal to a node built
464+ // from a separate-but-equal array (`BinaryNode` compares by content,
465+ // `POJONode` only by identity):
466+ JsonNode node2 = MAPPER .valueToTree (new byte [] { 1 , 2 , 3 });
467+ assertEquals (node , node2 );
468+
469+ // Empty array, too (from the original report):
470+ assertEquals (MAPPER .valueToTree (new byte [0 ]),
471+ MAPPER .valueToTree (new byte [0 ]));
472+ }
473+
474+ // [databind#6059]: also when `byte[]` is nested within Object/Array contexts
475+ @ Test
476+ public void nestedByteArrayToTree6059 () {
477+ final byte [] bytes = { 4 , 5 , 6 };
478+
479+ // As a value within an Object (Map):
480+ JsonNode objNode = MAPPER .valueToTree (Collections .singletonMap ("b" , bytes ));
481+ JsonNode objBinary = objNode .get ("b" );
482+ assertInstanceOf (BinaryNode .class , objBinary );
483+ assertArrayEquals (bytes , objBinary .binaryValue ());
484+
485+ // As an element within an Array (List):
486+ JsonNode arrNode = MAPPER .valueToTree (Collections .singletonList (bytes ));
487+ JsonNode arrBinary = arrNode .get (0 );
488+ assertInstanceOf (BinaryNode .class , arrBinary );
489+ assertArrayEquals (bytes , arrBinary .binaryValue ());
490+ }
491+
492+ // [databind#6059]: also when `byte[]` reaches the tree via `TokenBuffer`
493+ // replay (`writeEmbeddedObject()` path), not just the direct `writeBinary()`
494+ @ Test
495+ public void byteArrayViaTokenBufferToTree6059 () {
496+ final byte [] bytes = { 7 , 8 , 9 };
497+ TokenBuffer buf = TokenBuffer .forGeneration ();
498+ buf .writeBinary (bytes );
499+
500+ JsonNode node = MAPPER .valueToTree (buf );
501+ assertInstanceOf (BinaryNode .class , node );
502+ assertArrayEquals (bytes , node .binaryValue ());
503+ }
450504}
0 commit comments