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

Error in Serializing Avro Union Type ["null", "string"] #112

Open
slice-rishabhg opened this issue Sep 13, 2024 · 1 comment
Open

Error in Serializing Avro Union Type ["null", "string"] #112

slice-rishabhg opened this issue Sep 13, 2024 · 1 comment

Comments

@slice-rishabhg
Copy link

slice-rishabhg commented Sep 13, 2024

Summary
When attempting to serialize an Avro union type ["null", "string"] using the Schema Registry client, an error is thrown during the NativeFromTextual conversion process. The error encountered is as follows
NativeFromTextual conversion failed: cannot decode textual record "com.example.audit.AuditEvent": cannot decode textual union: expected: '{'; actual: '"' for key: "name"
Steps to Reproduce

  1. Define an Avro schema with a union type (["null", "string"]) for the name field:

{ "fields": [ { "name": "uuid", "type": "string" }, { "default": null, "name": "name", "type": ["null", "string"] } ], "name": "AuditEvent", "namespace": "com.example.audit", "type": "record" }

  1. Create a DTO in GO

type AuditTrailEventData struct { UUID string json:"uuid"Name stringjson:"name"}
3. Try to produce an event using the following code

`value, err := json.Marshal(dto)
if err != nil {
slogger.GetInstance().WithContext(ctx).Errorf("Failed to marshal dto: %s", err)
return err
}

// Convert JSON to native Avro format
native, _, navErr := schema.Codec().NativeFromTextual(value)
if navErr != nil {
	slogger.GetInstance().WithContext(ctx).Errorf("NativeFromTextual conversion failed: %s", navErr)
	return navErr
}

// Convert the native Avro format to binary
valueBytes, err := schema.Codec().BinaryFromNative(nil, native)
if err != nil {
	slogger.GetInstance().WithContext(ctx).Errorf("BinaryFromNative conversion failed: %s", err)
	return err
}

// Create the record value
recordValue := append([]byte{0}, schemaIDBytes...)
recordValue = append(recordValue, valueBytes...)

`
I would appreciate guidance or a fix for handling union types with Avro serialization in Go.

@AtakanColak
Copy link
Collaborator

Hello, I'm afraid I don't have any availabiltiy to work on this but I'd point you to the underlying goavro package that we are using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants