@@ -316,4 +316,69 @@ void serialize_WithNullDecimalLogicalType_ReturnsNull() {
316316 assertEquals ("{\" amount\" : null}" , json );
317317 }
318318
319+ @ Test
320+ void serialize_WithFixedDecimalLogicalType_UsesScaleFromSchema () {
321+ BigDecimal value = new BigDecimal ("12.34" ).setScale (2 );
322+ byte [] decimalBytes = value .unscaledValue ().toByteArray ();
323+
324+ Schema decimalSchema = new Schema .Parser ().parse (
325+ "{ \" type\" : \" record\" , \" name\" : \" DecimalRecord\" , \" fields\" : [" +
326+ "{\" name\" : \" amount\" , \" type\" : [\" null\" , {\" type\" :\" fixed\" ,\" size\" :" + decimalBytes .length +
327+ ",\" name\" :\" DecimalFixed\" ,\" logicalType\" :\" decimal\" ,\" precision\" :4,\" scale\" :2}]}" +
328+ "] }"
329+ );
330+
331+ GenericRecord record = new GenericData .Record (decimalSchema );
332+
333+ Schema fixedSchema = decimalSchema .getField ("amount" ).schema ().getTypes ().get (1 );
334+ GenericData .Fixed fixedValue = new GenericData .Fixed (fixedSchema , decimalBytes );
335+ record .put ("amount" , fixedValue );
336+
337+ String json = encoder .serialize (record );
338+
339+ assertEquals ("{\" amount\" : 12.34}" , json );
340+ }
341+
342+ @ Test
343+ void serialize_WithNonNullableFixedDecimalLogicalType_UsesScaleFromSchema () {
344+ BigDecimal value = new BigDecimal ("12.3456" ).setScale (4 );
345+ byte [] decimalBytes = value .unscaledValue ().toByteArray ();
346+
347+ Schema decimalSchema = new Schema .Parser ().parse (
348+ "{ \" type\" : \" record\" , \" name\" : \" DecimalRecord\" , \" fields\" : [" +
349+ "{\" name\" : \" amount\" , \" type\" : {\" type\" :\" fixed\" ,\" size\" :" + decimalBytes .length +
350+ ",\" name\" :\" DecimalFixed\" ,\" logicalType\" :\" decimal\" ,\" precision\" :6,\" scale\" :4}}" +
351+ "] }"
352+ );
353+
354+ GenericRecord record = new GenericData .Record (decimalSchema );
355+
356+ Schema fixedSchema = decimalSchema .getField ("amount" ).schema ();
357+ GenericData .Fixed fixedValue = new GenericData .Fixed (fixedSchema , decimalBytes );
358+ record .put ("amount" , fixedValue );
359+
360+ String json = encoder .serialize (record );
361+
362+ assertEquals ("{\" amount\" : 12.3456}" , json );
363+ }
364+
365+ @ Test
366+ void serialize_WithNonDecimalFixedType_ReturnsEscapedString () {
367+ byte [] tokenBytes = new byte [] { 34 , 92 , 13 , 10 , 9 };
368+ Schema tokenSchema = new Schema .Parser ().parse (
369+ "{ \" type\" : \" record\" , \" name\" : \" MyRecord\" , \" fields\" : [" +
370+ "{\" name\" : \" token\" , \" type\" : {\" type\" :\" fixed\" ,\" name\" :\" TokenFixed\" ,\" size\" :5}}" +
371+ "] }"
372+ );
373+
374+ GenericRecord record = new GenericData .Record (tokenSchema );
375+
376+ Schema fixedSchema = tokenSchema .getField ("token" ).schema ();
377+ GenericData .Fixed fixedValue = new GenericData .Fixed (fixedSchema , tokenBytes );
378+ record .put ("token" , fixedValue );
379+
380+ String json = encoder .serialize (record );
381+
382+ assertEquals ("{\" token\" : {\" bytes\" : \" \\ \" \\ \\ \\ r\\ n\\ t\" }}" , json );
383+ }
319384}
0 commit comments