Arun sethia created AVRO-2137:
---------------------------------

             Summary: avro JsonDecoding additional field in array type
                 Key: AVRO-2137
                 URL: https://issues.apache.org/jira/browse/AVRO-2137
             Project: Avro
          Issue Type: Bug
          Components: java
    Affects Versions: 1.8.1
            Reporter: Arun sethia


I have following avro schema

{ "type":"record", "name":"test", "namespace":"test.name",

"fields":[

               {"name":"items","type":

                    {"type":"array", "items":

                           {"type":"record","name":"items", "fields":

                               [

                                   \{"name":"name","type":"string"},

                                   \{"name":"state","type":"string"}

                              ]

                         }

                 }

           }

        , \{"name":"firstname","type":"string"}

       ]

}

 

when I am using Json decoder and avro encoder to encode Json data (scala code):

 

_val writer = new GenericDatumWriter[GenericRecord](schema)_

_val reader = new GenericDatumReader[GenericRecord](schema)_

_val baos = new ByteArrayOutputStream_

_val decoder: JsonDecoder = DecoderFactory.get.jsonDecoder(schema, json)_

_val encoder = EncoderFactory.get.binaryEncoder(baos, null)_

_val datum = reader.read(null, decoder) writer.write(datum, encoder)_

_encoder.flush()_

_val avroByteArray = baos.toByteArray_

 

*scenario1:* when I am passing following json to encode it works fine:

 \{ "items": [ { "name": "dallas", "state": "TX" }], "firstname":"arun"}

 *scenario2:* when I am passing additional attribute in json at root level 
(lastname) it is able to encode and works fine:

 \{ "items": [{ "name": "dallas", "state": "TX" }], "firstname":"fname", 
"lastname":"lname"}

*scenario3*: when I am add additional attribute in array record (country) it is 
throwing following exception:

Expected record-end. Got FIELD_NAME org.apache.avro.AvroTypeException: Expected 
record-end. Got FIELD_NAME at 
org.apache.avro.io.JsonDecoder.error(JsonDecoder.java:698) \{ "items": [ { 
"name": "dallas", "state": "TX", "country":"USA" } ], "firstname":"fname", 
"lastname":"lname" }

 In case of if we have any additional element in array type, it should work in 
same way as normal record; it should just discard them and decode the Json data.



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

Reply via email to