[ https://issues.apache.org/jira/browse/AVRO-2723?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17032362#comment-17032362 ]
Ryan Skraba commented on AVRO-2723: ----------------------------------- OK! Thanks for the explanation, I think I understand better. In my experience, schemas generated through ReflectData have been usually "short-lived" and infrequently use schema evolution (where the default attribute is useful). I have no objection to improving how schemas are created from POJO's via reflection, however! I suspect that the *{{AvroDefault}}* solution was chosen precisely because it permits generating the whole schema without having to instantiate an instance. However, I'm pretty sure you *can* apply it to *{{Integer}}* and complex classes. For example, the following works: {code:java} static class DefaultTest { @AvroDefault("1") Integer foo; } static class ComplexDefaultTest { @AvroDefault("2") Integer bar; @AvroDefault("{\"foo\":3}}") DefaultTest inner; } {code} A couple of things to consider: 1. there's already a *{{getDefaultValue(Field)}}*, which does something slightly different than the method of the same name you propose -- I'd suggest picking something more explicit. *{{createSchemaDefaultValue()}}* perhaps? 2. as you point out *{{ReflectData.AllowNull}}* (used in your example) always generates nullable fields with null-first unions, which can only have *{{null}}* as a default. You might want to switch the order of types in the union if a non-null default is discovered. I think the only existing solution today (without refactoring ReflectData) would be to generated schema, then "massage" it manually to modify the defaults that it gave you before saving it. > 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: Wish > Components: java > Affects Versions: 1.9.1 > Reporter: Anh 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)