Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/java/tools/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1363,13 +1363,13 @@ public <T extends JsonNode> 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);
Expand All @@ -1383,13 +1383,13 @@ public <T extends JsonNode> 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);
Expand All @@ -1403,13 +1403,13 @@ public <T extends JsonNode> 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));
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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]
Expand Down