Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add avro_schema_nested field to schema_registry_decode / encode #90

8 changes: 6 additions & 2 deletions internal/impl/confluent/serde_avro.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
refsMap[name] = info.Schema
return nil
}); err != nil {
return "", nil
return "", err
}

var schemaDry any
Expand All @@ -86,8 +86,12 @@
}

var ref reference
maxIterations := 100

for {
for i := 0; i <= maxIterations; i++ {
if i == maxIterations {
return "", fmt.Errorf("maximum iteration limit reached trying to resolve Avro references: possible circular dependency detected")

Check failure on line 93 in internal/impl/confluent/serde_avro.go

View workflow job for this annotation

GitHub Actions / golangci-lint

fmt.Errorf can be replaced with errors.New (perfsprint)
gregfurman marked this conversation as resolved.
Show resolved Hide resolved
}
initialSchemaDry := schemaString

for k, v := range refsMap {
Expand Down
151 changes: 151 additions & 0 deletions internal/impl/confluent/serde_avro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,154 @@ func TestAvroReferencesNested(t *testing.T) {
})
}
}

func TestAvroReferencesNestedCircularDependency(t *testing.T) {
tCtx, done := context.WithTimeout(context.Background(), time.Second*10)
defer done()
userSchema := `
{
"type": "record",
"name": "User",
"namespace": "com.example",
"fields": [
{
"name": "name",
"type": "string"
},
{
"name": "email",
"type": "string"
},
{
"name": "address",
"type": "Address"
}
]
}`

addressSchema := `
{
"type": "record",
"name": "Address",
"namespace": "com.example",
"fields": [
{
"name": "street",
"type": "string"
},
{
"name": "city",
"type": "string"
},
{
"name": "state",
"type": "State"
},
{
"name": "zip",
"type": "string"
}
]
}`

stateSchema := `
{
"type": "record",
"name": "State",
"namespace": "com.example",
"fields": [
{
"name": "state",
"type": "string"
},
{
"name": "address",
"type": "Address"
}
]
}`

userGabs, _ := gabs.ParseJSON([]byte(userSchema))
addressGabs, _ := gabs.ParseJSON([]byte(addressSchema))
stateGabs, _ := gabs.ParseJSON([]byte(stateSchema))

userSchema = userGabs.String()
addressSchema = addressGabs.String()
stateSchema = stateGabs.String()

urlStr := runSchemaRegistryServer(t, func(path string) ([]byte, error) {
switch path {
case "/subjects/com.example.User/versions/latest", "/schemas/ids/1":
return mustJBytes(t, map[string]any{
"id": 1,
"version": 1,
"schema": userSchema,
"schemaType": "AVRO",
"references": []any{
map[string]any{"name": "com.example.Address", "subject": "com.example.Address", "version": 1},
},
}), nil
case "/subjects/com.example.Address/versions/1", "/schemas/ids/2":
return mustJBytes(t, map[string]any{
"id": 1,
"version": 1,
"schema": addressSchema,
"schemaType": "AVRO",
"references": []any{
map[string]any{"name": "com.example.State", "subject": "com.example.State", "version": 1},
},
}), nil
case "/subjects/com.example.State/versions/1", "/schemas/ids/3":
return mustJBytes(t, map[string]any{
"id": 1,
"version": 1,
"schema": stateSchema,
"schemaType": "AVRO",
"references": []any{
map[string]any{"name": "com.example.Address", "subject": "com.example.Address", "version": 1},
},
}), nil
}
return nil, nil
})

subj, err := service.NewInterpolatedString("com.example.User")
require.NoError(t, err)

tests := []struct {
name string
input string
output string
errContains []string
}{
{
name: "a foo",
input: `{"hello":"world"}`,
output: `{}`,
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
encoder, err := newSchemaRegistryEncoder(urlStr, noopReqSign, nil, subj, true, true, time.Minute*10, time.Minute, service.MockResources())
require.NoError(t, err)

t.Cleanup(func() {
_ = encoder.Close(tCtx)
})

inMsg := service.NewMessage([]byte(test.input))
encodedMsgs, err := encoder.ProcessBatch(tCtx, service.MessageBatch{inMsg})

require.NoError(t, err)
require.Len(t, encodedMsgs, 1)
require.Len(t, encodedMsgs[0], 1)

encodedMsg := encodedMsgs[0][0]
require.Error(t, encodedMsg.GetError())
require.Equal(t, "maximum iteration limit reached trying to resolve Avro references: possible circular dependency detected", encodedMsg.GetError().Error())

})
}
}
Loading