From 6ee027c1de13bc343fcdf6c5f18c57d00c24b41b Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Thu, 20 Nov 2025 00:45:21 +0900 Subject: [PATCH] Fix JavaDoc and test --- .../java/tools/jackson/databind/JsonNode.java | 16 +++--- .../databind/node/RequiredAccessorTest.java | 52 ++++++++++++++++++- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/main/java/tools/jackson/databind/JsonNode.java b/src/main/java/tools/jackson/databind/JsonNode.java index 6ad0613ab4..d0d5c42770 100644 --- a/src/main/java/tools/jackson/databind/JsonNode.java +++ b/src/main/java/tools/jackson/databind/JsonNode.java @@ -1363,13 +1363,13 @@ public T requireNonNull() { * JSON Object value) and has value for specified property with key {@code propertyName} * (but note that value may be explicit JSON null value). * If this node is Object Node and has value for specified property, matching value - * is returned; otherwise {@link IllegalArgumentException} is thrown. + * is returned; otherwise {@link JsonNodeException} is thrown. * * @param propertyName Name of property to access * * @return Value of the specified property of this Object node * - * @throws IllegalArgumentException if this node is not an Object node or if it does not + * @throws JsonNodeException if this node is not an Object node or if it does not * have value for specified property */ public abstract JsonNode required(String propertyName); @@ -1383,13 +1383,13 @@ public T requireNonNull() { * JSON Array value) and has value for specified {@code index} * (but note that value may be explicit JSON null value). * If this node is Array Node and has value for specified index, value at index - * is returned; otherwise {@link IllegalArgumentException} is thrown. + * is returned; otherwise {@link JsonNodeException} is thrown. * * @param index Index of the value of this Array node to access * * @return Value at specified index of this Array node * - * @throws IllegalArgumentException if this node is not an Array node or if it does not + * @throws JsonNodeException if this node is not an Array node or if it does not * have value for specified index */ public abstract JsonNode required(int index); @@ -1403,13 +1403,13 @@ public T requireNonNull() { * starting from {@code this} node * (but note that value may be explicit JSON null value). * If such value node exists it is returned; - * otherwise {@link IllegalArgumentException} is thrown. + * otherwise {@link JsonNodeException} is thrown. * * @param pathExpr {@link JsonPointer} expression (as String) to use for finding value node * * @return Matching value node for given expression * - * @throws IllegalArgumentException if no value node exists at given {@code JSON Pointer} path + * @throws JsonNodeException if no value node exists at given {@code JSON Pointer} path */ public JsonNode requiredAt(String pathExpr) { return requiredAt(JsonPointer.compile(pathExpr)); @@ -1424,13 +1424,13 @@ public JsonNode requiredAt(String pathExpr) { * starting from {@code this} node * (but note that value may be explicit JSON null value). * If such value node exists it is returned; - * otherwise {@link IllegalArgumentException} is thrown. + * otherwise {@link JsonNodeException} is thrown. * * @param path {@link JsonPointer} expression to use for finding value node * * @return Matching value node for given expression * - * @throws IllegalArgumentException if no value node exists at given {@code JSON Pointer} path + * @throws JsonNodeException if no value node exists at given {@code JSON Pointer} path */ public final JsonNode requiredAt(final JsonPointer path) { JsonPointer currentExpr = path; diff --git a/src/test/java/tools/jackson/databind/node/RequiredAccessorTest.java b/src/test/java/tools/jackson/databind/node/RequiredAccessorTest.java index 32c69db017..e3e7115e16 100644 --- a/src/test/java/tools/jackson/databind/node/RequiredAccessorTest.java +++ b/src/test/java/tools/jackson/databind/node/RequiredAccessorTest.java @@ -1,9 +1,11 @@ package tools.jackson.databind.node; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import tools.jackson.core.JsonPointer; import tools.jackson.databind.*; +import tools.jackson.databind.exc.JsonNodeException; import tools.jackson.databind.testutil.DatabindTestUtil; import static org.junit.jupiter.api.Assertions.*; @@ -97,21 +99,67 @@ public void testSimpleRequireOk() throws Exception { } } + @Test + public void testSimpleRequireAtFailure() throws Exception { + try { + TEST_OBJECT.requiredAt("/some-random-path"); + fail("Should not pass"); + } catch (JsonNodeException e) { + verifyException(e, "No node at"); + } + try { + TEST_ARRAY.requiredAt("/some-random-path"); + fail("Should not pass"); + } catch (JsonNodeException e) { + verifyException(e, "No node at"); + } + try { + TEST_OBJECT.requiredAt(JsonPointer.compile("/some-random-path")); + fail("Should not pass"); + } catch (JsonNodeException e) { + verifyException(e, "No node at"); + } + try { + TEST_ARRAY.requiredAt(JsonPointer.compile("/some-random-path")); + fail("Should not pass"); + } catch (JsonNodeException e) { + verifyException(e, "No node at"); + } + } + @Test public void testSimpleRequireFail() throws Exception { + // required(String) try { TEST_OBJECT.required("bogus"); fail("Should not pass"); - } catch (DatabindException e) { + } catch (JsonNodeException e) { verifyException(e, "No value for property 'bogus'"); } + // required(String) try { TEST_ARRAY.required("bogus"); fail("Should not pass"); - } catch (DatabindException e) { + } catch (JsonNodeException e) { verifyException(e, "Node of type `tools.jackson.databind.node.ArrayNode` has no fields"); } + + // required(int) + try { + TEST_OBJECT.required(-1); + fail("Should not pass"); + } catch (JsonNodeException e) { + verifyException(e, "has no indexed values"); + } + + // required(int) + try { + TEST_ARRAY.required(-1); + fail("Should not pass"); + } catch (JsonNodeException e) { + verifyException(e, "No value at index #"); + } } // [databind#3005]