You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Anton Kondratev (Jira)" <ji...@apache.org> on 2022/01/10 08:22:00 UTC

[jira] [Updated] (AVRO-3286) Serialization of optional fields with C++ library doesn't work

     [ https://issues.apache.org/jira/browse/AVRO-3286?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Anton Kondratev updated AVRO-3286:
----------------------------------
    Description: 
There is a problem with serialization of data having optional fields. When I pass null in corresponding field it works but when I pass non-null value then serialization fails.

Example schema:
{code:java}
{
    "type": "record",
    "name": "schema",
    "namespace": "space",
    "fields": [
       {
           "name": "username",
           "type": "string"
       },
       {
           "name": "data",
           "type": [
               "null",
               "string"
           ],
           "defaults": null
       },
       {
           "name": "timestamp",
           "type": "long"
       }
    ]
}{code}
Data that works:

{"username":"user1","data":null,"timestamp": 1366150681 }

Data that fails:

{"username":"user1","data":"test","timestamp": 1366150681 }

My code:
{code:java}
std::unique_ptr<avro::InputStream> in = avro::memoryInputStream((const uint8_t*)&json[0], json.size()); // json is incoming data
avro::ValidSchema validSchema;
std::istringstream ins(schema); // schema is avro-schema
try {
    avro::compileJsonSchema(ins, validSchema);
}
catch (const std::exception& e1) {
    std::string errstr = e1.what();
}
avro::DecoderPtr jd = avro::jsonDecoder(validSchema);
avro::GenericDatum datum(validSchema);
jd->init(*in);
avro::decode(*jd, datum); //serialization with non-null data fails somewhere inside this step{code}
 

  was:
There is a problem with serialization of data having optional fields. When I pass null in corresponding field it works but when I pass non-null value then serialization fails.

Example schema:
{code:java}
{
    "type": "record",
    "name": "schema",
    "namespace": "space",
    "fields": [
       {
           "name": "username",
           "type": "string"
       },
       {
           "name": "data",
           "type": [
               "null",
               "string"
           ],
           "defaults": null
       },
       {
           "name": "timestamp",
           "type": "long"
       }
    ]
}{code}
 

Data that works:

{"username":"user1","data":null,"timestamp": 1366150681 }

 

Data that fails:

{"username":"user1","data":"test","timestamp": 1366150681 }

 

My code:

 

 

 
{code:java}
std::unique_ptr<avro::InputStream> in = avro::memoryInputStream((const uint8_t*)&json[0], json.size()); // json is incoming data
avro::ValidSchema validSchema;
std::istringstream ins(schema); // schema is avro-schema
try {
    avro::compileJsonSchema(ins, validSchema);
}
catch (const std::exception& e1) {
    std::string errstr = e1.what();
}
avro::DecoderPtr jd = avro::jsonDecoder(validSchema);
avro::GenericDatum datum(validSchema);
jd->init(*in);
avro::decode(*jd, datum); //serialization with non-null data fails somewhere inside this step{code}
 


> Serialization of optional fields with C++ library doesn't work
> --------------------------------------------------------------
>
>                 Key: AVRO-3286
>                 URL: https://issues.apache.org/jira/browse/AVRO-3286
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: c++
>    Affects Versions: 1.10.2
>            Reporter: Anton Kondratev
>            Priority: Critical
>
> There is a problem with serialization of data having optional fields. When I pass null in corresponding field it works but when I pass non-null value then serialization fails.
> Example schema:
> {code:java}
> {
>     "type": "record",
>     "name": "schema",
>     "namespace": "space",
>     "fields": [
>        {
>            "name": "username",
>            "type": "string"
>        },
>        {
>            "name": "data",
>            "type": [
>                "null",
>                "string"
>            ],
>            "defaults": null
>        },
>        {
>            "name": "timestamp",
>            "type": "long"
>        }
>     ]
> }{code}
> Data that works:
> {"username":"user1","data":null,"timestamp": 1366150681 }
> Data that fails:
> {"username":"user1","data":"test","timestamp": 1366150681 }
> My code:
> {code:java}
> std::unique_ptr<avro::InputStream> in = avro::memoryInputStream((const uint8_t*)&json[0], json.size()); // json is incoming data
> avro::ValidSchema validSchema;
> std::istringstream ins(schema); // schema is avro-schema
> try {
>     avro::compileJsonSchema(ins, validSchema);
> }
> catch (const std::exception& e1) {
>     std::string errstr = e1.what();
> }
> avro::DecoderPtr jd = avro::jsonDecoder(validSchema);
> avro::GenericDatum datum(validSchema);
> jd->init(*in);
> avro::decode(*jd, datum); //serialization with non-null data fails somewhere inside this step{code}
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)