javafirebasekotlingoogle-cloud-firestore

Firestore auto-remove "is" in property name?


I'm using Firestore for my project, and I've noticed that for some reason, the data stored in my db has the "is" removed from the key. Here is my model in Kotlin:

@Serializable
data class Transaction internal constructor(
  val projectId: String = "",
  val amount: Double = 0.0,
  val currency: String = "USD",
  val isRefund: Boolean = false,
) {
  @Serializable(with = InstantSerializer::class)
  val createdAt: Instant = Instant.now()

  // This is a lib and I want the consumer to use this instead of the constructor with default values
  companion object {
    fun new(
      projectId: String,
      amount: Double,
      currency: String,
      isRefund: Boolean
    ): Transaction {
      return Transaction(
        projectId = projectId,
        amount = amount,
        currency = currency,
        isRefund = isRefund
      )
    }
  }
}

In the db, it's being saved as refund. I searched my entire project, and there is no string refund (case-sensitive) at all. Is this a known behavior?

I'm setting the data using DocumentReference.set


Solution

  • The Firestore SDK attempts to serialize java objects to documents using document fields with the same names as those conforming to the JavaBeans standard. For boolean object properties, the "is" is stripped off to derive the name of the document field. For your object property isRefund, this means the name of the document field will be "refund".

    You can't change this behavior. If you want a different document field name, you should write some code to put the object properties into a Map that you pass to Firestore instead of the Transaction object.