[ https://issues.apache.org/jira/browse/AVRO-997?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13474548#comment-13474548 ]
Sean Busbey commented on AVRO-997: ---------------------------------- Scott, I could write a patch that maintains the current behavior for unions of "things that look like enum members when toString is called" and enums, which is to say the non-enum type gets picked if it is present. eg ["string", "long", {"type":"enum", "symbols":["ONE", "TWO", "42"]}] says that "ONE" is a string and 42 is a long. Since it would be consistent with current behavior, a warning in the API about the coercion might be sufficient. It would be less efficient than picking the enum member, but doing that would be just as breaking as reworking the generic api to be strict. The only time I've seen this come up is when someone has a union field and then wants to mark it as optional so they move it to a union with null. While their original use of Java String for field values was incorrect, the generic api as is would have worked fine up until the addition of the union. In general I'm a fan of "just works" behavior from APIs, but I can understand Doug's desire to favor consistent strict application as a way of steering users away from behavior that will lead to confusing non-roundtrip-able edge cases. > Union of enum and null cannot be serialized > ------------------------------------------- > > Key: AVRO-997 > URL: https://issues.apache.org/jira/browse/AVRO-997 > Project: Avro > Issue Type: Bug > Affects Versions: 1.5.1 > Reporter: Aaron Kimball > Assignee: Sean Busbey > Fix For: 1.8.0 > > Attachments: AVRO-997.patch, AVRO-997.patch > > > I have a schema like: > {code} > [ > { > "type": "enum", > "name": "Gender", > "symbols": ["M", "F"] > }, > { > "type" : "record", > "name" : "Foo", > "fields" : [ > { "type" : ["Gender", "null"], "name" : "gender" }, > ... > ] > } > ] > {code} > I build a record like {{Foo foo = new Foo(); foo.gender = Gender.M;}} > When I go to serialize this, I get: > {code}Not in union > [{"type":"enum","name":"Gender","symbols":["M","F"]},"null"]: M > at > org.apache.avro.generic.GenericData.resolveUnion(GenericData.java:482) > at > org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:70) > at > org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:104) > at > org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:65) > at > org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:57) > {code} -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira