Bug 7987: Json HTTP PATCH: Problem parsing simple leaf value
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / rest / impl / JsonToPATCHBodyReader.java
index 75240378cb99c6e174ac99523a6d670310c23ba1..5b169885bcd1819026bd9aab7eef028b4f3acac9 100644 (file)
@@ -52,7 +52,7 @@ import org.slf4j.LoggerFactory;
 
 /**
  * @deprecated This class will be replaced by
- *             {@link org.opendaylight.restconf.utils.patch.JsonToPATCHBodyReader}
+ *             {@link org.opendaylight.restconf.jersey.providers.JsonToPATCHBodyReader}
  */
 @Deprecated
 @Provider
@@ -273,7 +273,11 @@ public class JsonToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider
             value.append("[");
 
             while (in.hasNext()) {
-                readValueObject(value, in);
+                if (in.peek() == JsonToken.STRING) {
+                    value.append("\"" + in.nextString() + "\"");
+                } else {
+                    readValueObject(value, in);
+                }
                 if (in.peek() != JsonToken.END_ARRAY) {
                     value.append(",");
                 }
@@ -296,6 +300,12 @@ public class JsonToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider
      * @throws IOException
      */
     private void readValueObject(@Nonnull final StringBuffer value, @Nonnull final JsonReader in) throws IOException {
+        // read simple leaf value
+        if (in.peek() == JsonToken.STRING) {
+            value.append("\"" + in.nextString() + "\"");
+            return;
+        }
+
         in.beginObject();
         value.append("{");
 
@@ -311,7 +321,11 @@ public class JsonToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider
                     value.append("[");
 
                     while (in.hasNext()) {
-                        readValueObject(value, in);
+                        if (in.peek() == JsonToken.STRING) {
+                            value.append("\"" + in.nextString() + "\"");
+                        } else {
+                            readValueObject(value, in);
+                        }
                         if (in.peek() != JsonToken.END_ARRAY) {
                             value.append(",");
                         }
@@ -338,7 +352,7 @@ public class JsonToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider
      * @param in reader JsonReader reader
      * @return NormalizedNode representing data
      */
-    private NormalizedNode readEditData(@Nonnull final JsonReader in, @Nonnull final SchemaNode targetSchemaNode,
+    private static NormalizedNode readEditData(@Nonnull final JsonReader in, @Nonnull final SchemaNode targetSchemaNode,
                                         @Nonnull final InstanceIdentifierContext path) {
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
@@ -352,7 +366,7 @@ public class JsonToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider
      * @param edit Instance of PatchEdit
      * @return PATCHEntity
      */
-    private PATCHEntity prepareEditOperation(@Nonnull final PatchEdit edit) {
+    private static PATCHEntity prepareEditOperation(@Nonnull final PatchEdit edit) {
         if ((edit.getOperation() != null) && (edit.getTargetSchemaNode() != null)
                 && checkDataPresence(edit.getOperation(), (edit.getData() != null))) {
             if (isPatchOperationWithValue(edit.getOperation())) {
@@ -380,19 +394,11 @@ public class JsonToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider
      * @return true if data is present when operation requires it or if there are no data when operation does not
      * allow it, false otherwise
      */
-    private boolean checkDataPresence(@Nonnull final String operation, final boolean hasData) {
+    private static boolean checkDataPresence(@Nonnull final String operation, final boolean hasData) {
         if (isPatchOperationWithValue(operation)) {
-            if (hasData) {
-                return true;
-            } else {
-                return false;
-            }
+            return hasData;
         } else  {
-            if (!hasData) {
-                return true;
-            } else {
-                return false;
-            }
+            return !hasData;
         }
     }