Skip to content

Commit 7e261a6

Browse files
dylanBehetrem0mus
authored andcommitted
Bug 169 : Operation path encoding as required by RFC6901 (#170)
Signed-off-by: dbehetre <[email protected]>
1 parent 5cb006b commit 7e261a6

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -224,14 +224,14 @@ private void diff(String path, JsonValue source, JsonValue target) {
224224
private void diffObject(String path, JsonObject source, JsonObject target) {
225225
source.forEach((key, value) -> {
226226
if (target.containsKey(key)) {
227-
diff(path + '/' + key, value, target.get(key));
227+
diff(path + '/' + Json.encodePointer(key), value, target.get(key));
228228
} else {
229-
builder.remove(path + '/' + key);
229+
builder.remove(path + '/' + Json.encodePointer(key));
230230
}
231231
});
232232
target.forEach((key, value) -> {
233233
if (! source.containsKey(key)) {
234-
builder.add(path + '/' + key, value);
234+
builder.add(path + '/' + Json.encodePointer(key), value);
235235
}
236236
});
237237
}

tests/src/test/java/org/glassfish/json/tests/JsonPatchDiffTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -50,8 +50,8 @@ public class JsonPatchDiffTest {
5050

5151
@Parameters(name = "{index}: ({0})={1}")
5252
public static Iterable<Object[]> data() throws Exception {
53-
List<Object[]> examples = new ArrayList<Object[]>();
54-
JsonArray data = loadData();
53+
List<Object[]> examples = new ArrayList<>();
54+
JsonArray data = JsonPatchDiffTest.loadData();
5555
for (JsonValue jsonValue : data) {
5656
JsonObject test = (JsonObject) jsonValue;
5757
Object[] testData = new Object[4];
@@ -78,8 +78,12 @@ private static Class<? extends Exception> createExceptionClass(
7878
private static JsonArray loadData() {
7979
InputStream testData = JsonPatchTest.class
8080
.getResourceAsStream("/jsonpatchdiff.json");
81-
JsonReader reader = Json.createReader(testData);
82-
JsonArray data = (JsonArray) reader.read();
81+
82+
JsonArray data;
83+
try(JsonReader reader = Json.createReader(testData)){
84+
data = (JsonArray) reader.read();
85+
}
86+
8387
return data;
8488
}
8589

tests/src/test/resources/jsonpatchdiff.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,14 @@
156156
{ "path" : "/1", "value" : 1, "op" : "add" },
157157
{ "value" : 3, "op" : "add", "path" : "/3" }
158158
]
159+
},
160+
{
161+
"original": {"a/b": "c", "e/f": "i"},
162+
"target": {"a/b": "d", "f/g": "i"},
163+
"expected": [
164+
{ "op": "replace", "path": "/a~1b", "value": "d" },
165+
{ "op": "remove", "path":"/e~1f" },
166+
{ "op": "add", "path":"/f~1g", "value":"i" }
167+
]
159168
}
160169
]

0 commit comments

Comments
 (0)