DocumentReference.set(Object o) changes boolean field names

DocumentReference.set(Object o) changes boolean field names

When using DocumentReference.set(object), boolean fields are changed. Fields with 'is' prefix get changed to normal field value 
class Invitation {
    private boolean isRequested;
    private boolean isValid;
    private boolean isAccepted;
    private String lastName,firstName;
    private long sentOn;
}

And when I push this object to the database using set() method the boolean values are changed in this way:

Solutions/Answers:

Solution 1:

This is an automatic translation so the getter can be named isBoolean rather than getBoolean.

In Android, you can use the @PropertyName annotation to rename a property, which will allow you to specify a different name (in this instance, the exact name), which Firebase should use as is:

class Invitation {
    private boolean isRequested;

    @PropertyName(value="isRequested")
    public boolean isRequested() {
        return this.isRequested;
    }

    @PropertyName(value="isRequested")
    public void setRequested(boolean value) {
        this.isRequested = value;
    }

    // ...
}

However, I would suggest dropping the is prefix on the field names and instead only using it for the getter, like:

public boolean isRequested() {
    return this.requested;
}

Solution 2:

We experienced this issue and we solved like this.

boolean isPrimary;

public boolean getIsPrimary() {
    return this.isPrimary;
}

public void setIsPrimary(boolean isPrimary) {
    this.isPrimary = isPrimary;
}

I know it’s weird. It seems Firestore uses the ‘getter’ method to set object,
because I first changed the ‘setter’ method and nothing happened. And I changed ‘getter’ method and it finally worked correctly.

I hope they fix this issue. Because nobody uses the ‘getter’ method for boolean as ‘getIsSomething’ in Android.

References

Related:  Google Firebase forget password