Skip to content

Commit c05a405

Browse files
committed
fixed handling of arrays
1 parent f3f3c7a commit c05a405

9 files changed

Lines changed: 86 additions & 30 deletions

File tree

src/main/java/com/webfuzzing/overlayjvm/OverlayJVM.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,25 @@ private static void applyUpdate(ONode selection, ONode update) {
166166
return;
167167
}
168168

169-
for(ONode node : selection.getArray()){
170-
applyUpdate(node, update);
169+
if(selection.isArray()) {
170+
/*
171+
currently, in snackjson there is no clear way to distinguish between a selected array
172+
and an array of results, apart from checking the parent
173+
*/
174+
ONode parent = selection.parent();
175+
if(parent == null) {
176+
// newly created array containing result nodes
177+
for (ONode node : selection.getArray()) {
178+
applyUpdate(node, update);
179+
}
180+
} else {
181+
//the target is an array itself
182+
if(update.isArray()) {
183+
selection.addAll(update.getArrayUnsafe());
184+
} else {
185+
selection.add(update);
186+
}
187+
}
171188
}
172189
}
173190

src/test/java/com/webfuzzing/overlayjvm/OverlayJVM_V1_0_0_oas_overlay_java_Test.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ public void testInvalidAction() {
4747
public static Stream<Data> overlayProvider() {
4848

4949
return Stream.of(
50-
new Data("/openapi/town.yaml", "/overlays/remove-properties.yaml", "/expected/town-remove-properties.yaml"),
51-
new Data("/openapi/petstore.yaml", "/overlays/overlay.yaml", "/expected/output1.yaml"),
52-
new Data("/openapi/town.yaml", "/overlays/building-description.yaml", "/expected/town-building-description.yaml"),
53-
new Data("/openapi/town.yaml", "/overlays/update-root.yaml", "/expected/town-root-updated.yaml"),
54-
new Data("/openapi/town.yaml", "/overlays/remove-example.yaml", "/expected/town-remove-example.yaml"),
55-
new Data("/openapi/town.yaml", "/overlays/remove-descriptions.yaml", "/expected/town-remove-descriptions.yaml"),
56-
new Data("/openapi/openapi-with-servers.yaml", "/overlays/remove-server.yaml", "/expected/one-less-server.yaml"),
50+
new Data("/openapi/town.yaml", "/overlays/remove-properties.yaml", "/expected/town-remove-properties.yaml",0),
51+
new Data("/openapi/petstore.yaml", "/overlays/overlay.yaml", "/expected/output1.yaml",0),
52+
new Data("/openapi/town.yaml", "/overlays/building-description.yaml", "/expected/town-building-description.yaml",0),
53+
new Data("/openapi/town.yaml", "/overlays/update-root.yaml", "/expected/town-root-updated.yaml",0),
54+
new Data("/openapi/town.yaml", "/overlays/remove-example.yaml", "/expected/town-remove-example.yaml",0),
55+
new Data("/openapi/town.yaml", "/overlays/remove-descriptions.yaml", "/expected/town-remove-descriptions.yaml",0),
56+
new Data("/openapi/openapi-with-servers.yaml", "/overlays/remove-server.yaml", "/expected/one-less-server.yaml",0),
5757
//this seems was wrong... you are allowed to modify info.version
58-
new Data("/openapi/immutable.yaml", "/overlays/immutable.yaml", "/expected/immutable.yaml"),
59-
new Data("/openapi/responses.yaml", "/overlays/remove-responses.yaml", "/expected/remove-responses.yaml"),
58+
new Data("/openapi/immutable.yaml", "/overlays/immutable.yaml", "/expected/immutable.yaml",0),
59+
new Data("/openapi/responses.yaml", "/overlays/remove-responses.yaml", "/expected/remove-responses.yaml",0),
6060
//this seems was wrong... invalid RFC 9535 path
61-
new Data("/openapi/traits.yaml", "/overlays/traits.yaml", "/expected/traits.yaml")
61+
new Data("/openapi/traits.yaml", "/overlays/traits.yaml", "/expected/traits.yaml",0)
6262
);
6363
}
6464

src/test/java/com/webfuzzing/overlayjvm/OverlayJVM_V1_1_0_custom_Test.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public class OverlayJVM_V1_1_0_custom_Test extends ProcessorTestBase {
1414
public static Stream<Data> overlayProvider() {
1515

1616
return Stream.of(
17-
getDataFromName("query-examples")
17+
getDataFromName("query-examples", 0),
18+
getDataFromName("array",2)
1819
);
1920
}
2021

@@ -28,7 +29,14 @@ public void testOverlay(ProcessorTestBase.Data data) throws Exception {
2829
@Test
2930
public void testLibrarySupport() throws Exception {
3031

31-
String json = readResource("custom/array/array.json");
32+
String json = "{\n" +
33+
" \"a\": [{\"x\": 42}, {\"x\": 777}],\n" +
34+
" \"b\": [],\n" +
35+
" \"c\": {\"y\": 1},\n" +
36+
" \"d\": {\"y\": 2},\n" +
37+
" \"e\": null\n" +
38+
"}";
39+
3240
assertNotNull(json);
3341

3442
ONode schema = ONode.ofJson(json);

src/test/java/com/webfuzzing/overlayjvm/OverlayJVM_V1_1_0_specs_copy_Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class OverlayJVM_V1_1_0_specs_copy_Test extends ProcessorTestBase {
1616
public static Stream<Data> overlayProvider() {
1717

1818
return Stream.of(
19-
new Data("/simple/simple-openapi.yaml", "/simple/simple-overlay.yaml", "/simple/simple-result.yaml"),
20-
new Data("/exist/exist-openapi.yaml", "/exist/exist-overlay.yaml", "/exist/exist-result.yaml"),
21-
new Data("/move/move-openapi.yaml", "/move/move-overlay.yaml", "/move/move-result.yaml")
19+
new Data("/simple/simple-openapi.yaml", "/simple/simple-overlay.yaml", "/simple/simple-result.yaml",0),
20+
new Data("/exist/exist-openapi.yaml", "/exist/exist-overlay.yaml", "/exist/exist-result.yaml",0),
21+
new Data("/move/move-openapi.yaml", "/move/move-overlay.yaml", "/move/move-result.yaml",0)
2222
);
2323
}
2424

src/test/java/com/webfuzzing/overlayjvm/ProcessorTestBase.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.skyscreamer.jsonassert.JSONAssert;
44
import org.skyscreamer.jsonassert.JSONCompareMode;
55

6-
import java.io.IOException;
76
import java.nio.file.Files;
87
import java.nio.file.Path;
98
import java.nio.file.Paths;
@@ -27,23 +26,24 @@ protected void verifyOverlay(Data data, String base) throws Exception {
2726
TransformationResult tr = OverlayJVM.applyOverlay(openApi, overlay);
2827
String result = FormatUtils.normalizeJson(tr.transformedSchema);
2928

29+
assertEquals(data.warningsCount, tr.warnings.size(), "Expected " + data.warningsCount + "warning:\n"+String.join("\n", tr.warnings));
3030

3131
//this was failing on field name order :(
3232
//assertEquals(expectedResult, result);
3333
JSONAssert.assertEquals(expectedResult, result, JSONCompareMode.LENIENT);
34-
35-
assertFalse(tr.hasWarnings(), String.join("\n", tr.warnings));
3634
}
3735

3836
public static class Data {
3937
public final String openApi;
4038
public final String overlay;
4139
public final String expected;
40+
public final int warningsCount;
4241

43-
public Data(String openApi, String overlay, String expected) {
42+
public Data(String openApi, String overlay, String expected, int warningsCount) {
4443
this.openApi = openApi;
4544
this.overlay = overlay;
4645
this.expected = expected;
46+
this.warningsCount = warningsCount;
4747
}
4848

4949
@Override
@@ -52,15 +52,17 @@ public String toString() {
5252
"overlay='" + overlay + '\'' +
5353
", openApi='" + openApi + '\'' +
5454
", expected='" + expected + '\'' +
55+
", warningsCount=" + warningsCount +
5556
'}';
5657
}
5758
}
5859

59-
public static Data getDataFromName(String name){
60+
public static Data getDataFromName(String name, int warningsCount) {
6061
return new Data(
6162
"/"+name+"/"+name+"-openapi.yaml",
6263
"/"+name+"/"+name+"-overlay.yaml",
63-
"/"+name+"/"+name+"-result.yaml"
64+
"/"+name+"/"+name+"-result.yaml",
65+
warningsCount
6466
);
6567
}
6668
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Technically, not an openapi schema, but library should still work
2+
a:
3+
- x: 42
4+
- x: 777
5+
b: [9]
6+
c:
7+
y: 1
8+
d:
9+
y: 2
10+
e: null
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
overlay: 1.1.0
2+
info:
3+
title: Verify different operations on arrays
4+
version: 1.0.0
5+
actions:
6+
- target: "$.a[?@.y]" # no results
7+
remove: true
8+
- target: "$.b" # array
9+
update: [1,2,3]
10+
- target: "$.b" # already modified array
11+
update: 8
12+
- target: "$.*.y" # should return objects in c and d
13+
update: 7
14+
- target: "$.e" # it is null
15+
remove: true
16+
- target: "$.f" # it is undefined
17+
update:
18+
f: 9
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
a:
2+
- x: 42
3+
- x: 777
4+
b: [9,1,2,3,8]
5+
c:
6+
y: 7
7+
d:
8+
y: 7

src/test/resources/custom/array/array.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)