Skip to content

Commit 44a6bd1

Browse files
committed
Signed-off-by: Lukas Jungmann <[email protected]>
1 parent 5d7704d commit 44a6bd1

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

impl/src/main/java/org/glassfish/json/JsonMessages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ static String PATCH_MEMBER_MISSING(String operation, String member) {
257257
return localize("patch.member.missing", operation, member);
258258
}
259259

260+
static String PATCH_OPERATION_MISSING() {
261+
return localize("patch.operation.missing");
262+
}
263+
260264

261265
private static String localize(String key, Object ... args) {
262266
try {

impl/src/main/java/org/glassfish/json/JsonPatchImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ private JsonStructure apply(JsonStructure target, JsonObject operation) {
150150

151151
JsonPointer pointer = getPointer(operation, "path");
152152
JsonPointer from;
153-
switch (Operation.fromOperationName(operation.getString("op"))) {
153+
JsonString op = operation.getJsonString("op");
154+
if (op == null) {
155+
throw new JsonException(JsonMessages.PATCH_OPERATION_MISSING());
156+
}
157+
switch (Operation.fromOperationName(op.getString())) {
154158
case ADD:
155159
return pointer.add(target, getValue(operation));
156160
case REPLACE:

impl/src/main/resources/org/glassfish/json/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ patch.move.target.null=The ''{0}'' path of the patch operation ''move'' does not
8787
patch.test.failed=The JSON Patch operation ''test'' failed for path ''{0}'' and value ''{1}''
8888
patch.illegal.operation=Illegal value for the op member of the JSON Patch operation: ''{0}''
8989
patch.member.missing=The JSON Patch operation ''{0}'' must contain a ''{1}'' member
90+
patch.operation.missing=The JSON Patch must contain ''op'' member

impl/src/test/java/org/glassfish/json/tests/JsonPatchBugsTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
import jakarta.json.JsonArray;
2121
import jakarta.json.JsonException;
2222
import jakarta.json.JsonPatch;
23+
import jakarta.json.JsonReader;
24+
import jakarta.json.JsonStructure;
25+
26+
import java.io.StringReader;
27+
2328
import org.junit.Test;
2429

2530
/**
@@ -42,4 +47,33 @@ public void applyThrowsJsonException() {
4247
.build();
4348
JsonArray result = patch.apply(array);
4449
}
50+
51+
// https://github.com/eclipse-ee4j/jsonp/issues/181
52+
@Test(expected = JsonException.class)
53+
public void applyThrowsJsonException2() {
54+
// JSON document to be patched
55+
String targetDocument
56+
= "{\n"
57+
+ " \"firstName\": \"John\",\n"
58+
+ " \"lastName\": \"Doe\"\n"
59+
+ "}";
60+
61+
// JSON Patch document
62+
// Instead of "op", we have "op_", which is invalid
63+
String patchDocument
64+
= "[\n"
65+
+ " { \"op_\": \"replace\", \"path\": \"/firstName\", \"value\": \"Jane\" }\n"
66+
+ "]";
67+
68+
try (JsonReader objectReader = Json.createReader(new StringReader(targetDocument));
69+
JsonReader arrayReader = Json.createReader(new StringReader(patchDocument))) {
70+
71+
JsonStructure target = objectReader.read();
72+
JsonPatch patch = Json.createPatch(arrayReader.readArray());
73+
74+
// Applies the patch
75+
// It will throw a NullPointerException with no message
76+
JsonStructure patched = patch.apply(target);
77+
}
78+
}
4579
}

0 commit comments

Comments
 (0)