Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPROTO-350 Proto3 record default values initialized to null #295

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ private void discoverFieldsFromRecord(XClass clazz, Map<Integer, ProtoFieldMetad
String oneof = null;
Object defaultValue;
if (annotation == null) {
defaultValue = getDefaultValue(clazz, fieldName, javaType, protobufType, null, false);
defaultValue = getDefaultValue(clazz, fieldName, javaType, protobufType, "", false);
} else {
if (annotation.number() > 0) fieldNumber = annotation.number();
if (!annotation.name().isEmpty()) fieldName = annotation.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.infinispan.protostream.integrationtests.processor.marshaller.model.NullTestModel;
import org.infinispan.protostream.integrationtests.processor.marshaller.model.Player;
import org.infinispan.protostream.integrationtests.processor.marshaller.model.SimpleEnum;
import org.infinispan.protostream.integrationtests.processor.marshaller.model.SimpleRecord;
import org.junit.Test;

public class GeneratedMarshallerTest {
Expand Down Expand Up @@ -84,6 +85,11 @@ public void proto2NullFields() throws IOException {
assertNull(player.getShirtNumber());
assertEquals(0, player.getMatchRating());
assertNull(player.getBytes());

bytes = ProtobufUtil.toWrappedByteArray(ctx, new SimpleRecord(null, null));
SimpleRecord record = ProtobufUtil.fromWrappedByteArray(ctx, bytes);
assertNull(record.string());
assertNull(record.boxedInt());
}

@Test
Expand All @@ -100,6 +106,11 @@ public void proto3AllowNullFields() throws IOException {
assertEquals(0, model.primitiveInt);
assertNull(model.bytes);
assertNull(model.simpleEnum);

bytes = ProtobufUtil.toWrappedByteArray(ctx, new SimpleRecord(null, null));
SimpleRecord record = ProtobufUtil.fromWrappedByteArray(ctx, bytes);
assertNull(record.string());
assertNull(record.boxedInt());
}

@Test
Expand All @@ -116,12 +127,18 @@ public void proto3NoNullFields() throws IOException {
assertEquals(0, model.primitiveInt);
assertEquals(0, model.bytes.length);
assertEquals(SimpleEnum.FIRST, model.simpleEnum);

bytes = ProtobufUtil.toWrappedByteArray(ctx, new SimpleRecord(null, null));
SimpleRecord record = ProtobufUtil.fromWrappedByteArray(ctx, bytes);
assertEquals("", record.string());
assertEquals((Integer) 0, record.boxedInt());
}

@ProtoSchema(
includeClasses = {
NullTestModel.class,
SimpleEnum.class
SimpleEnum.class,
SimpleRecord.class
},
schemaPackageName = "nonulls",
schemaFilePath = "proto",
Expand All @@ -135,7 +152,8 @@ interface NoNullsSchema extends GeneratedSchema {
allowNullFields = true,
includeClasses = {
NullTestModel.class,
SimpleEnum.class
SimpleEnum.class,
SimpleRecord.class
},
schemaPackageName = "allownulls",
schemaFilePath = "proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;

@AutoProtoSchemaBuilder(includeClasses = { FootballTeam.class, Player.class }, schemaPackageName = "org.football",
@AutoProtoSchemaBuilder(includeClasses = { FootballTeam.class, Player.class, SimpleRecord.class }, schemaPackageName = "org.football",
schemaFileName = "football.proto", schemaFilePath = "proto")
public interface FootballSchema extends GeneratedSchema {
GeneratedSchema INSTANCE = new FootballSchemaImpl();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.infinispan.protostream.integrationtests.processor.marshaller.model;

import org.infinispan.protostream.annotations.Proto;

@Proto
public record SimpleRecord(String string, Integer boxedInt) {
}