diff --git a/ballerina/tests/record_tests.bal b/ballerina/tests/record_tests.bal index 63bb726..2335abc 100644 --- a/ballerina/tests/record_tests.bal +++ b/ballerina/tests/record_tests.bal @@ -371,7 +371,6 @@ public isolated function testOptionalMultipleFieldsInRecords() returns error? { return verifyOperation(Lecturer6, lecturer6, schema); } - @test:Config { groups: ["record", "union"] } @@ -409,7 +408,7 @@ public isolated function testTypeCastingInRecords() returns error? { }; Schema avro = check new (schema); byte[] serializedValue = check avro.toAvro(recordValue); - var deserializedValue = check avro.fromAvro(serializedValue, Record); + Record deserializedValue = check avro.fromAvro(serializedValue); json expected = { "hrdCasinoGgrLt":4999.01, @@ -443,32 +442,27 @@ public isolated function testTypeCastingInRecords() returns error? { test:assertEquals(deserializedValue.toJson(), expected.toJson()); } -type Record record { - decimal? hrdCasinoGgrLt; - string? hrdAccountCreationTimestamp; - float? hrdSportsBetCountLifetime; - float? hrdSportsFreeBetAmountLifetime; - string? hriUnityId; - string? hrdLastRealMoneySportsbookBetTs; - string? hrdLoyaltyTier; - string? rowInsertTimestampEst; - float? ltvSports365Total; - string? hrdFirstDepositTimestamp; - boolean? hrdVipStatus; - string? hrdLastRealMoneyCasinoWagerTs; - float? hrdSportsCashBetAmountLifetime; - string? hrdAccountStatus; - string? hrdAccountId; - float? ltvAllVerticals365Total; - boolean? hrdOptInSms; - float? ltvCasino365Total; - string? hrdAccountSubStatus; - float? hrdCasinoTotalWagerLifetime; - float? hrdSportsGgrLt; - string? currentGeoSegment; - string? kycStatus; - string? signupGeoSegment; - float? hrdSportsBetAmountLifetime; - boolean? hrdOptInEmail; - boolean? hrdOptInPush; -}; +@test:Config { + groups: ["record", "union"] +} +public isolated function testUnionsWithRecords() returns error? { + string schema = string `{ + "type": "record", + "name": "DataRecord", + "namespace": "data", + "fields": [ + { + "name": "shortValue", + "type": ["null", "int", "double"] + } + ] + }`; + int:Signed16 short = 5555; + json value = { + "shortValue": short + }; + Schema avro = check new (schema); + byte[] serializedValue = check avro.toAvro(value); + DataRecord deserializedValue = check avro.fromAvro(serializedValue); + test:assertEquals(deserializedValue, value); +} diff --git a/ballerina/tests/types.bal b/ballerina/tests/types.bal index 542dc61..f68869b 100644 --- a/ballerina/tests/types.bal +++ b/ballerina/tests/types.bal @@ -363,3 +363,4 @@ type FloatArray float[]; type EnumArray Numbers[]; type Enum2DArray Numbers[][]; type ReadOnlyString2DArray string[][] & readonly; +type DataRecord record{}; diff --git a/native/src/main/java/io/ballerina/lib/avro/deserialize/visitor/UnionRecordUtils.java b/native/src/main/java/io/ballerina/lib/avro/deserialize/visitor/UnionRecordUtils.java index b56484f..c631945 100644 --- a/native/src/main/java/io/ballerina/lib/avro/deserialize/visitor/UnionRecordUtils.java +++ b/native/src/main/java/io/ballerina/lib/avro/deserialize/visitor/UnionRecordUtils.java @@ -39,41 +39,26 @@ public class UnionRecordUtils { public static void visitUnionRecords(Type type, BMap ballerinaRecord, Schema.Field field, Object fieldData) throws Exception { + int size = ballerinaRecord.size(); for (Schema schemaType : field.schema().getTypes()) { if (fieldData == null) { ballerinaRecord.put(StringUtils.fromString(field.name()), null); break; } switch (schemaType.getType()) { - case BYTES: - handleBytesField(field, fieldData, ballerinaRecord); - break; - case FIXED: - handleFixedField(field, fieldData, ballerinaRecord); - break; - case ARRAY: - handleArrayField(field, fieldData, ballerinaRecord, schemaType); - break; - case MAP: - handleMapField(field, fieldData, ballerinaRecord); - break; - case RECORD: - handleRecordField(type, field, fieldData, ballerinaRecord, schemaType); - break; - case STRING: - handleStringField(field, fieldData, ballerinaRecord); - break; - case INT, LONG: - handleIntegerField(field, fieldData, ballerinaRecord); - break; - case FLOAT, DOUBLE: - handleFloatField(field, fieldData, ballerinaRecord); - break; - case ENUM: - handleEnumField(field, fieldData, ballerinaRecord); - break; - default: - handleDefaultField(field, fieldData, ballerinaRecord); + case BYTES -> handleBytesField(field, fieldData, ballerinaRecord); + case FIXED -> handleFixedField(field, fieldData, ballerinaRecord); + case ARRAY -> handleArrayField(field, fieldData, ballerinaRecord, schemaType); + case MAP -> handleMapField(field, fieldData, ballerinaRecord); + case RECORD -> handleRecordField(type, field, fieldData, ballerinaRecord, schemaType); + case STRING -> handleStringField(field, fieldData, ballerinaRecord); + case INT, LONG -> handleIntegerField(field, fieldData, ballerinaRecord); + case FLOAT, DOUBLE -> handleFloatField(field, fieldData, ballerinaRecord); + case ENUM -> handleEnumField(field, fieldData, ballerinaRecord); + default -> handleDefaultField(field, fieldData, ballerinaRecord); + } + if (ballerinaRecord.size() != size) { + break; } } }