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

LogicalType does not get ingested into the schema when using Type.forSchema() #493

Open
jtmarks6 opened this issue Feb 4, 2025 · 4 comments

Comments

@jtmarks6
Copy link

jtmarks6 commented Feb 4, 2025

I am trying to load a schema that has a timestamp field that uses logicalType: 'timestamp-millis' and can not get the logicalType to stay in the schema when loaded.

Here is a test case I wrote in my repo to show the issue:

it.only(`logicalType should be loaded into schema`, async () => {
    const avsc = require('avsc')
    const schema = {
        type: 'record',
        name: 'record_name',
        fields: [
            {
                name: 'timestamp_field',
                type: [
                    'null',
                    { type: 'long', logicalType: 'timestamp-millis' },
                ],
                default: null,
            },
        ],
        namespace: 'name.space',
    }

    const expectedSchema = {
        type: 'record',
        fields: [
            {
                name: 'timestamp_field',
                type: [
                    'null',
                    { type: 'long', logicalType: 'timestamp-millis' },
                ],
            },
        ],
        name: 'name.space.record_name',
    }

    const type = avsc.Type.forSchema(schema)
    expect(type.schema()).to.equal(expectedSchema)
})

type.schema() outputs this schema:

{
    type: 'record',
    fields: [
        {
            name: 'timestamp_field',
            type: [
                'null',
                'long'
            ],
        },
    ],
    name: 'name.space.record_name',
}
@jtmarks6 jtmarks6 changed the title LogicalType does get ingested into the schema when using Type.forSchema() LogicalType does not get ingested into the schema when using Type.forSchema() Feb 4, 2025
@mtth
Copy link
Owner

mtth commented Feb 5, 2025

Hi @jtmarks6. By default type.schema() returns the type's canonical schema. You can set the exportAttrs option to include other attributes, including logical type.

@urugator
Copy link

urugator commented Feb 11, 2025

Hi, maybe there is a bug? The following:

const type = avro.Type.forSchema(  
  {
    namespace: 'ns',
    type: 'record',
    name: 'foo',
    version: 1,
    fields: [
      {
        name: 'bar',
        type: { type: 'long', logicalType: 'local-timestamp-millis' },
      },
    ],
  },
);
console.log(type.schema({ exportAttrs: true }));

produces

{
  name: 'ns.foo',
  type: 'record',
  fields: [ { name: 'bar', type: 'long' } ]
}

@mtth
Copy link
Owner

mtth commented Feb 13, 2025

@urugator - this is WAI, actually. I forgot to mention that the logical type must be used. Here there are no constructors passed in when parsing the schema, so the logicalType key is ignored. Unknown keys are similarly ignored - if you want the original schema used to create a type, you will need to keep a reference to it separately. This design was chosen to avoid inconsistencies due to mutability.

@urugator
Copy link

urugator commented Feb 13, 2025

I see, thank you. To elaborate a bit, I attempted to use @kafka/confluent-schema-registry to obtain the original schema from registry, but unfortunately it returns directly the avsc.Type instance. I tried to work around it using .schema(), but in the end I ditched the library and call the API directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants