quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.82k stars 2.69k forks source link

Infinispan marshalling of POJOs with boolean fields not working #42023

Open omasseau opened 3 months ago

omasseau commented 3 months ago

Describe the bug

Using Infinispan extension, marshalling of POJOs with simple primitive boolean fields is not working and gives this error :

C:\dev\git\quarkus-test\src\main\java\com\test\Person.java:22: error: org.infinispan.protostream.annotations.ProtoSchemaBuilderException: Primitive field 'isMarried' of com.test.Person is not nullable so it should be either marked required or should have a default value, while processing com.test.Schema interface Schema extends GeneratedSchema { ^ at org.infinispan.protostream.annotations.impl.ProtoMessageTypeMetadata.discoverFieldsFromClassFields(ProtoMessageTypeMetadata.java:625)

Yet the infinispan documentation says :

image

So I'm expecting the marshalling of boolean primitive type to work.

Here is the class :

@Proto
public class Person {
    public String name;
    public boolean isMarried = false;

    public Person(){
    }

    public Person(String name, boolean isMarried) {
        this.name = name;
        this.isMarried = isMarried;
    }
}

@ProtoSchema(includeClasses = { Person.class })
interface Schema extends GeneratedSchema {
}

Is this a quarkus limitation or the Infinispan documentation is wrong ? Having to change all my boolean fields to Boolean is not a solution. I'm expecting a framework/library to be able at least to marshall any primitive types out of the box ;)

Expected behavior

POJOs with boolean fields should be able to be marshalled out of the box

Actual behavior

No response

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

java 21.0.3

Quarkus version or git rev

3.12.3

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

quarkus-bot[bot] commented 3 months ago

/cc @karesti (infinispan), @wburns (infinispan)

omasseau commented 3 months ago

Note:

It seems boolean fields work only with records. I have no error when trying this :

@Proto
public record Person (String name, boolean isMarried) {
}

@ProtoSchema(includeClasses = { Person.class })
interface Schema extends GeneratedSchema {
}

Still this is not a solution for me as in my case I cannot use records.

karesti commented 3 months ago

@omasseau have you tried adding @ProtoField(defaultValue = "false") over the field as a workaround? In any case, this should work for records and classes. Opening an issue in Protostream @tristantarrant

omasseau commented 3 months ago

@ProtoField(defaultValue = "false")

Hi @karesti

Yes it works, but only if I also explicitely define a number :

    @ProtoField(defaultValue = "false", number = 2)
    public boolean isMarried = false;

As you can guess, we don't really want to explicitely add this annotation on every boolean fields, as it somewhat defeats the expected benefits of having a protobuf schema being automatically generated for us :)

karesti commented 3 months ago

yes, I opened an issue @omasseau https://issues.redhat.com/browse/IPROTO-362 @fax4ever