mostafa/xk6-kafka

Type Map not working with Avro schema

tunovic opened this issue · 3 comments

Hi,

I am using k6 with xk6-kafka extension with a Avro schema containing map type:

import {
    Writer,
    SchemaRegistry,
    SCHEMA_TYPE_AVRO,
} from "k6/x/kafka";

const writer = new Writer({
    brokers: [`127.0.0.1:9092`],
    topic: "Event",
});

const schemaRegistry = new SchemaRegistry();

const valueSchema = `{
  "name": "Event",
  "type": "record",
  "namespace": "event",
  "fields": [
    {
      "name": "id",
      "type": "string"
    },
    {
      "name": "externalIds",
      "type": {
        "type": "map",
        "values": [
          "null",
          "string"
        ]
      }
    }
  ]
}`;

const valueData = JSON.parse(`{
  "id": "1",
  "externalIds": {
    "ID": "13c1a283a5e463a05f70f9e104334b1616ee3b73877384111cd215bad77cd707"
  }
}`);

export function setup() {
    return { index: 1 };
}

export default function (data) {
    writer.produce({
        messages: [
            {
                value: schemaRegistry.serialize({
                    data: valueData,
                    schema: { schema: valueSchema },
                    schemaType: SCHEMA_TYPE_AVRO,
                }),
            },
        ],
    });
}

export function teardown(data) {
    writer.close();
}

When I execute this test script, it fails with following error:

ERRO[0000] GoError: Failed to encode data, OriginalError: %!w(*errors.errorString=&{cannot decode textual record "event.Event": cannot decode textual union: expected: '{'; actual: '"' for key: "ID" for key: "externalIds"})
	at github.com/mostafa/xk6-kafka.(*Kafka).schemaRegistryClientClass.func4 (native)

Is the type map not working or am I doing something wrong here?

looks to be related to this: linkedin/goavro#265

Hey @tunovic,

Since you're using a union type inside your map, you must specify the type on serialization. Please read the following comment and see if it works for you:

Thank you @mostafa,

This worked.