|
| 1 | +package com.fasterxml.jackson.dataformat.protobuf.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.core.JsonParser; |
| 5 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 6 | +import com.fasterxml.jackson.dataformat.protobuf.*; |
| 7 | +import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema; |
| 8 | +import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader; |
| 9 | + |
| 10 | +// [dataformats-binary#202] |
| 11 | +public class ReadUnknownFields202Test extends ProtobufTestBase |
| 12 | +{ |
| 13 | + // [dataformats-binary#202] |
| 14 | + static class TestMessageV0 |
| 15 | + { |
| 16 | + @JsonProperty(required = true, index = 1) |
| 17 | + private String id; |
| 18 | + @JsonProperty(required = false, index = 2) |
| 19 | + private String plant; |
| 20 | + |
| 21 | + public TestMessageV0() { } |
| 22 | + |
| 23 | + public TestMessageV0(String id, String plant) { |
| 24 | + this.id = id; |
| 25 | + this.plant = plant; |
| 26 | + } |
| 27 | + |
| 28 | + public String getId() { |
| 29 | + return id; |
| 30 | + } |
| 31 | + |
| 32 | + public String getPlant() { |
| 33 | + return plant; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + static class TestMessageV1 |
| 38 | + { |
| 39 | + @JsonProperty(required = true, index = 1) |
| 40 | + private String id; |
| 41 | + @JsonProperty(required = false, index = 2) |
| 42 | + private String plant; |
| 43 | + @JsonProperty(required = false, index = 3) |
| 44 | + private Double length; |
| 45 | + @JsonProperty(required = false, index = 4) |
| 46 | + private Double width; |
| 47 | + @JsonProperty(required = false, index = 5) |
| 48 | + private String descr; |
| 49 | + @JsonProperty(required = false, index = 6) |
| 50 | + private String source; |
| 51 | + |
| 52 | + public TestMessageV1() { } |
| 53 | + |
| 54 | + public TestMessageV1(String id, String plant, Double length) { |
| 55 | + this.id = id; |
| 56 | + this.plant = plant; |
| 57 | + this.length = length; |
| 58 | + } |
| 59 | + |
| 60 | + public String getId() { |
| 61 | + return id; |
| 62 | + } |
| 63 | + |
| 64 | + public String getPlant() { |
| 65 | + return plant; |
| 66 | + } |
| 67 | + |
| 68 | + public Double getLength() { |
| 69 | + return length; |
| 70 | + } |
| 71 | + |
| 72 | + public Double getWidth() { |
| 73 | + return width; |
| 74 | + } |
| 75 | + |
| 76 | + public String getDescr() { |
| 77 | + return descr; |
| 78 | + } |
| 79 | + |
| 80 | + public String getSource() { |
| 81 | + return source; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // [dataformats-binary#202] |
| 86 | + private static final String MESSAGE_V0_SCHEMA = |
| 87 | + "message TestMessageV0 {\n" |
| 88 | + + "required string id = 1;\n" |
| 89 | + + "optional string plant = 2;\n" |
| 90 | + +"}"; |
| 91 | + |
| 92 | + private static final String MESSAGE_V1_SCHEMA = |
| 93 | + "message TestMessageV1 {\n" + |
| 94 | + "required string id = 1;\n" + |
| 95 | + "optional double length = 3;\n" + |
| 96 | + "optional double width = 4;\n" + |
| 97 | + "optional string source = 6;\n" + |
| 98 | + "optional string descr = 5;\n" + |
| 99 | + "optional string plant = 2;\n" + |
| 100 | + "}"; |
| 101 | + |
| 102 | + /* |
| 103 | + /********************************************************** |
| 104 | + /* Test methods |
| 105 | + /********************************************************** |
| 106 | + */ |
| 107 | + |
| 108 | + // [dataformats-binary#202] |
| 109 | + public void testV1toV0() throws Exception { |
| 110 | + final ProtobufMapper MAPPER = newMapperBuilder() |
| 111 | + .enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) |
| 112 | + .enable(JsonParser.Feature.IGNORE_UNDEFINED) |
| 113 | + .build(); |
| 114 | + |
| 115 | + TestMessageV1 messageV1 = new TestMessageV1("1", "test", 9.9); |
| 116 | + ProtobufSchema schemaV1 = ProtobufSchemaLoader.std.parse(MESSAGE_V1_SCHEMA); |
| 117 | + |
| 118 | + byte[] protobufData = MAPPER |
| 119 | + .writer(schemaV1) |
| 120 | + .writeValueAsBytes(messageV1); |
| 121 | + |
| 122 | + ProtobufSchema schemaV0 = ProtobufSchemaLoader.std.parse(MESSAGE_V0_SCHEMA); |
| 123 | + TestMessageV0 messageV0 = MAPPER |
| 124 | + .readerFor(TestMessageV0.class) |
| 125 | + .with(schemaV0) |
| 126 | + .with(JsonParser.Feature.IGNORE_UNDEFINED) |
| 127 | + .readValue(protobufData); |
| 128 | + |
| 129 | + assertEquals(messageV1.getId(), messageV0.getId()); |
| 130 | + assertEquals(messageV1.getPlant(), messageV0.getPlant()); |
| 131 | + } |
| 132 | +} |
0 commit comments