[ 
https://issues.apache.org/jira/browse/AVRO-2276?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16703573#comment-16703573
 ] 

ASF GitHub Bot commented on AVRO-2276:
--------------------------------------

dkulp closed pull request #396: AVRO-2276: Escape Map keys in 
GenericData.toString to generate JSON
URL: https://github.com/apache/avro/pull/396
 
 
   

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/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java 
b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
index 714f4e8b4..6dffa15c5 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
@@ -541,7 +541,7 @@ protected void toString(Object datum, StringBuilder buffer, 
IdentityHashMap<Obje
       Map<Object,Object> map = (Map<Object,Object>)datum;
       for (Map.Entry<Object,Object> entry : map.entrySet()) {
         buffer.append("\"");
-        buffer.append(String.valueOf(entry.getKey()));
+        writeEscapedString(String.valueOf(entry.getKey()), buffer);
         buffer.append("\": ");
         toString(entry.getValue(), buffer, seenObjects);
         if (++count < map.size())
diff --git 
a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java 
b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
index 0a98c7aab..fe8f61818 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
@@ -323,26 +323,26 @@ public void testMapWithNonStringKeyToStringIsJson() 
throws Exception {
     Schema decMapSchema = new Schema.Parser().parse("{\"type\": \"map\", 
\"values\": \"string\", \"java-key-class\" : \"java.math.BigDecimal\"}");
     Field decMapField = new Field("decMap", Schema.createMap(decMapSchema), 
null, null);
     Schema boolMapSchema = new Schema.Parser().parse("{\"type\": \"map\", 
\"values\": \"string\", \"java-key-class\" : \"java.lang.Boolean\"}");
-    Field boolMapField = new Field("boolMap", Schema.createMap(decMapSchema), 
null, null);
+    Field boolMapField = new Field("boolMap", Schema.createMap(boolMapSchema), 
null, null);
     Schema fileMapSchema = new Schema.Parser().parse("{\"type\": \"map\", 
\"values\": \"string\", \"java-key-class\" : \"java.io.File\"}");
-    Field fileMapField = new Field("fileMap", Schema.createMap(decMapSchema), 
null, null);
+    Field fileMapField = new Field("fileMap", Schema.createMap(fileMapSchema), 
null, null);
     Schema schema = Schema.createRecord("my_record", "doc", "mytest", false);
     
schema.setFields(Arrays.asList(intMapField,decMapField,boolMapField,fileMapField));
 
-    HashMap<Integer, String> intPair =  new HashMap<>();
+    HashMap<Integer, String> intPair = new HashMap<>();
     intPair.put(1, "one");
     intPair.put(2, "two");
 
-    HashMap<java.math.BigDecimal, String> decPair =  new HashMap<>();
+    HashMap<java.math.BigDecimal, String> decPair = new HashMap<>();
     decPair.put(java.math.BigDecimal.valueOf(1), "one");
     decPair.put(java.math.BigDecimal.valueOf(2), "two");
 
-    HashMap<Boolean, String> boolPair =  new HashMap<>();
+    HashMap<Boolean, String> boolPair = new HashMap<>();
     boolPair.put(true, "isTrue");
     boolPair.put(false, "isFalse");
     boolPair.put(null, null);
 
-    HashMap<java.io.File, String> filePair =  new HashMap<>();
+    HashMap<java.io.File, String> filePair = new HashMap<>();
     java.io.File f = new java.io.File( 
getClass().getResource("/SchemaBuilder.avsc").toURI() );
     filePair.put(f, "File");
 
@@ -368,6 +368,13 @@ public void testMapWithNonStringKeyToStringIsJson() throws 
Exception {
     assertEquals("{\"bytes\": \"a\\nb\"}", data.toString(bytes));
   }
 
+  @Test public void testToStringEscapesControlCharsInMap() {
+    GenericData data = GenericData.get();
+    Map<String, String> m = new HashMap<>();
+    m.put("a\n\\b", "a\n\\b");
+    assertEquals("{\"a\\n\\\\b\": \"a\\n\\\\b\"}", data.toString(m));
+  }
+
   @Test public void testToStringFixed() throws Exception {
     GenericData data = GenericData.get();
     assertEquals("[97, 10, 98]", data.toString(new GenericData.Fixed(


 

----------------------------------------------------------------
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]


> GenericData.toString does not always generate valid JSON for Map datum
> ----------------------------------------------------------------------
>
>                 Key: AVRO-2276
>                 URL: https://issues.apache.org/jira/browse/AVRO-2276
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.9.0
>            Reporter: Ismaël Mejía
>            Assignee: Ismaël Mejía
>            Priority: Minor
>
> Avro represents data as json internally so it requires to escape the keys of 
> the objects (Maps) as mandated by https://tools.ietf.org/html/rfc8259
> I discover this while running a build on windows because of '\' characters. 
> But it can be easily reproduced on linux creating a file/dir with backspaces.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to