You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@kafka.apache.org by "José Armando García Sancio (Jira)" <ji...@apache.org> on 2023/03/08 17:15:00 UTC

[jira] [Created] (KAFKA-14794) Unable to deserialize base64 JSON strings

José Armando García Sancio created KAFKA-14794:
--------------------------------------------------

             Summary: Unable to deserialize base64 JSON strings 
                 Key: KAFKA-14794
                 URL: https://issues.apache.org/jira/browse/KAFKA-14794
             Project: Kafka
          Issue Type: Bug
            Reporter: José Armando García Sancio
            Assignee: José Armando García Sancio


h1. Problem

The following test fails:
{code:java}
@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);
}
{code}
with the following error:
{code:java}
 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)
{code}
The reason for the failure is because FasterXML Jackson deserializes base64 JSON strings to a TextNode not to a BinaryNode.
h1. 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{}}}.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)