riferrei / srclient

Golang Client for Schema Registry
Apache License 2.0
235 stars 70 forks source link

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

Open slice-rishabhg opened 2 months ago

slice-rishabhg commented 2 months ago

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 stringjson:"uuid" Name stringjson:"name" }

  1. 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.