Rog, I alluded to it previously, but I really think you should send a PR to improve the docs. This knowledge was hard-won for you, and the authors of Avro are quite responsive at the moment after a couple of years (the black-winter of 1.8.2) of low-activity.
Carpe diem! Thanks, this thread has been fun! Lee Hambley http://lee.hambley.name/ +49 (0) 170 298 5667 On Fri, 6 Dec 2019 at 14:47, roger peppe <rogpe...@gmail.com> wrote: > On Fri, 6 Dec 2019 at 10:38, Ryan Skraba <r...@skraba.com> wrote: > >> Hello! I had a Java unit test ready to go (looking at default values >> for complex types for AVRO-2636), so just reporting back (the easy >> work!): >> > > Thanks for the responses! > > >> 1. In Java, the schema above is parsed without error, but when >> attempting to use the default value, it fails with a >> NullPointerException (trying to find the symbol C in E1). >> > > I tried it with gogen-avro <https://github.com/actgardner/gogen-avro> and > I had a similar issue. It generates invalid Go code as output. > > 2. If you were to disambiguate the symbols using the Avro JSON >> encoding ("default": [{"E1":"B"},{"E2":"A"},{"E2":"C"}]), Java fails >> while parsing the schema: >> >> org.apache.avro.AvroTypeException: Invalid default for field F: >> [{"E1":"B"},{"E2":"A"},{"E2":"C"}] not a >> >> {"type":"array","items":[{"type":"enum","name":"E1","symbols":["A","B"]},{"type":"enum","name":"E2","symbols":["B","A","C"]}]} >> at org.apache.avro.Schema.validateDefault(Schema.java:1542) >> at org.apache.avro.Schema.access$500(Schema.java:87) >> at org.apache.avro.Schema$Field.<init>(Schema.java:523) >> at org.apache.avro.Schema.parse(Schema.java:1649) >> at org.apache.avro.Schema$Parser.parse(Schema.java:1396) >> at org.apache.avro.Schema$Parser.parse(Schema.java:1384) >> >> It seems that Java implements `Only the first schema in any union can >> be used in a default value` as opposed to `Default values for union >> fields correspond to the first schema in the union` (in the example, >> it isn't a union field). >> >> Naively, I would expect any JSON encoded data to be a valid default >> value (which is not what the spec says). Does anyone know why the >> "first schema only" rule was added to the spec? >> > > Yes, I think the authors missed a trick here. I suspect things would be > cleaner if the default value was encoded in exactly the way as JSON-encoded > Avro values. As things currently are, an implementer needs to implement two > slightly different ways of translating from JSON to an Avro value. > > cheers, > rog. >