On 10/18/11 9:47 AM, "Doug Cutting" <[email protected]> wrote:
>On 10/17/2011 08:14 PM, 常冰琳 wrote: >> What I do in the demo is add a new nullable string in server side, not >> change a string to nullable string. >> I add a new field with default value using specific, and it works fine, >> so I suspect the reason that reflect doesn't work is that I didn't add >> default value to the nullable string field. >> Perhaps the default value for nullable field should be null by default? > >Reflect by default assumes that all values are not nullable. This is >perhaps a bug, but the alternative is to make every non-numeric value >nullable, which would result in verbose schemas. > >To amend this, you can use Avro's @Nullable annotation: > >http://avro.apache.org/docs/current/api/java/org/apache/avro/reflect/Nulla >ble.html > >This can be applied to parameters, return types and fields. > >For example: > >import org.apache.avro.reflect.Nullable; > >public class Foo { > @Nullable String x; > public void setX(@Nullable String x) { this.x = x; } > @Nullable public String getX() { return x; } >} The problem is that this does not provide the ability to evolve schemas if you add a field since you would need @Default or something similar, as well: @Nullable @Default(null) Does reflect have any concept of default values? > >Doug
