Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Byte arrays are represented as strings in generated avro schema? #39

@asmaier

Description

@asmaier

It looks like byte arrays are represented as strings in the generated Avro schema. When I try to generate a schema from my File class below and use that schema to write an Avro message, I get an JsonMappingException: Not in union ["null",{"type":"array","items":"string"}]: java.nio.HeapByteBuffer[pos=0 lim=9 cap=9]:

    private static class File {
        public String filename = "TestFile.txt";
        public byte[] filedata = "Test Text".getBytes();
    }

    @Test
    public void testAvroSchemaGenerationWithJackson() throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper(new AvroFactory());
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        AvroSchemaGenerator visitor = new AvroSchemaGenerator();
        mapper.acceptJsonFormatVisitor(File.class, visitor);
        AvroSchema schema = visitor.getGeneratedSchema();

        ObjectMapper mapper2 = new ObjectMapper(new AvroFactory());
        System.out.println(mapper2.writer(schema).writeValueAsBytes(new File()));

    }

I believe the reason is, because the generated schema represents the byte array as a string:

{
  "type" : "record",
  "name" : "File",
  "namespace" : "xx.xxx.nss",
  "doc" : "Schema for xx.xxx.SchemaGenerationTest$File",
  "fields" : [ {
    "name" : "filedata",
    "type" : [ "null", {
      "type" : "array",
      "items" : "string"
    } ]
  }, {
    "name" : "filename",
    "type" : [ "null", "string" ]
  } ]
}

How can I make byte arrays be represented simply by the type "bytes" ? Or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions