|
33 | 33 | import org.apache.avro.generic.GenericDatumReader; |
34 | 34 | import org.apache.avro.generic.GenericRecord; |
35 | 35 | import org.apache.avro.io.BinaryDecoder; |
36 | | -import org.apache.avro.io.BinaryEncoder; |
37 | 36 | import org.apache.avro.io.Decoder; |
38 | | -import org.apache.avro.specific.SpecificDatumWriter; |
39 | 37 | import org.apache.avro.util.Utf8; |
40 | 38 | import org.testng.Assert; |
41 | 39 | import org.testng.SkipException; |
42 | 40 | import org.testng.annotations.BeforeTest; |
43 | 41 | import org.testng.annotations.DataProvider; |
44 | 42 | import org.testng.annotations.Test; |
45 | | -import org.testng.collections.Lists; |
46 | 43 | import org.testng.internal.collections.Pair; |
47 | 44 |
|
48 | 45 | import static com.linkedin.avro.fastserde.FastSerdeTestsSupport.*; |
@@ -256,6 +253,154 @@ public void shouldReadFixed(Implementation implementation) { |
256 | 253 | ((List<GenericData.Fixed>) record.get("testFixedUnionArray")).get(0).bytes()); |
257 | 254 | } |
258 | 255 |
|
| 256 | + @Test(groups = {"deserializationTest"}, dataProvider = "Implementation") |
| 257 | + public void shouldHandleDiffNamepspaceInRecords(Implementation implementation) { |
| 258 | + //record with two fields, first optional string second a record with namespace "a.b.c" with name "innerRecordName". Inner record has one int field |
| 259 | + Schema writerSchema = Schema.parse("{\n" + |
| 260 | + " \"type\": \"record\",\n" + |
| 261 | + " \"name\": \"OuterRecord\",\n" + |
| 262 | + " \"fields\": [\n" + |
| 263 | + " {\n" + |
| 264 | + " \"name\": \"optionalString\",\n" + |
| 265 | + " \"type\": [\"null\", \"string\"],\n" + |
| 266 | + " \"default\": null\n" + |
| 267 | + " },\n" + |
| 268 | + " {\n" + |
| 269 | + " \"name\": \"innerRecord\",\n" + |
| 270 | + " \"type\": {\n" + |
| 271 | + " \"type\": \"record\",\n" + |
| 272 | + " \"name\": \"innerRecordName\",\n" + |
| 273 | + " \"namespace\": \"a.b.c\",\n" + |
| 274 | + " \"fields\": [\n" + |
| 275 | + " {\n" + |
| 276 | + " \"name\": \"intField\",\n" + |
| 277 | + " \"type\": \"int\"\n" + |
| 278 | + " }\n" + |
| 279 | + " ]\n" + |
| 280 | + " }\n" + |
| 281 | + " }\n" + |
| 282 | + " ]\n" + |
| 283 | + "}"); |
| 284 | + |
| 285 | + //change namespace on inner record to "d.e.f" |
| 286 | + Schema readerSchema = Schema.parse("{\n" + |
| 287 | + " \"type\": \"record\",\n" + |
| 288 | + " \"name\": \"OuterRecord\",\n" + |
| 289 | + " \"fields\": [\n" + |
| 290 | + " {\n" + |
| 291 | + " \"name\": \"optionalString\",\n" + |
| 292 | + " \"type\": [\"null\", \"string\"],\n" + |
| 293 | + " \"default\": null\n" + |
| 294 | + " },\n" + |
| 295 | + " {\n" + |
| 296 | + " \"name\": \"innerRecord\",\n" + |
| 297 | + " \"type\": [\"null\", {\n" + |
| 298 | + " \"type\": \"record\",\n" + |
| 299 | + " \"name\": \"innerRecordName\",\n" + |
| 300 | + " \"namespace\": \"d.e.f\",\n" + |
| 301 | + " \"fields\": [\n" + |
| 302 | + " {\n" + |
| 303 | + " \"name\": \"intField\",\n" + |
| 304 | + " \"type\": \"int\"\n" + |
| 305 | + " }\n" + |
| 306 | + " ]\n" + |
| 307 | + " }],\n" + |
| 308 | + " \"default\": null\n" + |
| 309 | + " }\n" + |
| 310 | + " ]\n" + |
| 311 | + "}"); |
| 312 | + |
| 313 | + GenericRecord originalRecord = new GenericData.Record(writerSchema); |
| 314 | + originalRecord.put("optionalString", "abc"); |
| 315 | + GenericRecord innerRecord = new GenericData.Record(writerSchema.getField("innerRecord").schema()); |
| 316 | + innerRecord.put("intField", 1); |
| 317 | + originalRecord.put("innerRecord", innerRecord); |
| 318 | + |
| 319 | + // when |
| 320 | + try{ |
| 321 | + GenericRecord record = implementation.decode(writerSchema, readerSchema, genericDataAsDecoder(originalRecord)); |
| 322 | + // then |
| 323 | + if(Utils.usesQualifiedNameForNamedTypedMatching()){ |
| 324 | + Assert.fail("1.5-1.7 don't support unqualified name for named type matching so we should have failed"); |
| 325 | + } |
| 326 | + Assert.assertEquals(new Utf8("abc"), record.get("optionalString")); |
| 327 | + GenericRecord innerRecordDecoded = (GenericRecord) record.get("innerRecord"); |
| 328 | + Assert.assertEquals(1, innerRecordDecoded.get("intField")); |
| 329 | + } catch (Exception e){ |
| 330 | + if(!Utils.usesQualifiedNameForNamedTypedMatching()) { |
| 331 | + Assert.fail("1.4, and 1.8+ support unqualified name for named type matching"); |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | + |
| 337 | + @Test(groups = {"deserializationTest"}, dataProvider = "Implementation") |
| 338 | + public void shouldNotFailOnNamespaceMismatch(Implementation implementation) throws IOException { |
| 339 | + // writer-side schema: "metadata" has NO namespace |
| 340 | + String writerSchemaStr = "{\n" + |
| 341 | + " \"type\": \"record\",\n" + |
| 342 | + " \"name\": \"wrapper\",\n" + |
| 343 | + " \"fields\": [\n" + |
| 344 | + " {\n" + |
| 345 | + " \"name\": \"metadata\",\n" + |
| 346 | + " \"type\": [\"null\", {\n" + |
| 347 | + " \"type\": \"record\",\n" + |
| 348 | + " \"name\": \"metadata\",\n" + |
| 349 | + " \"fields\": [\n" + |
| 350 | + " {\"name\": \"fieldName\", \"type\": [\"null\", \"string\"], \"default\": null}\n" + |
| 351 | + " ]\n" + |
| 352 | + " }],\n" + |
| 353 | + " \"default\": null\n" + |
| 354 | + " }\n" + |
| 355 | + " ]\n" + |
| 356 | + "}"; |
| 357 | + |
| 358 | + // reader-side schema: same nested record but WITH namespace "rtapi.surge" |
| 359 | + String readerSchemaStr = "{\n" + |
| 360 | + " \"type\": \"record\",\n" + |
| 361 | + " \"name\": \"Wrapper\",\n" + |
| 362 | + " \"fields\": [\n" + |
| 363 | + " {\n" + |
| 364 | + " \"name\": \"metadata\",\n" + |
| 365 | + " \"type\": [\"null\", {\n" + |
| 366 | + " \"type\": \"record\",\n" + |
| 367 | + " \"name\": \"metadata\",\n" + |
| 368 | + " \"namespace\": \"some.other.namespace\",\n" + |
| 369 | + " \"fields\": [\n" + |
| 370 | + " {\"name\": \"fieldName\", \"type\": [\"null\", \"string\"], \"default\": null}\n" + |
| 371 | + " ]\n" + |
| 372 | + " }],\n" + |
| 373 | + " \"default\": null\n" + |
| 374 | + " }\n" + |
| 375 | + " ]\n" + |
| 376 | + "}"; |
| 377 | + |
| 378 | + Schema writerSchema = AvroCompatibilityHelper.parse(writerSchemaStr); |
| 379 | + Schema readerSchema = AvroCompatibilityHelper.parse(readerSchemaStr); |
| 380 | + |
| 381 | + // Build a writer-side record instance |
| 382 | + GenericRecord wrapper = new GenericData.Record(writerSchema); |
| 383 | + Schema metadataSchema = writerSchema.getField("metadata").schema().getTypes().get(1); |
| 384 | + GenericRecord metadataRecord = new GenericData.Record(metadataSchema); |
| 385 | + metadataRecord.put("fieldName", "abc-123"); |
| 386 | + wrapper.put("metadata", metadataRecord); |
| 387 | + |
| 388 | + // Attempt to deserialize – should throw AvroTypeException because of namespace mismatch |
| 389 | + try{ |
| 390 | + GenericRecord record = implementation.decode(writerSchema, readerSchema, genericDataAsDecoder(wrapper)); |
| 391 | + if(Utils.usesQualifiedNameForNamedTypedMatching()){ |
| 392 | + Assert.fail("1.5-1.7 don't support unqualified name for named type matching so we should have failed"); |
| 393 | + } |
| 394 | + Assert.assertEquals(((GenericRecord)record.get("metadata")).get("fieldName").toString(), "abc-123"); |
| 395 | + } catch (AvroTypeException e){ |
| 396 | + if(!Utils.usesQualifiedNameForNamedTypedMatching()) { |
| 397 | + Assert.fail("1.4, and 1.8+ support unqualified name for named type matching"); |
| 398 | + } |
| 399 | + // expected exception |
| 400 | + } |
| 401 | + |
| 402 | + } |
| 403 | + |
259 | 404 | @Test(groups = {"deserializationTest"}, dataProvider = "Implementation") |
260 | 405 | public void shouldReadEnum(Implementation implementation) { |
261 | 406 | // given |
|
0 commit comments