29
29
import com .google .gson .Gson ;
30
30
31
31
import java .nio .ByteBuffer ;
32
+ import java .nio .charset .StandardCharsets ;
33
+ import java .util .Base64 ;
32
34
import java .util .Collections ;
33
35
import java .util .HashMap ;
34
36
import java .util .Map ;
@@ -358,6 +360,7 @@ public void testKeyValueSerializeRecordToJsonExpandingValue(SchemaType schemaTyp
358
360
RecordSchemaBuilder udtSchemaBuilder = SchemaBuilder .record ("type1" );
359
361
udtSchemaBuilder .field ("a" ).type (SchemaType .STRING ).optional ().defaultValue (null );
360
362
udtSchemaBuilder .field ("b" ).type (SchemaType .BOOLEAN ).optional ().defaultValue (null );
363
+ udtSchemaBuilder .field ("c" ).type (SchemaType .BYTES ).optional ().defaultValue (null );
361
364
udtSchemaBuilder .field ("d" ).type (SchemaType .DOUBLE ).optional ().defaultValue (null );
362
365
udtSchemaBuilder .field ("f" ).type (SchemaType .FLOAT ).optional ().defaultValue (null );
363
366
udtSchemaBuilder .field ("i" ).type (SchemaType .INT32 ).optional ().defaultValue (null );
@@ -366,12 +369,16 @@ public void testKeyValueSerializeRecordToJsonExpandingValue(SchemaType schemaTyp
366
369
valueSchemaBuilder .field ("e" , udtGenericSchema ).type (schemaType ).optional ().defaultValue (null );
367
370
GenericSchema <GenericRecord > valueSchema = Schema .generic (valueSchemaBuilder .build (schemaType ));
368
371
372
+ byte [] bytes = "10" .getBytes (StandardCharsets .UTF_8 );
369
373
GenericRecord valueGenericRecord = valueSchema .newRecordBuilder ()
370
374
.set ("c" , "1" )
371
375
.set ("d" , 1 )
372
376
.set ("e" , udtGenericSchema .newRecordBuilder ()
373
377
.set ("a" , "a" )
374
378
.set ("b" , true )
379
+ // There's a bug in json-flattener that doesn't handle byte[] fields correctly.
380
+ // But since we use AUTO_CONSUME, we won't get byte[] fields for JSON schema anyway.
381
+ .set ("c" , schemaType == SchemaType .AVRO ? bytes : Base64 .getEncoder ().encodeToString (bytes ))
375
382
.set ("d" , 1.0 )
376
383
.set ("f" , 1.0f )
377
384
.set ("i" , 1 )
@@ -434,16 +441,17 @@ public Optional<Long> getEventTime() {
434
441
String json = Utils .serializeRecordToJsonExpandingValue (objectMapper , genericObjectRecord , false );
435
442
436
443
assertEquals (json , "{\" topicName\" :\" data-ks1.table1\" ,\" key\" :\" message-key\" ,"
437
- + "\" payload\" :{\" value\" :{\" c\" :\" 1\" ,\" d\" :1,\" e\" :{\" a\" :\" a\" ,\" b\" :true,\" d \" :1.0 ,\" f \" :1.0,"
438
- + "\" i\" :1,\" l\" :10}},\" key\" :{\" a\" :\" 1\" ,\" b\" :1}}, \" properties \" :{ \" prop-key \" : \" prop-value \" },"
439
- + "\" eventTime\" :1648502845803}" );
444
+ + "\" payload\" :{\" value\" :{\" c\" :\" 1\" ,\" d\" :1,\" e\" :{\" a\" :\" a\" ,\" b\" :true,\" c \" :\" MTA= \" ,\" d \" :1.0,"
445
+ + "\" f \" :1.0, \" i\" :1,\" l\" :10}},\" key\" :{\" a\" :\" 1\" ,\" b\" :1}},"
446
+ + "\" properties \" :{ \" prop-key \" : \" prop-value \" }, \" eventTime\" :1648502845803}" );
440
447
441
448
json = Utils .serializeRecordToJsonExpandingValue (objectMapper , genericObjectRecord , true );
442
449
443
450
assertEquals (json , "{\" topicName\" :\" data-ks1.table1\" ,\" key\" :\" message-key\" ,\" payload.value.c\" :\" 1\" ,"
444
- + "\" payload.value.d\" :1,\" payload.value.e.a\" :\" a\" ,\" payload.value.e.b\" :true,\" payload.value.e"
445
- + ".d\" :1.0,\" payload.value.e.f\" :1.0,\" payload.value.e.i\" :1,\" payload.value.e.l\" :10,\" payload.key"
446
- + ".a\" :\" 1\" ,\" payload.key.b\" :1,\" properties.prop-key\" :\" prop-value\" ,\" eventTime\" :1648502845803}" );
451
+ + "\" payload.value.d\" :1,\" payload.value.e.a\" :\" a\" ,\" payload.value.e.b\" :true,"
452
+ + "\" payload.value.e.c\" :\" MTA=\" ,\" payload.value.e.d\" :1.0,\" payload.value.e.f\" :1.0,"
453
+ + "\" payload.value.e.i\" :1,\" payload.value.e.l\" :10,\" payload.key.a\" :\" 1\" ,\" payload.key.b\" :1,"
454
+ + "\" properties.prop-key\" :\" prop-value\" ,\" eventTime\" :1648502845803}" );
447
455
}
448
456
449
457
@ Test (dataProvider = "schemaType" )
0 commit comments