diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..a315b97 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,5 @@ +# Changelog + +# 0.5 - 2024-02-26 + +- Fix: oneofs were not being emitted. diff --git a/avro/record.go b/avro/record.go index a4127fb..93703d3 100644 --- a/avro/record.go +++ b/avro/record.go @@ -78,6 +78,7 @@ func RecordFromProto(proto *descriptorpb.DescriptorProto, namespace string) []Na if field.OneofIndex != nil && !field.GetProto3Optional() { union := oneofs[field.GetOneofIndex()].Type.(Union) union.Types = append(union.Types, FieldTypeFromProto(field)) + oneofs[field.GetOneofIndex()].Type = union } else { fields = append(fields, FieldFromProto(field)) } diff --git a/testdata/base/AOneOf.avsc b/testdata/base/AOneOf.avsc new file mode 100644 index 0000000..1f59e63 --- /dev/null +++ b/testdata/base/AOneOf.avsc @@ -0,0 +1,37 @@ +{ + "type": "record", + "name": "AOneOf", + "namespace": "testdata", + "fields": [ + { + "name": "a_oneof", + "type": [ + { + "type": "record", + "name": "TypeA", + "namespace": "testdata", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] + }, + { + "type": "record", + "name": "TypeB", + "namespace": "testdata", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] + } + ], + "default": null + } + ] +} \ No newline at end of file diff --git a/testdata/base/AYowzaMapEntry.avsc b/testdata/base/AYowzaMapEntry.avsc index ec68f11..8f576f3 100644 --- a/testdata/base/AYowzaMapEntry.avsc +++ b/testdata/base/AYowzaMapEntry.avsc @@ -25,4 +25,4 @@ "default": {} } ] -} +} \ No newline at end of file diff --git a/testdata/base/TypeA.avsc b/testdata/base/TypeA.avsc new file mode 100644 index 0000000..a0de7f4 --- /dev/null +++ b/testdata/base/TypeA.avsc @@ -0,0 +1,12 @@ +{ + "type": "record", + "name": "TypeA", + "namespace": "testdata", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] +} \ No newline at end of file diff --git a/testdata/base/TypeB.avsc b/testdata/base/TypeB.avsc new file mode 100644 index 0000000..e3e6c51 --- /dev/null +++ b/testdata/base/TypeB.avsc @@ -0,0 +1,12 @@ +{ + "type": "record", + "name": "TypeB", + "namespace": "testdata", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] +} \ No newline at end of file diff --git a/testdata/base/Widget.avsc b/testdata/base/Widget.avsc index ee68a98..5821529 100644 --- a/testdata/base/Widget.avsc +++ b/testdata/base/Widget.avsc @@ -181,6 +181,50 @@ "values": "testdata.StringList" }, "default": {} + }, + { + "name": "a_one_of", + "type": [ + "null", + { + "type": "record", + "name": "AOneOf", + "namespace": "testdata", + "fields": [ + { + "name": "a_oneof", + "type": [ + { + "type": "record", + "name": "TypeA", + "namespace": "testdata", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] + }, + { + "type": "record", + "name": "TypeB", + "namespace": "testdata", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] + } + ], + "default": null + } + ] + } + ], + "default": null } ] } \ No newline at end of file diff --git a/testdata/collapse_fields/AOneOf.avsc b/testdata/collapse_fields/AOneOf.avsc new file mode 100644 index 0000000..1f59e63 --- /dev/null +++ b/testdata/collapse_fields/AOneOf.avsc @@ -0,0 +1,37 @@ +{ + "type": "record", + "name": "AOneOf", + "namespace": "testdata", + "fields": [ + { + "name": "a_oneof", + "type": [ + { + "type": "record", + "name": "TypeA", + "namespace": "testdata", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] + }, + { + "type": "record", + "name": "TypeB", + "namespace": "testdata", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] + } + ], + "default": null + } + ] +} \ No newline at end of file diff --git a/testdata/collapse_fields/TypeA.avsc b/testdata/collapse_fields/TypeA.avsc new file mode 100644 index 0000000..a0de7f4 --- /dev/null +++ b/testdata/collapse_fields/TypeA.avsc @@ -0,0 +1,12 @@ +{ + "type": "record", + "name": "TypeA", + "namespace": "testdata", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] +} \ No newline at end of file diff --git a/testdata/collapse_fields/TypeB.avsc b/testdata/collapse_fields/TypeB.avsc new file mode 100644 index 0000000..e3e6c51 --- /dev/null +++ b/testdata/collapse_fields/TypeB.avsc @@ -0,0 +1,12 @@ +{ + "type": "record", + "name": "TypeB", + "namespace": "testdata", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] +} \ No newline at end of file diff --git a/testdata/collapse_fields/Widget.avsc b/testdata/collapse_fields/Widget.avsc index 0c147bc..dd0969f 100644 --- a/testdata/collapse_fields/Widget.avsc +++ b/testdata/collapse_fields/Widget.avsc @@ -174,6 +174,50 @@ "values": "testdata.StringList" }, "default": {} + }, + { + "name": "a_one_of", + "type": [ + "null", + { + "type": "record", + "name": "AOneOf", + "namespace": "testdata", + "fields": [ + { + "name": "a_oneof", + "type": [ + { + "type": "record", + "name": "TypeA", + "namespace": "testdata", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] + }, + { + "type": "record", + "name": "TypeB", + "namespace": "testdata", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] + } + ], + "default": null + } + ] + } + ], + "default": null } ] } \ No newline at end of file diff --git a/testdata/emit_only/Widget.avsc b/testdata/emit_only/Widget.avsc index ee68a98..5821529 100644 --- a/testdata/emit_only/Widget.avsc +++ b/testdata/emit_only/Widget.avsc @@ -181,6 +181,50 @@ "values": "testdata.StringList" }, "default": {} + }, + { + "name": "a_one_of", + "type": [ + "null", + { + "type": "record", + "name": "AOneOf", + "namespace": "testdata", + "fields": [ + { + "name": "a_oneof", + "type": [ + { + "type": "record", + "name": "TypeA", + "namespace": "testdata", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] + }, + { + "type": "record", + "name": "TypeB", + "namespace": "testdata", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] + } + ], + "default": null + } + ] + } + ], + "default": null } ] } \ No newline at end of file diff --git a/testdata/namespace_map/AOneOf.avsc b/testdata/namespace_map/AOneOf.avsc new file mode 100644 index 0000000..eec9b15 --- /dev/null +++ b/testdata/namespace_map/AOneOf.avsc @@ -0,0 +1,37 @@ +{ + "type": "record", + "name": "AOneOf", + "namespace": "mynamespace", + "fields": [ + { + "name": "a_oneof", + "type": [ + { + "type": "record", + "name": "TypeA", + "namespace": "mynamespace", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] + }, + { + "type": "record", + "name": "TypeB", + "namespace": "mynamespace", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] + } + ], + "default": null + } + ] +} \ No newline at end of file diff --git a/testdata/namespace_map/TypeA.avsc b/testdata/namespace_map/TypeA.avsc new file mode 100644 index 0000000..7f6b883 --- /dev/null +++ b/testdata/namespace_map/TypeA.avsc @@ -0,0 +1,12 @@ +{ + "type": "record", + "name": "TypeA", + "namespace": "mynamespace", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] +} \ No newline at end of file diff --git a/testdata/namespace_map/TypeB.avsc b/testdata/namespace_map/TypeB.avsc new file mode 100644 index 0000000..3e17f35 --- /dev/null +++ b/testdata/namespace_map/TypeB.avsc @@ -0,0 +1,12 @@ +{ + "type": "record", + "name": "TypeB", + "namespace": "mynamespace", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] +} \ No newline at end of file diff --git a/testdata/namespace_map/Widget.avsc b/testdata/namespace_map/Widget.avsc index 3328c41..2d8b85f 100644 --- a/testdata/namespace_map/Widget.avsc +++ b/testdata/namespace_map/Widget.avsc @@ -181,6 +181,50 @@ "values": "mynamespace.StringList" }, "default": {} + }, + { + "name": "a_one_of", + "type": [ + "null", + { + "type": "record", + "name": "AOneOf", + "namespace": "mynamespace", + "fields": [ + { + "name": "a_oneof", + "type": [ + { + "type": "record", + "name": "TypeA", + "namespace": "mynamespace", + "fields": [ + { + "name": "foo", + "type": "string", + "default": "" + } + ] + }, + { + "type": "record", + "name": "TypeB", + "namespace": "mynamespace", + "fields": [ + { + "name": "bar", + "type": "string", + "default": "" + } + ] + } + ], + "default": null + } + ] + } + ], + "default": null } ] } \ No newline at end of file diff --git a/testdata/widget.proto b/testdata/widget.proto index 118a229..98787df 100644 --- a/testdata/widget.proto +++ b/testdata/widget.proto @@ -9,10 +9,26 @@ message StringList { repeated string data = 1; } +message TypeA { + string foo = 1; +} + +message TypeB { + string bar = 2; +} + +message AOneOf { + oneof a_oneof { + TypeA a = 1; + TypeB b = 2; + } +} + message Widget { optional google.protobuf.Timestamp from_date = 1; google.protobuf.Timestamp to_date = 2; Foobar foobar = 3; StringList strings = 4; map string_map = 5; + optional AOneOf a_one_of = 6; }