|
1 | 1 | package com.fasterxml.jackson.dataformat.protobuf;
|
2 | 2 |
|
3 |
| -import com.fasterxml.jackson.annotation.JsonProperty; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
4 | 5 |
|
| 6 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
5 | 8 | import com.fasterxml.jackson.core.JsonParser;
|
6 |
| - |
7 | 9 | import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
|
8 | 10 |
|
9 |
| -// [dataformats-binary#108] |
10 | 11 | public class ReadNestedUnknownFieldsTest extends ProtobufTestBase
|
11 | 12 | {
|
| 13 | + // [dataformats-binary#108] |
12 | 14 | public static class LessNestedField {
|
13 | 15 | @JsonProperty(value = "f1", index = 1)
|
14 | 16 | private NestedOneField f1;
|
@@ -75,32 +77,107 @@ public void setNested2(int nested2) {
|
75 | 77 | }
|
76 | 78 | }
|
77 | 79 |
|
| 80 | + // [dataformats-binary#126] |
| 81 | + @JsonPropertyOrder({"embed", "state"}) |
| 82 | + public static class OuterV2 { |
| 83 | + @JsonProperty("embed") |
| 84 | + public EmbedV2 embed; |
| 85 | + @JsonProperty("state") |
| 86 | + public String state; |
| 87 | + } |
| 88 | + |
| 89 | + @JsonPropertyOrder({"embed", "state"}) |
| 90 | + public static class Outer { |
| 91 | + @JsonProperty("embed") |
| 92 | + public Embed embed; |
| 93 | + @JsonProperty("state") |
| 94 | + public String state; |
| 95 | + } |
| 96 | + |
| 97 | + @JsonPropertyOrder({"a", "b", "c", "extraField"}) |
| 98 | + public static class EmbedV2 { |
| 99 | + @JsonProperty("a") |
| 100 | + public String a; |
| 101 | + @JsonProperty("b") |
| 102 | + public String b; |
| 103 | + @JsonProperty("c") |
| 104 | + public List<String> c; |
| 105 | + @JsonProperty("extraField") |
| 106 | + public String extraField; |
| 107 | + } |
| 108 | + |
| 109 | + @JsonPropertyOrder({"a", "b", "c"}) |
| 110 | + public static class Embed { |
| 111 | + @JsonProperty("a") |
| 112 | + public String a; |
| 113 | + @JsonProperty("b") |
| 114 | + public String b; |
| 115 | + @JsonProperty("c") |
| 116 | + public List<String> c; |
| 117 | + } |
| 118 | + |
78 | 119 | /*
|
79 | 120 | /**********************************************************
|
80 | 121 | /* Test methods
|
81 | 122 | /**********************************************************
|
82 | 123 | */
|
83 | 124 |
|
84 |
| - final ProtobufMapper MAPPER = new ProtobufMapper(); |
| 125 | + private final ProtobufMapper MAPPER = new ProtobufMapper(); |
| 126 | + |
| 127 | + // [dataformats-binary#108] |
| 128 | + public void testMultipleUnknown() throws Exception |
| 129 | + { |
| 130 | + MoreNestedField moreNestedField = new MoreNestedField(); |
| 131 | + NestedTwoField nestedTwoField = new NestedTwoField(); |
| 132 | + nestedTwoField.setNested1(1); |
| 133 | + nestedTwoField.setNested2(2); |
| 134 | + moreNestedField.setF1(nestedTwoField); |
| 135 | + |
| 136 | + byte[] in = MAPPER.writerFor(MoreNestedField.class) |
| 137 | + .with(MAPPER.generateSchemaFor(MoreNestedField.class)) |
| 138 | + .writeValueAsBytes(moreNestedField); |
| 139 | + |
| 140 | + LessNestedField lesser = MAPPER.readerFor(LessNestedField.class) |
| 141 | + .with(MAPPER.generateSchemaFor(LessNestedField.class)) |
| 142 | + // important: skip through unknown |
| 143 | + .with(JsonParser.Feature.IGNORE_UNDEFINED) |
| 144 | + .readValue(in); |
| 145 | + |
| 146 | + assertEquals(moreNestedField.getF1().getNested2(), lesser.getF1().getNested2()); |
| 147 | + } |
| 148 | + |
| 149 | + // [dataformats-binary#126] |
| 150 | + public void testCheckEndAfterSkip() throws Exception |
| 151 | + { |
| 152 | + ProtobufMapper mapper = new ProtobufMapper(); |
| 153 | + mapper.enable(JsonParser.Feature.IGNORE_UNDEFINED); |
| 154 | + ProtobufSchema schema = MAPPER.generateSchemaFor(Outer.class); |
| 155 | + ProtobufSchema schemaV2 = MAPPER.generateSchemaFor(OuterV2.class); |
| 156 | + |
| 157 | + EmbedV2 embedV2 = new EmbedV2(); |
| 158 | + embedV2.c = Arrays.asList("c"); |
| 159 | + embedV2.extraField = "extra"; |
| 160 | + |
| 161 | + OuterV2 v2Expected = new OuterV2(); |
| 162 | + v2Expected.embed = embedV2; |
| 163 | + v2Expected.state="state"; |
| 164 | + |
| 165 | + // serialize type with extra field |
| 166 | + byte[] doc = mapper.writer(schemaV2).writeValueAsBytes(v2Expected); |
85 | 167 |
|
86 |
| - public void testMultipleUnknown() throws Exception |
87 |
| - { |
88 |
| - MoreNestedField moreNestedField = new MoreNestedField(); |
89 |
| - NestedTwoField nestedTwoField = new NestedTwoField(); |
90 |
| - nestedTwoField.setNested1(1); |
91 |
| - nestedTwoField.setNested2(2); |
92 |
| - moreNestedField.setF1(nestedTwoField); |
| 168 | +// showBytes(bout.toByteArray()); |
93 | 169 |
|
94 |
| - byte[] in = MAPPER.writerFor(MoreNestedField.class) |
95 |
| - .with(MAPPER.generateSchemaFor(MoreNestedField.class)) |
96 |
| - .writeValueAsBytes(moreNestedField); |
| 170 | + // deserialize type with extra field |
| 171 | + OuterV2 v2Actual = mapper.readerFor(OuterV2.class) |
| 172 | + .with(schemaV2).readValue(doc); |
| 173 | + // this is ok |
| 174 | + assertEquals(v2Expected.state, v2Actual.state); |
97 | 175 |
|
98 |
| - LessNestedField lesser = MAPPER.readerFor(LessNestedField.class) |
99 |
| - .with(MAPPER.generateSchemaFor(LessNestedField.class)) |
100 |
| - // important: skip through unknown |
101 |
| - .with(JsonParser.Feature.IGNORE_UNDEFINED) |
102 |
| - .readValue(in); |
| 176 | + // deserialize type without extra field |
| 177 | + Outer v1Actual = mapper.readerFor(Outer.class).with(schema) |
| 178 | + .readValue(doc); |
103 | 179 |
|
104 |
| - assertEquals(moreNestedField.getF1().getNested2(), lesser.getF1().getNested2()); |
| 180 | + // Outer.state is skipped when skipping Embed.extraField |
| 181 | + assertEquals(v2Expected.state, v1Actual.state); |
105 | 182 | }
|
106 | 183 | }
|
0 commit comments