Fix default case in Edit list processing 74/115874/29
authorPeter Suna <peter.suna@pantheon.tech>
Tue, 11 Mar 2025 18:43:44 +0000 (19:43 +0100)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 6 May 2025 11:10:11 +0000 (11:10 +0000)
If an unknown JSON element is present, it can break our JSON parsing
logic, and it will fail with an IllegalStateException.

Fix unhandled default case in processing Edit list in Yang Patch
request. Throw an exception if any unknown field is present.

JIRA: NETCONF-1438
Change-Id: I2f46ce0ce8903e4dcc818702ef83bb04a5e4aa31
Signed-off-by: Peter Suna <peter.suna@pantheon.tech>
protocol/restconf-server-api/src/main/java/org/opendaylight/restconf/server/api/JsonPatchBody.java
protocol/restconf-server-jaxrs/src/test/java/org/opendaylight/restconf/server/jaxrs/NC1438Test.java

index c7e0460647e2543a29503ec8d8836149eabc58ef..66413c6584e60fdc29a5dd8f63f344863f03d0d6 100644 (file)
@@ -167,9 +167,8 @@ public final class JsonPatchBody extends PatchBody {
                         edit.setData(readEditData(in, edit.getTargetSchemaNode(), codecs));
                     }
                 }
-                default -> {
-                    // FIXME: this does not look right, as it can wreck our logic
-                }
+                default -> throw new RequestException(ErrorType.APPLICATION, ErrorTag.UNKNOWN_ELEMENT,
+                    "Provided unknown element '" + editDefinition + "'");
             }
         }
 
index 50cda91892dbbd65cb53c1286f76b2dd5c81700c..914f9a070033ce547bd83c000f3cfdeeff391830 100644 (file)
@@ -522,6 +522,43 @@ class NC1438Test extends AbstractRestconfTest {
             }""", body::formatToJSON, true);
     }
 
+    @Test
+    void testPatchWrongEditSchemaNode() {
+        final var body = assert400PatchError(ar -> restconf.dataYangJsonPATCH(stringInputStream("""
+            {
+              "ietf-yang-patch:yang-patch" : {
+                "patch-id" : "test patch id",
+                "edit" : [
+                  {
+                    "wrong" : "create data",
+                    "operation" : "create",
+                    "target" : "/example-jukebox:jukebox",
+                    "value" : {
+                      "jukebox" : {
+                        "player" : {
+                          "gap" : "0.2"
+                        }
+                      }
+                    }
+                  }
+                ]
+              }
+            }"""), uriInfo, sc, ar));
+
+        assertFormat("""
+            {
+              "errors": {
+                "error": [
+                  {
+                    "error-tag": "unknown-element",
+                    "error-message": "Provided unknown element 'wrong'",
+                    "error-type": "application"
+                  }
+                ]
+              }
+            }""", body::formatToJSON, true);
+    }
+
     private static YangErrorsBody assert400PatchError(final Consumer<AsyncResponse> invocation) {
         return assertInstanceOf(YangErrorsBody.class, assertFormattableBody(400, invocation));
     }