Skip to content

Missing Field-Level Error Context in Avro Serialization for String types #364

@arav1895agy

Description

@arav1895agy

Environment Information

  • OS: Ubuntu 20.04.6 LTS
  • Node Version : 18.20.6
  • NPM Version : 10.8.2
  • confluent-kafka-javascript version: 1.4.0

Steps to Reproduce

When a schema defines a field of type string without a default value, and the payload being serialized omits that field, serialization fails with the error invalid "string": undefined. Unfortunately, the error message doesn’t indicate which field caused the violation, making it very hard to troubleshoot in production when working with large schemas.

After raising this with the AVSC project (see issue #502
), their recommendation was to call isValid() method with an errorHook() before invoking toBuffer() to give detailed information on path.

Below is the error message with the current implementation:

invalid "string": undefined
at throwInvalidError (node_modules/avsc/lib/types.js:3042:9)
at StringType._write (node_modules/avsc/lib/types.js:1083:5)
at RecordType.writeRandomTest [as _write] (eval at RecordType._createWriter (node_modules/avsc/lib/types.js:2343:10), :4:6)
at RecordType.Type.toBuffer (node_modules/avsc/lib/types.js:658:8)
at AvroSerializer.serialize (node_modules/@confluentinc/schemaregistry/dist/serde/avro.js:97:33)
at Object. (avro2.test.ts:37:5)

The test code below illustrates the issue:

import { describe, it } from '@jest/globals';
import {
  AvroSerializer,
  ClientConfig,
  SchemaRegistryClient,
  SerdeType
} from "@confluentinc/schemaregistry";

const testSchema = `
{
    "type": "record",
    "namespace": "examples",
    "name": "RandomTest",
    "fields": [
        { "name": "fullName", "type": "string" },
        { "name": "lastName", "type": "string" }
    ],
  "version": "1"
}`;

const baseURL = 'mock://';
const topic = 'topic1';
const subject = topic + '-value';

describe('AvroSerializer', () => {
  it('string undefined', async () => {
    const conf: ClientConfig = {
      baseURLs: [baseURL]
    };
    const client = SchemaRegistryClient.newClient(conf);
    const ser = new AvroSerializer(client, SerdeType.VALUE, { useLatestVersion: true });
    const info = {
      schemaType: 'AVRO',
      schema: testSchema
    };
    await client.register(subject, info, false);
    await ser.serialize(topic, { lastName: "lastName" });
  });
});

Please provide a build including this fix to help with better troubleshooting.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions