Search code examples
javakotlinamazon-dynamodbaws-sdkdynamodb-enhanced-client

Kotlin DynamoDB Boolean nullable property never get assigned


I'm writing a test which I need to create a record in local DynamoDB with a nullable boolean property. This is my setup:

@DynamoDbBean
data class Event(
  // other properties
  @get:DynamoDbAttribute("is_enabled")
  var isEnabled: Boolean? = false,
)

companion object {
  val SCHEMA: TableSchema<Event> = TableSchema.fromBean(Event::class.java)
}
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient

data class EventRepository(
  private val enhancedClient: DynamoDbEnhancedClient,
) {
  val table: DynamoDbTable<Event> = enhancedClient.table(
        tableNameWithPrefix, Event.SCHEMA)
}
class TestController  {
  @Autowired
  private lateinit var repo: EventRepository
  
  @Test
  fun test() {
    repo.table.putItem(Event(isEnabled = true))
  }
}

I'm not sure how DynamoDB handles boolean values, somehow this field is_enabled is always missing in the database. When the property was set to Boolean?, the record in database has is_enabled = null.

It works if I change the property to Boolean, the record in database has is_enabled = true when I set the value.

And changing the property to Boolean does not work for me because this value can be nullable.


Solution

  • DynamoDB does not support nulling of values, it instead has a data type Null.

    As DynamoDB is a schemaless database, it's suggested not to save Null values.

    Furthermore, Java Bean has some guidance on using the isVariable naming convention which may also cause issue: From the JavaBeans specification, 8.3.2:

    In addition, for boolean properties, we allow a getter method to match the pattern: public boolean is(); This is method may be provided instead of a get method, or it may be provided in addition to a get method.

    http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/