mtth / avsc

Avro for JavaScript :zap:
MIT License
1.27k stars 147 forks source link

"Error: trailing data" when using custom Long type in Avro with KafkaJS Confluent Schema Registry #464

Open floriansollami opened 1 month ago

floriansollami commented 1 month ago

I'm encountering an issue when trying to implement a custom type for Long using the __with method. Here's the relevant code snippet:

import Long from 'long';

const longType = avro.types.LongType.__with(
  {
    fromBuffer: (buf: Buffer) => {
      return new Long(buf.readInt32LE(), buf.readInt32LE(4));
    },
    toBuffer: (n: { getLowBits: () => number; getHighBits: () => number }) => {
      const buf = Buffer.alloc(8);

      buf.writeInt32LE(n.getLowBits());
      buf.writeInt32LE(n.getHighBits(), 4);
      return buf;
    },
    fromJSON: Long.fromValue,
    toJSON: (n: any) => +n,
    isValid: Long.isLong,
    compare: (n1: any, n2: any) => n1.compare(n2),
  },
  false,
);

I am then supplying this custom type within the typeHook for the KafkaJS Confluent Schema Registry as follows:

this.registry = new SchemaRegistry(
  {
    ...config,
    host: config?.host ?? 'http://localhost:8081',
  },
  {
    [SchemaType.AVRO]: {
      ...avroOptions,
      registry: {
        long: longType,
      },
      typeHook: (
        schema: Schema,
        opts: ForSchemaOptions,
      ): Type | undefined => {
        return avro.Type.forSchema('long', {
          registry: { long: longType },
        });
      },
    },
  },
);

However, when attempting to deserialize data, I receive the following error:

"Error: trailing data", "at Type.fromBuffer (/Documents/GitHub/repository-v2/node_modules/avsc/lib/types.js:614:11)"

It appears that the custom Long type's fromBuffer or toBuffer methods might be causing this issue by not correctly handling the buffer data. @mtth

mtth commented 3 weeks ago

Hi @floriansollami. The typehook above is causing all types (even non-longs) to be mapped to a longType - that's probably not what you want. Can you try omitting the hook?