[ 
https://issues.apache.org/jira/browse/AVRO-2723?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17052619#comment-17052619
 ] 

Kengo Seki commented on AVRO-2723:
----------------------------------

[~anhldbk],

bq. But if without refactoring ReflectData, where we will put this function?

I think Ryan is not against refactoring ReflectData, but just suggested a 
better method name (createSchemaDefaultValue rather than getDefaultValue).
(Correct me if I misunderstood [~rskraba])
So I think you can submit a PR. Your proposal sounds reasonable to me.

BTW,

bq. will trigger an exception:

You have to put double quotes into the default value, as Ryan showed above:

{code}
package com.example;

import org.apache.avro.Schema;
import org.apache.avro.reflect.AvroDefault;
import org.apache.avro.reflect.ReflectData;

public class App {
    public static void main(String[] args) {
        Schema schema = ReflectData.get().getSchema(User.class);
        System.out.println(schema.toString());
    }
}

class User {
    @AvroDefault("\"Andy\"")
    public String first;
}
{code}

{code}
{"type":"record","name":"User","namespace":"com.example","fields":[{"name":"first","type":"string","default":"Andy"}]}
{code}

> Avro Java: Obtaining default values for POJO objects with ReflectData
> ---------------------------------------------------------------------
>
>                 Key: AVRO-2723
>                 URL: https://issues.apache.org/jira/browse/AVRO-2723
>             Project: Apache Avro
>          Issue Type: New Feature
>          Components: java
>    Affects Versions: 1.9.1
>            Reporter: Andy Le
>            Priority: Critical
>
> Hi guys,
>  
> I've got a simple app using Avro Reflection:
>  
> {code:java}
> public class App {
>   public static void main(String[] args) {
>     testReflection();
>   }
>   static class User {
>     public String first = "Andy";
>     public String last = "Le";
>   }
>   static void testReflection(){
>     // get the reflected schema for packets
>     Schema schema = ReflectData.AllowNull.get().getSchema(User.class);
>     System.out.println(schema.toString(true));
>   }
> {code}
> The output on console will be:
> {noformat}
> {
>   "type" : "record",
>   "name" : "User",
>   "namespace" : "App",
>   "fields" : [ {
>     "name" : "first",
>     "type" : [ "null", "string" ],
>     "default" : null
>   }, {
>     "name" : "last",
>     "type" : [ "null", "string" ],
>     "default" : null
>   } ]
> }
> {noformat}
>  
> As you can see, there's no default values for fields. Would you please tell 
> me how to obtain such values?
> Thank you.
>  
>  
>  
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to