|
| 1 | +package com.fasterxml.jackson.dataformat.avro.schemaev; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.avro.AvroSchema; |
| 6 | +import com.fasterxml.jackson.dataformat.avro.AvroTestBase; |
| 7 | + |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class ErrorReproduceTest extends AvroTestBase { |
| 12 | + |
| 13 | + static String SCHEMA_V1 = aposToQuotes("{\n" + |
| 14 | + "'type' : 'record',\n" + |
| 15 | + "'name': 'V1',\n" + |
| 16 | + "'namespace': 'test',\n" + |
| 17 | + "'doc': 'simple schema testing compatibility',\n" + |
| 18 | + "'fields': [\n" + |
| 19 | + "{ 'name': 'name', 'type': 'string', 'doc': 'object name' }\n" + |
| 20 | + "]}"); |
| 21 | + |
| 22 | + static String SCHEMA_V2 = aposToQuotes("{\n" + |
| 23 | + "'type' : 'record',\n" + |
| 24 | + "'name': 'V1',\n" + |
| 25 | + "'namespace': 'test',\n" + |
| 26 | + "'doc': 'simple schema testing compatibility',\n" + |
| 27 | + "'fields': [\n" + |
| 28 | + "{ 'name': 'name', 'type': 'string', 'doc': 'object name' },\n" + |
| 29 | + "{ 'name': 'label', 'type': [ 'null', { 'type' : 'array', 'items' : 'string' } ] }\n" + |
| 30 | + "]}"); |
| 31 | + |
| 32 | + static class DataV1 { |
| 33 | + public String name; |
| 34 | + |
| 35 | + protected DataV1() { |
| 36 | + } |
| 37 | + |
| 38 | + public DataV1(String name) { |
| 39 | + this.name = name; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static class DataV2 { |
| 44 | + public String name; |
| 45 | + public List<String> label; |
| 46 | + |
| 47 | + public DataV2(String name, List<String> label) { |
| 48 | + this.name = name; |
| 49 | + this.label = label; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private final ObjectMapper MAPPER = getMapper() |
| 54 | + .configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false) |
| 55 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 56 | + |
| 57 | + public void testShouldDeserialize() throws Exception { |
| 58 | + final AvroSchema srcSchema = getMapper().schemaFrom(SCHEMA_V2); |
| 59 | + final AvroSchema dstSchema = getMapper().schemaFrom(SCHEMA_V1); |
| 60 | + final AvroSchema xlate = srcSchema.withReaderSchema(dstSchema); |
| 61 | + |
| 62 | + // receive event with new schema |
| 63 | + DataV2 event = new DataV2("test", null); |
| 64 | + byte[] bytes = getMapper().writer(getMapper().schemaFrom(SCHEMA_V2)).writeValueAsBytes(event); |
| 65 | + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); |
| 66 | + |
| 67 | + //now I want to deserialize |
| 68 | + DataV1 v1 = MAPPER |
| 69 | + .readerFor(DataV1.class) |
| 70 | + .with(xlate) |
| 71 | + .readValue(stream); |
| 72 | + } |
| 73 | +} |
0 commit comments