You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2020/02/04 16:03:28 UTC

[avro] branch branch-1.9 updated: AVRO-2641: Fix for SpecificRecord String deserialization (#728)

This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch branch-1.9
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.9 by this push:
     new f25dc7c  AVRO-2641: Fix for SpecificRecord String deserialization (#728)
f25dc7c is described below

commit f25dc7c49457c75307a0257c90bf58eda9b19148
Author: Magne Helleborg <ma...@gmail.com>
AuthorDate: Tue Feb 4 17:01:56 2020 +0100

    AVRO-2641: Fix for SpecificRecord String deserialization (#728)
    
    * AVRO-2641: Fix for SpecificRecord String deserialization
    
    Changed from casting object to String when schema expects String field.
    Since Avro can represent Strings in Java as either String,
    CharSequence or org.apache.avro.util.Utf8,the put can fail
    with a ClassCastException.
    By changing from cast to toString() the issue is circumvented.
    
    * Apply suggestions from code review
    
    Added spacing for legibility as suggested.
    
    Co-Authored-By: Fokko Driesprong <fo...@driesprong.frl>
    
    * AVRO-2641: Whitespace fixes for unit tests.
    
    Co-authored-by: Fokko Driesprong <fo...@driesprong.frl>
    Co-authored-by: RyanSkraba <ry...@skraba.com>
---
 .../apache/avro/compiler/specific/templates/java/classic/record.vm    | 2 +-
 .../test/compiler/output-string/avro/examples/baseball/FieldTest.java | 2 +-
 .../test/compiler/output-string/avro/examples/baseball/Player.java    | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
index 23db9d8..0c262b8 100755
--- a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
+++ b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
@@ -201,7 +201,7 @@ static {
     switch (field$) {
 #set ($i = 0)
 #foreach ($field in $schema.getFields())
-    case $i: ${this.mangle($field.name(), $schema.isError())} = #if(${this.javaType($field.schema())} != "java.lang.Object")(${this.javaType($field.schema())})#{end}value$; break;
+    case $i: ${this.mangle($field.name(), $schema.isError())} = #if(${this.javaType($field.schema())} != "java.lang.Object" && ${this.javaType($field.schema())} != "java.lang.String")(${this.javaType($field.schema())})#{end}value$#if(${this.javaType($field.schema())} == "java.lang.String") != null ? value$.toString() : null#{end}; break;
 #set ($i = $i + 1)
 #end
     default: throw new org.apache.avro.AvroRuntimeException("Bad index");
diff --git a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java
index 7270748..6d08f85 100644
--- a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java
+++ b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java
@@ -145,7 +145,7 @@ static {
   public void put(int field$, java.lang.Object value$) {
     switch (field$) {
     case 0: number = (java.lang.Integer)value$; break;
-    case 1: last_name = (java.lang.String)value$; break;
+    case 1: last_name = value$ != null ? value$.toString() : null; break;
     case 2: timestamp = (java.time.Instant)value$; break;
     case 3: timestampMicros = (java.time.Instant)value$; break;
     case 4: timeMillis = (java.time.LocalTime)value$; break;
diff --git a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
index 83a7892..0a0c91b 100644
--- a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
+++ b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
@@ -117,8 +117,8 @@ public class Player extends org.apache.avro.specific.SpecificRecordBase implemen
   public void put(int field$, java.lang.Object value$) {
     switch (field$) {
     case 0: number = (java.lang.Integer)value$; break;
-    case 1: first_name = (java.lang.String)value$; break;
-    case 2: last_name = (java.lang.String)value$; break;
+    case 1: first_name = value$ != null ? value$.toString() : null; break;
+    case 2: last_name = value$ != null ? value$.toString() : null; break;
     case 3: position = (java.util.List<avro.examples.baseball.Position>)value$; break;
     default: throw new org.apache.avro.AvroRuntimeException("Bad index");
     }