mtth / avsc

Avro for JavaScript :zap:
MIT License
1.28k stars 148 forks source link

schema resolution issue #383

Closed bellomimarc closed 2 years ago

bellomimarc commented 2 years ago

Hi! I encounter a problem in this script:

const avro = require('avsc')

const typeNew = avro.Type.forSchema({
    "name": "s",
    "type": "record",
    "fields": [
        {
            "name": "id",
            "type": "int"
        },
        {
            "name": "newField",
            "type": "string"
        }
    ]
})
const type = avro.Type.forSchema({
    "name": "s",
    "type": "record",
    "fields": [
        {
            "name": "id",
            "type": "int"
        }
    ]
})

const sampleEvent = {
    id: 5,
    newField: "test"
}

const buf = typeNew.toBuffer(sampleEvent);

const val = type.fromBuffer(buf);

console.log(val)

Based on avro schema resolution, I expect that I can do that but I'm receiving an error "trailing data" when I call the fromBuffer function. Why does this occur?

mtth commented 2 years ago

Hi @bellomimarc. You need to provide a resolver to use schema resolution. In your example it would look something like:

const resolver = type.createResolver(newType);
const val = type.fromBuffer(buf, resolver);

See also https://github.com/mtth/avsc/wiki/Advanced-usage#schema-evolution for more context.

bellomimarc commented 2 years ago

Thanks!