You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Teemu Kanstrén <tk...@gmail.com> on 2015/08/21 17:00:20 UTC

Java code generation for nested non-optional records

Hello all,

 I am using the Maven Avro compiler to generate Java code for my Avro
schemas. Problem. If I have a nested record which is optional, everything
generates OK. If the type is not optional, the generated reference is set
to generic Object type and I cannot use it. What might I be doing wrong?

 Example schema:

{
  "namespace": "avro.test",
  "type": "record",
  "name": "TopObj",
  "fields": [
    {"name": "inner1", "type": [
      {"type": "record", "name": "Inner1",
        "fields": [
          {"name": "value1", "type": "string"},
          {"name": "value2", "type": "string"}
        ]
      }]
    },
    {"name": "inner2", "type": [
      {"type": "record", "name": "Inner2",
        "fields": [
          {"name": "value3", "type": "string"},
          {"name": "value4", "type": "string"}
         ]
      }, "null"]
    }
  ]
}

Running the Avro Java code generator for this produces three classes
as expected: TopObj, Inner1 and Inner2. However, in TopObject, the
definitions are as:

@Deprecated public java.lang.Object inner1;
@Deprecated public avro.test.Inner2 inner2;

If I set inner1 as optional (add the , "null" to it) the type also
becomes Inner1 in TopObj, which is as I would expect. But I do not
want it to be optional.

How do I get inner1 to generate as type Inner1 in TopObj?

Cheers & thanks,

Teemu

Re: Java code generation for nested non-optional records

Posted by Teemu Kanstrén <tk...@gmail.com>.
Thanks! This works. So it was the extra [] left over from the optional
definition.

These schemas keep confusing me with all the combinations and braces.. :)

On 21 August 2015 at 18:28, Prajwal Tuladhar <pr...@infynyxx.com> wrote:

> How about?
>
> {
>   "namespace": "avrò.test",
>   "type": "record",
>   "name": "TopObj",
>   "fields": [
>     {
>       "name": "inner1",
>       "type": {
>         "name": "Inner1",
>         "type": "record",
>         "fields": [
>           {
>             "name": "value1",
>             "type": "string"
>           },
>           {
>             "name": "value2",
>             "type": "string"
>           }
>         ]
>       }
>     },
>     {
>       "name": "inner2",
>       "type": [
>         {
>           "type": "record",
>           "name": "Inner2",
>           "fields": [
>             {
>               "name": "value3",
>               "type": "string"
>             },
>             {
>               "name": "value4",
>               "type": "string"
>             }
>           ]
>         },
>         "null"
>       ]
>     }
>   ]
> }
>
>
> On Fri, Aug 21, 2015 at 3:00 PM, Teemu Kanstrén <tk...@gmail.com>
> wrote:
>
>> Hello all,
>>
>>  I am using the Maven Avro compiler to generate Java code for my Avro
>> schemas. Problem. If I have a nested record which is optional, everything
>> generates OK. If the type is not optional, the generated reference is set
>> to generic Object type and I cannot use it. What might I be doing wrong?
>>
>>  Example schema:
>>
>> {
>>   "namespace": "avro.test",
>>   "type": "record",
>>   "name": "TopObj",
>>   "fields": [
>>     {"name": "inner1", "type": [
>>       {"type": "record", "name": "Inner1",
>>         "fields": [
>>           {"name": "value1", "type": "string"},
>>           {"name": "value2", "type": "string"}
>>         ]
>>       }]
>>     },
>>     {"name": "inner2", "type": [
>>       {"type": "record", "name": "Inner2",
>>         "fields": [
>>           {"name": "value3", "type": "string"},
>>           {"name": "value4", "type": "string"}
>>          ]
>>       }, "null"]
>>     }
>>   ]
>> }
>>
>> Running the Avro Java code generator for this produces three classes as expected: TopObj, Inner1 and Inner2. However, in TopObject, the definitions are as:
>>
>> @Deprecated public java.lang.Object inner1;
>> @Deprecated public avro.test.Inner2 inner2;
>>
>> If I set inner1 as optional (add the , "null" to it) the type also becomes Inner1 in TopObj, which is as I would expect. But I do not want it to be optional.
>>
>> How do I get inner1 to generate as type Inner1 in TopObj?
>>
>> Cheers & thanks,
>>
>> Teemu
>>
>>
>
>
> --
> --
> Cheers,
> Praj
>

Re: Java code generation for nested non-optional records

Posted by Prajwal Tuladhar <pr...@infynyxx.com>.
How about?

{
  "namespace": "avrò.test",
  "type": "record",
  "name": "TopObj",
  "fields": [
    {
      "name": "inner1",
      "type": {
        "name": "Inner1",
        "type": "record",
        "fields": [
          {
            "name": "value1",
            "type": "string"
          },
          {
            "name": "value2",
            "type": "string"
          }
        ]
      }
    },
    {
      "name": "inner2",
      "type": [
        {
          "type": "record",
          "name": "Inner2",
          "fields": [
            {
              "name": "value3",
              "type": "string"
            },
            {
              "name": "value4",
              "type": "string"
            }
          ]
        },
        "null"
      ]
    }
  ]
}


On Fri, Aug 21, 2015 at 3:00 PM, Teemu Kanstrén <tk...@gmail.com> wrote:

> Hello all,
>
>  I am using the Maven Avro compiler to generate Java code for my Avro
> schemas. Problem. If I have a nested record which is optional, everything
> generates OK. If the type is not optional, the generated reference is set
> to generic Object type and I cannot use it. What might I be doing wrong?
>
>  Example schema:
>
> {
>   "namespace": "avro.test",
>   "type": "record",
>   "name": "TopObj",
>   "fields": [
>     {"name": "inner1", "type": [
>       {"type": "record", "name": "Inner1",
>         "fields": [
>           {"name": "value1", "type": "string"},
>           {"name": "value2", "type": "string"}
>         ]
>       }]
>     },
>     {"name": "inner2", "type": [
>       {"type": "record", "name": "Inner2",
>         "fields": [
>           {"name": "value3", "type": "string"},
>           {"name": "value4", "type": "string"}
>          ]
>       }, "null"]
>     }
>   ]
> }
>
> Running the Avro Java code generator for this produces three classes as expected: TopObj, Inner1 and Inner2. However, in TopObject, the definitions are as:
>
> @Deprecated public java.lang.Object inner1;
> @Deprecated public avro.test.Inner2 inner2;
>
> If I set inner1 as optional (add the , "null" to it) the type also becomes Inner1 in TopObj, which is as I would expect. But I do not want it to be optional.
>
> How do I get inner1 to generate as type Inner1 in TopObj?
>
> Cheers & thanks,
>
> Teemu
>
>


-- 
--
Cheers,
Praj