Skip to content

Commit

Permalink
fix: union reader schema resolution (#358)
Browse files Browse the repository at this point in the history
fix: resolve composite schema for Union reader
  • Loading branch information
redaLaanait authored Mar 8, 2024
1 parent 979cff3 commit 1cb8acd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions schema_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ func (c *SchemaCompatibility) resolve(reader, writer Schema) (schema Schema, res
if writer.Type() != reader.Type() {
if reader.Type() == Union {
for _, schema := range reader.(*UnionSchema).Types() {
// Compatibility is not guaranteed for every Union reader schema.
// Therefore, we need to check compatibility in every iteration.
if err := c.compatible(schema, writer); err != nil {
continue
}
sch, _, err := c.resolve(schema, writer)
if err != nil {
continue
Expand Down
41 changes: 41 additions & 0 deletions schema_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,3 +862,44 @@ func TestSchemaCompatibility_ResolveWithRefs(t *testing.T) {
want := map[string]any{"a": "foo"}
assert.Equal(t, want, result)
}

func TestSchemaCompatibility_ResolveWithComplexUnion(t *testing.T) {
r := avro.MustParse(`[
{
"type":"record",
"name":"testA",
"aliases": ["test1"],
"namespace": "org.hamba.avro",
"fields":[{"name": "a", "type": "long"}]
},
{
"type":"record",
"name":"testB",
"aliases": ["test2"],
"namespace": "org.hamba.avro",
"fields":[{"name": "b", "type": "bytes"}]
}
]`)

w := avro.MustParse(`{
"type":"record",
"name":"test2",
"namespace": "org.hamba.avro",
"fields":[{"name": "b", "type": "string"}]
}`)

value := map[string]any{"b": "foo"}
b, err := avro.Marshal(w, value)
assert.NoError(t, err)

sc := avro.NewSchemaCompatibility()
sch, err := sc.Resolve(r, w)
assert.NoError(t, err)

var result any
err = avro.Unmarshal(sch, b, &result)
assert.NoError(t, err)

want := map[string]any{"b": []byte("foo")}
assert.Equal(t, want, result)
}

0 comments on commit 1cb8acd

Please sign in to comment.