Micronaut/Kotlin - AWS: One of the required keys was not given a value

255 Views Asked by At

So I have a rest controller and I'm sending a request with body expecting to store that body on a dynamodb table, but I'm getting the error on the title, here is my code:

Controller:

@Patch("/activity/{activity-id}/")
    fun createNoteRecord(@PathVariable("activity-id") activity: String,
                         @PathVariable("enrollment-id") enrollment: String,
                         @PathVariable("student-id") student: String,
                         @Body eNote: ENote
    ): Mono<HttpResponse<ENote>> {
        logger.info("{activity-id: $activity - enrollment-id: $enrollment - student-id: $student - eNote: $eNote}")
        return service.save(eNote)
            .map { HttpResponse.ok(it) }
    }

Model:

@Introspected
@DynamoDbBean
data class ENote(
    @get:DynamoDbPartitionKey
    @get:DynamoDbAttribute(value = "student_id")
    var studentId: String? = null,

    @get:DynamoDbAttribute(value = "note_key")
    val noteKey: String? = null,

    @get:DynamoDbAttribute(value = "enrollment_id")
    val enrollmentId: String? = null,

    @get:DynamoDbAttribute(value = "course_id")
    val courseId: String? = null,

    @get:DynamoDbAttribute(value = "activity_id")
    val activityId: String? = null,

    @get:DynamoDbAttribute(value = "note_entries")
    val noteEntries: List<String> = emptyList(),

    @get:DynamoDbAttribute(value = "audit")
    val audit: Audit = Audit(),
)

Repository:

private val tableSchema = TableSchema.fromBean(ENote::class.java)
    private val eNoteMapTable = dynamoDbEnhancedAsyncClient.table(eNoteTableMapName, tableSchema)

    fun save(eNote: ENote): Mono<ENote>{
        return eNoteMapTable.putItem(eNote)
            .thenApply { eNote }
            .toMono()
    }

Postman body:

{
    "studentId": "ce63fa97-9f26-4ae2-869e-529904f41e91",
    "noteKey": "ENROLLMENT:f84d89b6-0b21-4461-9fc0-43a7f0ab6b9f#ACTIVITY:7b763b3c-1faa-41f0-b6be-1664034acd88",
    "enrollmentId": "f84d89b6-0b21-4461-9fc0-43a7f0ab6b9f",
    "courseId": "1bebf49b-cc49-4aa9-a0b6-c0ac6ad62b14",
    "activityId": "7b763b3c-1faa-41f0-b6be-1664034acd88",
    "noteEntries": [
        "Nota 1",
        "Nota 2",
        "Nota 3"
    ],
    "audit": {
        "createdBy":"ce63fa97-9f26-4ae2-869e-529904f41e91",
        "updatedBy":"ce63fa97-9f26-4ae2-869e-529904f41e91",
        "createdAt":"2022-02-18T19:51:35.605706Z",
        "updatedAt":"2022-02-18T19:51:35.605711Z"
    }
}

Object log output:

eNote: ENote(studentId=ce63fa97-9f26-4ae2-869e-529904f41e91, noteKey=ENROLLMENT:f84d89b6-0b21-4461-9fc0-43a7f0ab6b9f#ACTIVITY:7b763b3c-1faa-41f0-b6be-1664034acd88, enrollmentId=f84d89b6-0b21-4461-9fc0-43a7f0ab6b9f, courseId=1bebf49b-cc49-4aa9-a0b6-c0ac6ad62b14, activityId=7b763b3c-1faa-41f0-b6be-1664034acd88, noteEntries=[Nota 1, Nota 2, Nota 3], audit=Audit(createdBy=ce63fa97-9f26-4ae2-869e-529904f41e91, createdAt=2022-02-18T19:51:35.605706Z, updatedBy=ce63fa97-9f26-4ae2-869e-529904f41e91, updatedAt=2022-02-18T19:51:35.605711Z))}","logger_name":"com.il.cw.enote.controller.ENotesController","thread_name":"default-nioEventLoopGroup-1-3","level":"INFO","level_value":20000}

As you can see, when sending the request the object is actually received in the cotroller, so maybe I'm missing something somewhere else? I already verify that the database actually exist, and here is the schema:

{   "AttributeDefinitions": [
    {
      "AttributeName": "student_id",
      "AttributeType": "S"
    },
    {
      "AttributeName": "note_key",
      "AttributeType": "S"
    }   ],   "TableName": "namespace-cwng-local-enote",   "KeySchema": [
    {
      "AttributeName": "student_id",
      "KeyType": "HASH"
    },
    {
      "AttributeName": "note_key",
      "KeyType": "RANGE"
    }   ],   "TableStatus": "ACTIVE",   "CreationDateTime": "2022-10-10T18:15:04.722Z",   "ProvisionedThroughput": {
    "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
    "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
    "NumberOfDecreasesToday": 0,
    "ReadCapacityUnits": 10,
    "WriteCapacityUnits": 5   },   "TableSizeBytes": 519,   "ItemCount": 1,   "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/namespace-cwng-local-enote" }

Am I doing something wrong?

1

There are 1 best solutions below

1
Leeroy Hannigan On

In your DynamoDB table you have set a PK = student_id and an SK = note_key

When you declare your class you only state PK and there is no declaration for SK.

Try adding the range key annotation for your sort key.

Also just after seeing the postman data does not have the same naming convention for variables as your tables. You need to check that also.