viktorsomogyi commented on a change in pull request #4090: URL: https://github.com/apache/kafka/pull/4090#discussion_r492704280
########## File path: core/src/test/scala/unit/kafka/utils/JsonTest.scala ########## @@ -40,25 +40,34 @@ class JsonTest { def testJsonParse(): Unit = { val jnf = JsonNodeFactory.instance - assertEquals(Json.parseFull("{}"), Some(JsonValue(new ObjectNode(jnf)))) + assertEquals(Some(JsonValue(new ObjectNode(jnf))), Json.parseFull("{}")) + assertEquals(Right(JsonValue(new ObjectNode(jnf))), Json.tryParseFull("{}")) + assertThrows(classOf[IllegalArgumentException], () => Json.tryParseFull(null)) + assertThrows(classOf[IllegalArgumentException], () => Json.tryParseBytes(null)) - assertEquals(Json.parseFull("""{"foo":"bar"s}"""), None) + assertEquals(Option(MissingNode.getInstance()).map(JsonValue(_)), Json.parseFull("")) + assertEquals(Right(MissingNode.getInstance()).map(JsonValue(_)), Json.tryParseFull("")) Review comment: You're right, it makes sense to return `None` and `Left` in those cases. Will change it and upload it to this PR. ########## File path: core/src/main/scala/kafka/utils/Json.scala ########## @@ -69,8 +70,11 @@ object Json { * @return An `Either` which in case of `Left` means an exception and `Right` is the actual return value. */ def tryParseFull(input: String): Either[JsonProcessingException, JsonValue] = - try Right(mapper.readTree(input)).map(JsonValue(_)) - catch { case e: JsonProcessingException => Left(e) } + if (input != null && input.isEmpty) Review comment: Wasn't sure what to do here but `null` is checked in `readTree` and an IllegalArgumentException is thrown so I decided to go skip here and leave the original behavior. ########## File path: core/src/main/scala/kafka/utils/Json.scala ########## @@ -69,8 +70,11 @@ object Json { * @return An `Either` which in case of `Left` means an exception and `Right` is the actual return value. */ def tryParseFull(input: String): Either[JsonProcessingException, JsonValue] = - try Right(mapper.readTree(input)).map(JsonValue(_)) - catch { case e: JsonProcessingException => Left(e) } + if (input != null && input.isEmpty) Review comment: Wasn't sure what to do here but `null` is checked in `readTree` and an IllegalArgumentException is thrown so I decided to skip here and leave the original behavior. ########## File path: core/src/main/scala/kafka/utils/Json.scala ########## @@ -69,8 +70,11 @@ object Json { * @return An `Either` which in case of `Left` means an exception and `Right` is the actual return value. */ def tryParseFull(input: String): Either[JsonProcessingException, JsonValue] = - try Right(mapper.readTree(input)).map(JsonValue(_)) - catch { case e: JsonProcessingException => Left(e) } + if (input != null && input.isEmpty) + Left(new JsonParseException(MissingNode.getInstance().traverse(), "The input string shouldn't be empty")) Review comment: It seems like that the most adequate JsonProcessingException is JsonParseException that could apply here. Another possibility is to change the exception in return type to something like IOException and throw that here but it seems it's more vague than throwing JsonParseException. Another option is to simple throw IllegalArgumentException here instead of Left. That would correspond to the `readTree(InputStream)` as well. What do you think? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org