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

Type Map not working with Avro schema #241

Closed
tunovic opened this issue Aug 1, 2023 · 3 comments
Closed

Type Map not working with Avro schema #241

tunovic opened this issue Aug 1, 2023 · 3 comments

Comments

@tunovic
Copy link

tunovic commented Aug 1, 2023

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?

@bram209
Copy link

bram209 commented Aug 1, 2023

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

@mostafa
Copy link
Owner

mostafa commented Aug 1, 2023

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:

@tunovic
Copy link
Author

tunovic commented Aug 1, 2023

Thank you @mostafa,

This worked.

@tunovic tunovic closed this as completed Aug 1, 2023
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

3 participants