From: Sangwook Ha Date: Fri, 6 May 2022 17:14:04 +0000 (-0700) Subject: Fix PATCH request issue with top-level container as target X-Git-Tag: v4.0.0~73 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=a304aa0a7aeb0063624764b30a1501e0e5a9cf9f;hp=ad67f040475b5a037e1d9fafd2b5c7d5d5c11a3d;p=netconf.git Fix PATCH request issue with top-level container as target PATCH request to the data root with a top-level container as target does not work because JsonPatchBodyReader and XmlPatchBodyReader expect a parent statement in the stack. Address the issue by setting target schema node to the path schema context when stack is empty and add test cases for JsonPatchBodyReader and XmlPatchBodyReader. JIRA: NETCONF-877 Change-Id: Ibb65ccdf3f03d8a9a9023d75bf288e5e4416ca50 Signed-off-by: Sangwook Ha --- diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReader.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReader.java index c2886b4de7..c581243ab3 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReader.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReader.java @@ -239,8 +239,10 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader { stack.exit(); } - final EffectiveStatement parentStmt = stack.currentStatement(); - verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt); + if (!stack.isEmpty()) { + final EffectiveStatement parentStmt = stack.currentStatement(); + verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt); + } edit.setTargetSchemaNode(stack.toInference()); } diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java index f6138d109c..109c6a77f0 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java @@ -124,9 +124,13 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader { stack.exit(); } - final EffectiveStatement parentStmt = stack.currentStatement(); - verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt); - targetNode = (SchemaNode) parentStmt; + if (stack.isEmpty()) { + targetNode = pathContext.getSchemaContext(); + } else { + final EffectiveStatement parentStmt = stack.currentStatement(); + verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt); + targetNode = (SchemaNode) parentStmt; + } } if (targetNode == null) { diff --git a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java index 91a409d763..d9f42d9978 100644 --- a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java +++ b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java @@ -162,4 +162,19 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest { final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream); checkPatchContext(returnValue); } + + /** + * Test of Yang Patch on the top-level container with empty URI for data root. + */ + @Test + public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception { + final String uri = ""; + mockBodyReader(uri, jsonToPatchBodyReader, false); + + final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream( + "/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json"); + + final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream); + checkPatchContext(returnValue); + } } diff --git a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java index 60571fa66a..96e16d71eb 100644 --- a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java +++ b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java @@ -116,4 +116,15 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest { .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml"); checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream)); } + + /** + * Test of Yang Patch on the top-level container with empty URI for data root. + */ + @Test + public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception { + mockBodyReader("", xmlToPatchBodyReader, false); + final InputStream inputStream = XmlBodyReaderTest.class + .getResourceAsStream("/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml"); + checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream)); + } } diff --git a/restconf/restconf-nb-rfc8040/src/test/resources/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json b/restconf/restconf-nb-rfc8040/src/test/resources/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json new file mode 100644 index 0000000000..31bc148c44 --- /dev/null +++ b/restconf/restconf-nb-rfc8040/src/test/resources/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json @@ -0,0 +1,22 @@ +{ + "ietf-yang-patch:yang-patch" : { + "patch-id" : "test-patch", + "comment" : "Test patch applied to the top-level container with empty URI", + "edit" : [ + { + "edit-id": "edit1", + "operation": "replace", + "target": "/instance-identifier-patch-module:patch-cont", + "value": { + "patch-cont": { + "my-list1": [ + { + "name": "my-leaf10" + } + ] + } + } + } + ] + } +} diff --git a/restconf/restconf-nb-rfc8040/src/test/resources/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml b/restconf/restconf-nb-rfc8040/src/test/resources/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml new file mode 100644 index 0000000000..11b619dcf5 --- /dev/null +++ b/restconf/restconf-nb-rfc8040/src/test/resources/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml @@ -0,0 +1,16 @@ + + test-patch + Test patch applied to the top-level container with empty URI + + edit1 + replace + /instance-identifier-patch-module:patch-cont + + + + my-leaf10 + + + + +