|
| 1 | +package com.fasterxml.jackson.dataformat.protobuf; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.*; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema; |
| 8 | + |
| 9 | +public class ReadUnkownFieldsTest extends ProtobufTestBase |
| 10 | +{ |
| 11 | + static class OneField { |
| 12 | + |
| 13 | + @JsonProperty(value = "f3", index = 3) |
| 14 | + private int f3; |
| 15 | + |
| 16 | + public int getF3() { |
| 17 | + return f3; |
| 18 | + } |
| 19 | + |
| 20 | + public void setF3(int f3) { |
| 21 | + this.f3 = f3; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + static class ThreeField { |
| 26 | + |
| 27 | + @JsonProperty(value = "f1", index = 1) |
| 28 | + private int f1; |
| 29 | + |
| 30 | + @JsonProperty(value = "f2", index = 2) |
| 31 | + private int f2; |
| 32 | + |
| 33 | + @JsonProperty(value = "f3", index = 3) |
| 34 | + private int f3; |
| 35 | + |
| 36 | + public int getF1() { |
| 37 | + return f1; |
| 38 | + } |
| 39 | + |
| 40 | + public void setF1(int f1) { |
| 41 | + this.f1 = f1; |
| 42 | + } |
| 43 | + |
| 44 | + public int getF2() { |
| 45 | + return f2; |
| 46 | + } |
| 47 | + |
| 48 | + public void setF2(int f2) { |
| 49 | + this.f2 = f2; |
| 50 | + } |
| 51 | + |
| 52 | + public int getF3() { |
| 53 | + return f3; |
| 54 | + } |
| 55 | + |
| 56 | + public void setF3(int f3) { |
| 57 | + this.f3 = f3; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /* |
| 62 | + /********************************************************** |
| 63 | + /* Test methods |
| 64 | + /********************************************************** |
| 65 | + */ |
| 66 | + |
| 67 | + final ProtobufMapper MAPPER = new ProtobufMapper(); |
| 68 | + |
| 69 | + public void testMultipleUnknown() throws Exception |
| 70 | + { |
| 71 | + ProtobufMapper mapper = new ProtobufMapper(); |
| 72 | + |
| 73 | + ThreeField threeField = new ThreeField(); |
| 74 | + threeField.setF1(1); |
| 75 | + threeField.setF2(2); |
| 76 | + threeField.setF3(3); |
| 77 | + |
| 78 | + ProtobufSchema schemaWith3 = MAPPER.generateSchemaFor(ThreeField.class); |
| 79 | + byte[] in = mapper.writerFor(ThreeField.class).with(schemaWith3) |
| 80 | + .writeValueAsBytes(threeField); |
| 81 | + |
| 82 | + ProtobufSchema schemaWith1 = MAPPER.generateSchemaFor(OneField.class); |
| 83 | + OneField oneField = mapper.readerFor(OneField.class).with(schemaWith1) |
| 84 | + // important: skip through unknown |
| 85 | + .with(JsonParser.Feature.IGNORE_UNDEFINED) |
| 86 | + .readValue(in); |
| 87 | + |
| 88 | + assertEquals(threeField.getF3(), oneField.getF3()); |
| 89 | + } |
| 90 | +} |
0 commit comments