-
Notifications
You must be signed in to change notification settings - Fork 155
Description
Given the following code:
import avsc from 'avsc';
const userSchemaV1 = { type: 'record', name: 'User', fields: [] };
const userSchemaV2 = { type: 'record', name: 'User', fields: [] };
const opts = {};
avsc.Type.forSchema(userSchemaV1, opts);
avsc.Type.forSchema(userSchemaV2, opts);The code unexpectedly crashes when we try and parse userSchemaV2 with the error: Error: duplicate type name: User.
I looked at the source code and the problem is that forSchema mutates the passed opts to add a registry to it. So it tries to parse the second schema but User is already in the registry, so it crashes. Here's the offending line:
Line 120 in f69d61c
| opts.registry = opts.registry || {}; |
It feels like very confusing behaviour that the top-level opts object is mutated in any way, and in fact has led to a crash for us when using https://github.com/kafkajs/confluent-schema-registry/, because that repo re-uses the options object between calls to forSchema – see https://github.com/kafkajs/confluent-schema-registry/blob/b342f4eb42599447a39c9506ca501e3a59afc7c3/src/cache.ts#L28
Thanks very much. Hope that makes sense, let me know if you need any more information.