You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/08/22 14:43:00 UTC

[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&focusedCommentId=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:
users@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<Integer, String> intPair =  new HashMap<>();
> intPair.put(1, "one");
> intPair.put(2, "two");
> 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<>();
> boolPair.put(true, "isTrue");
> boolPair.put(false, "isFalse");
> boolPair.put(null, null);
> HashMap<java.io.File, String> 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&name=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)