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

Support for Union of Complex Types #340

Closed
sj-freitas opened this issue Mar 26, 2021 · 2 comments
Closed

Support for Union of Complex Types #340

sj-freitas opened this issue Mar 26, 2021 · 2 comments
Labels

Comments

@sj-freitas
Copy link

Hello, first of all, thanks for the library!

Currently we found that in the avro-schema reference there is support for multiple complex union types, however using avsc we found that schemas such as this:

const employeeSchema = avro.Type.forSchema({
  name: 'Employee',
  type: 'record',
  fields: [
    { name: 'name', type: 'string' },
    {
      name: 'role',
      type: [
        {
          name: 'Manager',
          type: 'record',
          fields: [
            {
              name: 'managees',
              type: { type: 'array', items: 'string' },
            },
          ],
        },
        {
          name: 'Developer',
          type: 'record',
          fields: [{ name: 'programmingLanguage', type: 'string' }],
        },
      ],
    },
  ],
});

When we do the following:

const employee = {
  name: 'Alice',
  role: {
    programmingLanguage: 'Typescript',
  },
};

// Throws an exception!
const buffered = employeeSchema.toBuffer(dummyValue);

We get this error message:
invalid [{"name":"Manager","type":"record","fields":[{"name":"managees","type":{"type":"array","items":"string"}}]},{"name":"Developer","type":"record","fields":[{"name":"programmingLanguage","type":"string"}]}]: {"programmingLanguage":"Typescript"}

We'd like to know if this is a problem with our interpretation of the documentation of the avro-schema or if there's any workaround that we can do to support union complex types.

@mtth
Copy link
Owner

mtth commented Mar 27, 2021

Hi @sj-freitas. The issue you're bumping into is that unions with multiple records need to be wrapped to ensure that they don't lose branch information. For your example, you'll need to tweak the data as follows:

const employee = {
  name: 'Alice',
  role: {
    Developer: { // Note this extra key, matching the branch's record's name.
      programmingLanguage: 'TypeScript',
    },
  },
};

Take a look at this comment for more context as well as a couple examples.

@mtth mtth added the question label Mar 27, 2021
@sj-freitas
Copy link
Author

Thank you very much! I looked into the logicalTypes and I managed to find a solution!

@mtth mtth closed this as completed Mar 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants