1

I use Firestore (from Firebase). For a reason that I don't understand a variable of one of my classes is not saved (in Firestore) with his correct name.

Here is an example class :

data class ExampleClass(
        val id: String,
        val foo: String,
        val bar: String,
        val isEnabled: Bool)

When I save this object in Firestore, I have no problem with id, foo or bar. However, isEnabled is saved as enabled. The is is removed without any reason that I know of.

Do one of you know if this is normal? I want to save the value as isEnabled and not as enabled.

Also, is it possible to specify a "name" to use in Firestore for each value? So I could force to save that value as "isEnabled". I have seen @PropertyName (Firebase doc) but it does not seem to have the effect I expect.

Thanks in advance

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Valentin
  • 5,379
  • 7
  • 36
  • 50

1 Answers1

1

This is normal, and intuitive. It's the way that JavaBean type objects are serialized. According to the JavaBeans specification section 8.3.2 boolean properties disregard the "is" on the method name for boolean getters:

Boolean properties

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is();

This "isPropertyName" method may be provided instead of a "get" method, or it may be provided in addition to a "get" method. In either case, if the is method is present for a boolean property then we will use the "is" method to read the property value. An example boolean property might be:

public boolean isMarsupial();

public void setMarsupial(boolean m);

Having "is" prefixed on a boolean is kind of redundant, in my opinion. If you know a property is a boolean, the implication is that it implicitly "is" or "isn't" the thing in its name.

But if you really must change the name, you can use the @PropertyName annotation:

data class ExampleClass(
    val id: String,
    val foo: String,
    val bar: String,
    @PropertyName(value="isEnabled")
    val isEnabled: Bool
)
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Thanks a lot Doug. Everything makes sense now. However the @PropertyName does not work for me :/ – Valentin Apr 20 '18 at 07:00
  • 2
    Ok, I found the reason of that last problem there: https://stackoverflow.com/questions/38681260/firebase-propertyname-doesnt-work?rq=1 – Valentin Apr 20 '18 at 09:16