[jira] [Commented] (AVRO-2216) GenericData toString generates invalid JSON when map keys are not Strings
[ https://issues.apache.org/jira/browse/AVRO-2216?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16680011#comment-16680011 ] ASF GitHub Bot commented on AVRO-2216: -- dkulp closed pull request #330: AVRO-2216 Force use of String as Map keys URL: https://github.com/apache/avro/pull/330 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 8b8a2961f..bc453a8d1 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 @@ -540,8 +540,8 @@ protected void toString(Object datum, StringBuilder buffer, IdentityHashMap map = (Map)datum; for (Map.Entry entry : map.entrySet()) { -toString(entry.getKey(), buffer, seenObjects); -buffer.append(": "); +buffer.append("\"" + String.valueOf(entry.getKey()) ); +buffer.append("\": "); toString(entry.getValue(), buffer, seenObjects); if (++count < map.size()) buffer.append(", "); 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 3a81169bb..983c0932a 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 @@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; @@ -324,6 +325,51 @@ public void testToStringIsJson() throws JsonParseException, IOException { mapper.readTree(parser); } + @Test + public void testMapWithNonStringKeyToStringIsJson() throws JsonParseException, IOException, URISyntaxException { +Schema intMapSchema = new Schema.Parser().parse("{\"type\": \"map\", \"values\": \"string\", \"java-key-class\" : \"java.lang.Integer\"}"); +Field intMapField = new Field("intMap", Schema.createMap(intMapSchema), null, null); +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); +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); +Schema schema = Schema.createRecord("my_record", "doc", "mytest", false); + schema.setFields(Arrays.asList(intMapField,decMapField,boolMapField,fileMapField)); + +HashMap intPair = new HashMap<>(); +intPair.put(1, "one"); +intPair.put(2, "two"); + +HashMap decPair = new HashMap<>(); +decPair.put(java.math.BigDecimal.valueOf(1), "one"); +decPair.put(java.math.BigDecimal.valueOf(2), "two"); + +HashMap boolPair = new HashMap<>(); +boolPair.put(true, "isTrue"); +boolPair.put(false, "isFalse"); +boolPair.put(null, null); + +HashMap filePair = new HashMap<>(); +java.io.File f = new java.io.File( getClass().getResource("/mapTestFile.txt").toURI() ); +filePair.put(f, "File"); + +GenericRecord r = new GenericData.Record(schema); +r.put(intMapField.name(), intPair); +r.put(decMapField.name(), decPair); +r.put(boolMapField.name(), boolPair); +r.put(fileMapField.name(), filePair); + +String json = r.toString(); +JsonFactory factory = new JsonFactory(); +JsonParser parser = factory.createJsonParser(json); +ObjectMapper mapper = new ObjectMapper(); + +// will throw exception if string is not parsable json +mapper.readTree(parser); + } + @Test public void testToStringEscapesControlCharsInBytes() throws Exception { GenericData data = GenericData.get(); ByteBuffer bytes = ByteBuffer.wrap(new byte[] {'a', '\n', 'b'}); diff --git a/lang/java/avro/src/test/resources/mapTestFile.txt b/lang/java/avro/src/test/resources/mapTestFile.txt new file mode 100644 index 0..e69de29bb This is an automated message from the Apache Git Service. To respond to the message, please
[jira] [Commented] (AVRO-2216) GenericData toString generates invalid JSON when map keys are not Strings
[ https://issues.apache.org/jira/browse/AVRO-2216?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16680010#comment-16680010 ] ASF GitHub Bot commented on AVRO-2216: -- dkulp commented on issue #330: AVRO-2216 Force use of String as Map keys URL: https://github.com/apache/avro/pull/330#issuecomment-437075632 Conflicts resolved and merged 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: us...@infra.apache.org > GenericData toString generates invalid JSON when map keys are not Strings > - > > Key: AVRO-2216 > URL: https://issues.apache.org/jira/browse/AVRO-2216 > Project: Apache Avro > Issue Type: Bug > Components: java >Affects Versions: 1.9.0 >Reporter: Alberto Lago >Assignee: Daniel Kulp >Priority: Major > Fix For: 1.9.0 > > > toString() method for records with maps that have non String Keys generates > wrong Json: > This code: > {code:java} > Schema intMapSchema = new Schema.Parser().parse("{\"type\": \"map\", > \"values\": \"string\", \"java-key-class\" : \"java.lang.Integer\"}"); > Field intMapField = new Field("intMap", Schema.createMap(intMapSchema), null, > null); > 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); > 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); > Schema schema = Schema.createRecord("my_record", "doc", "mytest", false); > schema.setFields(Arrays.asList(intMapField,decMapField,boolMapField,fileMapField)); > HashMap intPair = new HashMap<>(); > intPair.put(1, "one"); > intPair.put(2, "two"); > HashMap decPair = new HashMap<>(); > decPair.put(java.math.BigDecimal.valueOf(1), "one"); > decPair.put(java.math.BigDecimal.valueOf(2), "two"); > HashMap boolPair = new HashMap<>(); > boolPair.put(true, "isTrue"); > boolPair.put(false, "isFalse"); > boolPair.put(null, null); > HashMap filePair = new HashMap<>(); > java.io.File f = new java.io.File( > getClass().getResource("/mapTestFile.txt").toURI() ); > filePair.put(f, "File"); > GenericRecord r = new GenericData.Record(schema); > r.put(intMapField.name(), intPair); > r.put(decMapField.name(), decPair); > r.put(boolMapField.name(), boolPair); > r.put(fileMapField.name(), filePair); > String json = r.toString(); > {code} > Would generate the following json: > {code:java} > {"intMap": {1: "one", 2: "two"}, "decMap": {2: "two", 1: "one"}, "boolMap": > {null: null, false: "isFalse", true: "isTrue"}, "fileMap": > {/workspace/avro/lang/java/avro/target/test-classes/mapTestFile.txt: > "File"}}{code} > It's missing double quotes for all keys. > Note that I used classes that are considered as Stringable by ReflectData > (plus Integer.class ) and thus, would be able to be serializable. > [This change > |https://github.com/apache/avro/compare/master...trompa:master?diff=split=master#diff-5a41450f3008ee0da59dec14ada2356aL543]to > force the use of Strings always for Map Keys should be enough to fix this > error. > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (AVRO-2216) GenericData toString generates invalid JSON when map keys are not Strings
[ https://issues.apache.org/jira/browse/AVRO-2216?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16680022#comment-16680022 ] ASF GitHub Bot commented on AVRO-2216: -- nandorKollar commented on issue #330: AVRO-2216 Force use of String as Map keys URL: https://github.com/apache/avro/pull/330#issuecomment-437079486 @dkulp I see you merged this PR, but the commit message doesn't have any indication which Jira is related to this issue, making a bit hard to tell the connection between the PR and the Jira (often the PR is not referenced from the Jira too). If there's an already open Jira, can we include the Jira number in the commit message as we usually used before? 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: us...@infra.apache.org > GenericData toString generates invalid JSON when map keys are not Strings > - > > Key: AVRO-2216 > URL: https://issues.apache.org/jira/browse/AVRO-2216 > Project: Apache Avro > Issue Type: Bug > Components: java >Affects Versions: 1.9.0 >Reporter: Alberto Lago >Assignee: Daniel Kulp >Priority: Major > Fix For: 1.9.0 > > > toString() method for records with maps that have non String Keys generates > wrong Json: > This code: > {code:java} > Schema intMapSchema = new Schema.Parser().parse("{\"type\": \"map\", > \"values\": \"string\", \"java-key-class\" : \"java.lang.Integer\"}"); > Field intMapField = new Field("intMap", Schema.createMap(intMapSchema), null, > null); > 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); > 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); > Schema schema = Schema.createRecord("my_record", "doc", "mytest", false); > schema.setFields(Arrays.asList(intMapField,decMapField,boolMapField,fileMapField)); > HashMap intPair = new HashMap<>(); > intPair.put(1, "one"); > intPair.put(2, "two"); > HashMap decPair = new HashMap<>(); > decPair.put(java.math.BigDecimal.valueOf(1), "one"); > decPair.put(java.math.BigDecimal.valueOf(2), "two"); > HashMap boolPair = new HashMap<>(); > boolPair.put(true, "isTrue"); > boolPair.put(false, "isFalse"); > boolPair.put(null, null); > HashMap filePair = new HashMap<>(); > java.io.File f = new java.io.File( > getClass().getResource("/mapTestFile.txt").toURI() ); > filePair.put(f, "File"); > GenericRecord r = new GenericData.Record(schema); > r.put(intMapField.name(), intPair); > r.put(decMapField.name(), decPair); > r.put(boolMapField.name(), boolPair); > r.put(fileMapField.name(), filePair); > String json = r.toString(); > {code} > Would generate the following json: > {code:java} > {"intMap": {1: "one", 2: "two"}, "decMap": {2: "two", 1: "one"}, "boolMap": > {null: null, false: "isFalse", true: "isTrue"}, "fileMap": > {/workspace/avro/lang/java/avro/target/test-classes/mapTestFile.txt: > "File"}}{code} > It's missing double quotes for all keys. > Note that I used classes that are considered as Stringable by ReflectData > (plus Integer.class ) and thus, would be able to be serializable. > [This change > |https://github.com/apache/avro/compare/master...trompa:master?diff=split=master#diff-5a41450f3008ee0da59dec14ada2356aL543]to > force the use of Strings always for Map Keys should be enough to fix this > error. > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (AVRO-2216) GenericData toString generates invalid JSON when map keys are not Strings
[ https://issues.apache.org/jira/browse/AVRO-2216?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16588942#comment-16588942 ] Alberto Lago commented on AVRO-2216: PR created with proposed change. See https://github.com/apache/avro/pull/330 > GenericData toString generates invalid JSON when map keys are not Strings > - > > Key: AVRO-2216 > URL: https://issues.apache.org/jira/browse/AVRO-2216 > Project: Avro > Issue Type: Bug > Components: java >Affects Versions: 1.9.0 >Reporter: Alberto Lago >Priority: Major > > toString() method for records with maps that have non String Keys generates > wrong Json: > This code: > {code:java} > Schema intMapSchema = new Schema.Parser().parse("{\"type\": \"map\", > \"values\": \"string\", \"java-key-class\" : \"java.lang.Integer\"}"); > Field intMapField = new Field("intMap", Schema.createMap(intMapSchema), null, > null); > 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); > 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); > Schema schema = Schema.createRecord("my_record", "doc", "mytest", false); > schema.setFields(Arrays.asList(intMapField,decMapField,boolMapField,fileMapField)); > HashMap intPair = new HashMap<>(); > intPair.put(1, "one"); > intPair.put(2, "two"); > HashMap decPair = new HashMap<>(); > decPair.put(java.math.BigDecimal.valueOf(1), "one"); > decPair.put(java.math.BigDecimal.valueOf(2), "two"); > HashMap boolPair = new HashMap<>(); > boolPair.put(true, "isTrue"); > boolPair.put(false, "isFalse"); > boolPair.put(null, null); > HashMap filePair = new HashMap<>(); > java.io.File f = new java.io.File( > getClass().getResource("/mapTestFile.txt").toURI() ); > filePair.put(f, "File"); > GenericRecord r = new GenericData.Record(schema); > r.put(intMapField.name(), intPair); > r.put(decMapField.name(), decPair); > r.put(boolMapField.name(), boolPair); > r.put(fileMapField.name(), filePair); > String json = r.toString(); > {code} > Would generate the following json: > {code:java} > {"intMap": {1: "one", 2: "two"}, "decMap": {2: "two", 1: "one"}, "boolMap": > {null: null, false: "isFalse", true: "isTrue"}, "fileMap": > {/workspace/avro/lang/java/avro/target/test-classes/mapTestFile.txt: > "File"}}{code} > It's missing double quotes for all keys. > Note that I used classes that are considered as Stringable by ReflectData > (plus Integer.class ) and thus, would be able to be serializable. > [This change > |https://github.com/apache/avro/compare/master...trompa:master?diff=split=master#diff-5a41450f3008ee0da59dec14ada2356aL543]to > force the use of Strings always for Map Keys should be enough to fix this > error. > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (AVRO-2216) GenericData toString generates invalid JSON when map keys are not Strings
[ https://issues.apache.org/jira/browse/AVRO-2216?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16588937#comment-16588937 ] ASF GitHub Bot commented on AVRO-2216: -- trompa opened a new pull request #330: AVRO-2216 Force use of String as Map keys URL: https://github.com/apache/avro/pull/330 Adding always double quotes. 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: us...@infra.apache.org > GenericData toString generates invalid JSON when map keys are not Strings > - > > Key: AVRO-2216 > URL: https://issues.apache.org/jira/browse/AVRO-2216 > Project: Avro > Issue Type: Bug > Components: java >Affects Versions: 1.9.0 >Reporter: Alberto Lago >Priority: Major > > toString() method for records with maps that have non String Keys generates > wrong Json: > This code: > {code:java} > Schema intMapSchema = new Schema.Parser().parse("{\"type\": \"map\", > \"values\": \"string\", \"java-key-class\" : \"java.lang.Integer\"}"); > Field intMapField = new Field("intMap", Schema.createMap(intMapSchema), null, > null); > 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); > 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); > Schema schema = Schema.createRecord("my_record", "doc", "mytest", false); > schema.setFields(Arrays.asList(intMapField,decMapField,boolMapField,fileMapField)); > HashMap intPair = new HashMap<>(); > intPair.put(1, "one"); > intPair.put(2, "two"); > HashMap decPair = new HashMap<>(); > decPair.put(java.math.BigDecimal.valueOf(1), "one"); > decPair.put(java.math.BigDecimal.valueOf(2), "two"); > HashMap boolPair = new HashMap<>(); > boolPair.put(true, "isTrue"); > boolPair.put(false, "isFalse"); > boolPair.put(null, null); > HashMap filePair = new HashMap<>(); > java.io.File f = new java.io.File( > getClass().getResource("/mapTestFile.txt").toURI() ); > filePair.put(f, "File"); > GenericRecord r = new GenericData.Record(schema); > r.put(intMapField.name(), intPair); > r.put(decMapField.name(), decPair); > r.put(boolMapField.name(), boolPair); > r.put(fileMapField.name(), filePair); > String json = r.toString(); > {code} > Would generate the following json: > {code:java} > {"intMap": {1: "one", 2: "two"}, "decMap": {2: "two", 1: "one"}, "boolMap": > {null: null, false: "isFalse", true: "isTrue"}, "fileMap": > {/workspace/avro/lang/java/avro/target/test-classes/mapTestFile.txt: > "File"}}{code} > It's missing double quotes for all keys. > Note that I used classes that are considered as Stringable by ReflectData > (plus Integer.class ) and thus, would be able to be serializable. > [This change > |https://github.com/apache/avro/compare/master...trompa:master?diff=split=master#diff-5a41450f3008ee0da59dec14ada2356aL543]to > force the use of Strings always for Map Keys should be enough to fix this > error. > -- This message was sent by Atlassian JIRA (v7.6.3#76005)