Do not suppress module name for top-level elements 59/79559/1
authorTomas Cere <tomas.cere@pantheon.tech>
Thu, 10 Jan 2019 14:25:45 +0000 (15:25 +0100)
committerJakub Morvay <jakub.morvay@gmail.com>
Wed, 16 Jan 2019 08:59:33 +0000 (08:59 +0000)
RFC8040 states that top-level elements should start off fully
qualified -- hence we should disregard initial namespace when
for them.

JIRA: NETCONF-497
Change-Id: I1e638957510fe2efa8a42a430fb4c2ded48082ec
Signed-off-by: Tomas Cere <tomas.cere@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 562b1c7b11119b8dd4676082628f2c64a14d735f)

restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/NormalizedNodeJsonBodyWriter.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/JSONRestconfServiceRfc8040ImplTest.java

index 59a976743cb9ee1c5f0b35f5a76af63f0c7965a0..de5d7cd2eb51b478b655a4df3e83970d82a52179 100644 (file)
@@ -151,21 +151,24 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
         final SchemaNode schema = context.getSchemaNode();
         final JSONCodecFactory codecs = getCodecFactory(context);
 
-        final URI initialNs;
-        if (schema instanceof DataSchemaNode
-                && !((DataSchemaNode)schema).isAugmenting()
-                && !(schema instanceof SchemaContext)) {
-            initialNs = schema.getQName().getNamespace();
-        } else if (schema instanceof RpcDefinition) {
-            initialNs = schema.getQName().getNamespace();
-        } else {
-            initialNs = null;
-        }
         final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(
-                codecs, path, initialNs, jsonWriter);
+                codecs, path, initialNamespaceFor(schema, depth), jsonWriter);
+
         return ParameterAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth, fields);
     }
 
+    private static URI initialNamespaceFor(final SchemaNode schema, final Integer depth) {
+        if (schema instanceof RpcDefinition) {
+            return schema.getQName().getNamespace();
+        }
+        // For top-level elements we always want to use namespace prefix, hence use a null initial namespace
+        if (depth == null || depth == 0 || schema instanceof SchemaContext) {
+            return null;
+        }
+        return schema instanceof DataSchemaNode && !((DataSchemaNode)schema).isAugmenting()
+                ? schema.getQName().getNamespace() : null;
+    }
+
     private static JsonWriter createJsonWriter(final OutputStream entityStream, final boolean prettyPrint) {
         if (prettyPrint) {
             return JsonWriterFactory.createJsonWriter(
index 7e82357cf53c4a6c3d48fb98081ed7c81a372ff8..730ea064e89ebb445815e2f7dee3f89c61589a48 100644 (file)
@@ -557,6 +557,8 @@ public class JSONRestconfServiceRfc8040ImplTest {
         final String jsonResp = optionalResp.get();
 
         assertNotNull("Returned null response", jsonResp);
+        assertThat("Top level module has incorrect format", jsonResp, containsString("\"ietf-interfaces:interface\""));
+        assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
         assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
         assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
         assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));