Uploaded image for project: 'Kafka'
  1. Kafka
  2. KAFKA-14794

Unable to deserialize base64 JSON strings

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 3.5.0
    • None
    • None

    Description

      Problem

      The following test fails:

      @Test
      public void testBinaryNode() throws IOException {
          byte[] expected = new byte[] {5, 2, 9, 4, 1, 8, 7, 0, 3, 6};
          StringWriter writer = new StringWriter();
          ObjectMapper mapper = new ObjectMapper();
      
          mapper.writeTree(mapper.createGenerator(writer), new BinaryNode(expected));
      
          JsonNode binaryNode = mapper.readTree(writer.toString());
      
          assertTrue(binaryNode.isTextual(), binaryNode.toString());
          byte[] actual = MessageUtil.jsonNodeToBinary(binaryNode, "Test base64 JSON string");
          assertEquals(expected, actual);
      }
      

      with the following error:

       Gradle Test Run :clients:test > Gradle Test Executor 20 > MessageUtilTest > testBinaryNode() FAILED
          java.lang.RuntimeException: Test base64 JSON string: expected Base64-encoded binary data.
              at org.apache.kafka.common.protocol.MessageUtil.jsonNodeToBinary(MessageUtil.java:165)
              at org.apache.kafka.common.protocol.MessageUtilTest.testBinaryNode(MessageUtilTest.java:102)
      

      The reason for the failure is because FasterXML Jackson deserializes base64 JSON strings to a TextNode not to a BinaryNode.

      Solution

      The method MessageUtil::jsonNodeToBinary should not assume that the input JsonNode is always a BinaryNode. It should also support decoding TextNode.

      JsonNode::binaryValue is supported by both BinaryNode and TextNode.

      Attachments

        Issue Links

          Activity

            People

              jsancio José Armando García Sancio
              jsancio José Armando García Sancio
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: