knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kris...@apache.org
Subject knox git commit: KNOX-1602 - JsonFilterReader should handle strings, numbers, booleans, and null at root
Date Thu, 15 Nov 2018 13:49:08 GMT
Repository: knox
Updated Branches:
  refs/heads/v1.2.0 5be3f1a64 -> 634783426


KNOX-1602 - JsonFilterReader should handle strings, numbers, booleans, and null at root

Signed-off-by: Kevin Risden <krisden@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/63478342
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/63478342
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/63478342

Branch: refs/heads/v1.2.0
Commit: 634783426356ee2d2629466d614bb4db996101f4
Parents: 5be3f1a
Author: Kevin Risden <krisden@apache.org>
Authored: Wed Nov 14 17:26:32 2018 -0500
Committer: Kevin Risden <krisden@apache.org>
Committed: Thu Nov 15 08:48:58 2018 -0500

----------------------------------------------------------------------
 .../rewrite/impl/json/JsonFilterReader.java     | 16 ++++++++
 .../rewrite/impl/json/JsonFilterReaderTest.java | 42 ++++++++++++++++++++
 2 files changed, 58 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/63478342/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReader.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReader.java
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReader.java
index 2e47a58..ecc7925 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReader.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReader.java
@@ -305,6 +305,10 @@ class JsonFilterReader extends Reader {
     Level child;
     Level parent;
     String value = null;
+    if(stack.isEmpty()) {
+      generator.writeString( parser.getText() );
+      return;
+    }
     parent = stack.peek();
     if( parent.isArray() ) {
       ArrayNode array = (ArrayNode)parent.node;
@@ -337,6 +341,10 @@ class JsonFilterReader extends Reader {
   private void processValueNumber() throws IOException {
     Level child;
     Level parent;
+    if(stack.isEmpty()) {
+      processedUnbufferedValueNumber();
+      return;
+    }
     parent = stack.peek();
     if( parent.isArray() ) {
       if( bufferingLevel != null ) {
@@ -430,6 +438,10 @@ class JsonFilterReader extends Reader {
   private void processValueBoolean() throws IOException {
     Level child;
     Level parent;
+    if(stack.isEmpty()) {
+      generator.writeBoolean(parser.getBooleanValue());
+      return;
+    }
     parent = stack.peek();
     if( parent.isArray() ) {
       ((ArrayNode)parent.node ).add( parser.getBooleanValue() );
@@ -453,6 +465,10 @@ class JsonFilterReader extends Reader {
 
   private void processValueNull() throws IOException {
     Level child;
+    if(stack.isEmpty()) {
+      generator.writeNull();
+      return;
+    }
     Level parent = stack.peek();
     if( parent.isArray() ) {
       ((ArrayNode)parent.node ).addNull();

http://git-wip-us.apache.org/repos/asf/knox/blob/63478342/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
index b92363a..25488a0 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
@@ -33,6 +33,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -56,6 +59,45 @@ public class JsonFilterReaderTest {
     assertThat( output, containsString( "\"startedTime\":1399975176760}" ) );
   }
 
+  @Test
+  public void testString() throws IOException {
+    String inputJson = "\"abc\"";
+    StringReader inputReader = new StringReader( inputJson );
+    JsonFilterReader filterReader = new TestJsonFilterReader( inputReader, null );
+    String outputJson = new String( IOUtils.toCharArray( filterReader ) );
+    JsonAssert.with( outputJson ).assertThat( "$", is( "abc" ) );
+  }
+
+  @Test
+  public void testNumber() throws IOException {
+    int num = new Random().nextInt();
+    String inputJson = String.valueOf(num);
+    StringReader inputReader = new StringReader( inputJson );
+    JsonFilterReader filterReader = new TestJsonFilterReader( inputReader, null );
+    String outputJson = new String( IOUtils.toCharArray( filterReader ) );
+    JsonAssert.with( outputJson ).assertThat( "$", is( num ) );
+  }
+
+  @Test
+  public void testBoolean() throws IOException {
+    List<Boolean> booleans = Arrays.asList(true, false);
+    for(boolean bool : booleans) {
+      String inputJson = String.valueOf(bool);
+      StringReader inputReader = new StringReader(inputJson);
+      JsonFilterReader filterReader = new TestJsonFilterReader(inputReader, null);
+      String outputJson = new String(IOUtils.toCharArray(filterReader));
+      JsonAssert.with(outputJson).assertThat("$", is(bool));
+    }
+  }
+
+  @Test
+  public void testNull() throws IOException {
+    String inputJson = "null";
+    StringReader inputReader = new StringReader( inputJson );
+    JsonFilterReader filterReader = new TestJsonFilterReader( inputReader, null );
+    String outputJson = new String( IOUtils.toCharArray( filterReader ) );
+    assertThat(inputJson, is(outputJson));
+  }
 
   @Test
   public void testSimple() throws IOException {


Mime
View raw message