Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-2418

BUG in schema resolver, when resolving map<> to union {null, map<>}

VotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 1.9.0
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      Here is unit test to reproduce the issue:

      package org.apache.avro.io.parsing;
      
      import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.util.HashMap;
      import java.util.Map;
      import org.junit.Assert;
      import org.apache.avro.Schema;
      import org.apache.avro.SchemaBuilder;
      import org.apache.avro.generic.GenericData;
      import org.apache.avro.generic.GenericDatumReader;
      import org.apache.avro.generic.GenericDatumWriter;
      import org.apache.avro.generic.GenericRecord;
      import org.apache.avro.generic.GenericRecordBuilder;
      import org.apache.avro.io.DatumReader;
      import org.apache.avro.io.DatumWriter;
      import org.apache.avro.io.Decoder;
      import org.apache.avro.io.DecoderFactory;
      import org.apache.avro.io.Encoder;
      import org.apache.avro.io.EncoderFactory;
      import org.apache.avro.util.Utf8;
      import org.junit.Test;
      
      /**
       *
       * @author Zoltan Farkas
       */
      public class TestUnionPromotion {
      
        @Test
        public void testUnionPromotionCollection() throws Exception {
          Schema directFieldSchema = SchemaBuilder.record("MyRecord").namespace("ns").fields().name("field1").type().map()
              .values().stringType().noDefault().endRecord();
          Schema schemaWithField = SchemaBuilder.record("MyRecord").namespace("ns").fields().name("field1").type().nullable()
              .map().values().stringType().noDefault().endRecord();
          Map<String, String> data = new HashMap<>();
          data.put("a", "someValue");
          GenericData.Record record = new GenericRecordBuilder(directFieldSchema).set("field1", data).build();
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          writeAvroBin(bos, record);
          Object read = readAvroBin(new ByteArrayInputStream(bos.toByteArray()), directFieldSchema, schemaWithField);
          Map name = (Map) ((GenericRecord) read).get("field1");
          Assert.assertEquals("someValue", name.get(new Utf8("a")).toString());
      
        }
      
        private static Object readAvroBin(final InputStream input, final Schema writerSchema, final Schema readerSchema)
            throws IOException {
          DatumReader reader = new GenericDatumReader(writerSchema, readerSchema);
          DecoderFactory decoderFactory = DecoderFactory.get();
          Decoder decoder = decoderFactory.binaryDecoder(input, null);
          return reader.read(null, decoder);
        }
      
        private static void writeAvroBin(final OutputStream out, final GenericRecord req) throws IOException {
          @SuppressWarnings("unchecked")
          DatumWriter writer = new GenericDatumWriter(req.getSchema());
          Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
          writer.write(req, encoder);
          encoder.flush();
        }
      
      }
      

        Attachments

        Issue Links

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              zolyfarkas Zoltan Farkas

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment