bhardwajrahul20 closed pull request #1: Sling-7346-Json response converting 
response property value to string even if it is json object
URL: https://github.com/apache/sling-org-apache-sling-servlets-post/pull/1
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/sling/servlets/post/JSONResponse.java 
b/src/main/java/org/apache/sling/servlets/post/JSONResponse.java
index e65e90c..dcf9c72 100644
--- a/src/main/java/org/apache/sling/servlets/post/JSONResponse.java
+++ b/src/main/java/org/apache/sling/servlets/post/JSONResponse.java
@@ -24,6 +24,7 @@
 import javax.json.JsonObjectBuilder;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -103,9 +104,14 @@ JsonObject getJson() {
         JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
         for (Map.Entry<String, Object> entry : json.entrySet()) {
             if (entry.getValue() != null) {
-                jsonBuilder.add(entry.getKey(), entry.getValue().toString());
-            }
-            else {
+                //Check for org.apache.sling.commons.json.JSONObject for 
backward compatibility
+                
if(entry.getValue().getClass().getName().equals("org.apache.sling.commons.json.JSONObject")
 || entry.getValue() instanceof JsonObject) {
+                    jsonBuilder.add(entry.getKey(),
+                            Json.createReader(new 
StringReader(entry.getValue().toString())).readObject());
+                } else {
+                    jsonBuilder.add(entry.getKey(), 
entry.getValue().toString());
+                }
+            } else {
                 jsonBuilder.addNull(entry.getKey());
             }
         }
diff --git a/src/test/java/org/apache/sling/servlets/post/JsonResponseTest.java 
b/src/test/java/org/apache/sling/servlets/post/JsonResponseTest.java
index d591628..1be0f29 100644
--- a/src/test/java/org/apache/sling/servlets/post/JsonResponseTest.java
+++ b/src/test/java/org/apache/sling/servlets/post/JsonResponseTest.java
@@ -115,11 +115,28 @@ public void testNoChangesOnError() throws Exception {
         assertEquals(0, obj.getJsonArray("changes").size());
     }
 
+    public void testSendWithJsonAsPropertyValue() throws Exception {
+        String testResponseJson = 
"{\"user\":\"testUser\",\"properties\":{\"id\":\"testId\", \"name\":\"test\"}}";
+        JsonObject customProperty = Json.createReader(new 
StringReader(testResponseJson)).readObject();
+        res.setProperty("response", customProperty);
+        MockResponseWithHeader response = new MockResponseWithHeader();
+        res.send(response, true);
+        JsonObject result = Json.createReader(new 
StringReader(response.getOutput().toString())).readObject();
+        assertProperty(result, "response", customProperty);
+    }
+
     private static JsonValue assertProperty(JsonObject obj, String key) {
         assertTrue("JSON object does not have property " + key, 
obj.containsKey(key));
         return obj.get(key);
     }
 
+    @SuppressWarnings({"unchecked"})
+    private static JsonObject assertProperty(JsonObject obj, String key, 
JsonObject expected) {
+        JsonObject res = (JsonObject) assertProperty(obj, key);
+        assertEquals(expected, res);
+        return res;
+    }
+
     @SuppressWarnings({"unchecked"})
     private static JsonString assertProperty(JsonObject obj, String key, 
String expected) {
         JsonString res = (JsonString) assertProperty(obj, key);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to