Skip to content

UpdatedBy annotation not auto populating when update(fields...) repository method used #2015

@olliefreeman

Description

@olliefreeman

Expected Behavior

When I update an entity the UpdatedBy annotated field should be updated when using the preferred method of update(@Id String id, String fieldName) or updateFieldNameById(String id, String fieldName)

Actual Behaviour

this works when passing the entire object into the update repository call, but not when using the preferred method of update(@Id String id, String fieldName) or `updateFieldNameById(String id, String fieldName).
When using either of the latter methods the following exception is thrown

[default-nioEventLoopGroup-1-2] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Cannot auto populate property: updatedBy for entity: Entity
java.lang.IllegalStateException: Cannot auto populate property: updatedBy for entity: Entity
	at io.micronaut.data.runtime.support.DefaultRuntimeEntityRegistry.autoPopulateRuntimeProperty(DefaultRuntimeEntityRegistry.java:99)
	at io.micronaut.data.mongodb.operations.DefaultMongoStoredQuery$1.autoPopulateRuntimeProperty(DefaultMongoStoredQuery.java:375)
	at io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersStoredQuery.bindParameter(DefaultBindableParametersStoredQuery.java:151)
	at io.micronaut.data.mongodb.operations.DefaultMongoStoredQuery.bind(DefaultMongoStoredQuery.java:371)
	at io.micronaut.data.mongodb.operations.DefaultMongoStoredQuery.replaceQueryParametersInBsonValue(DefaultMongoStoredQuery.java:427)
	at io.micronaut.data.mongodb.operations.DefaultMongoStoredQuery.replaceQueryParametersInBsonValue(DefaultMongoStoredQuery.java:506)
	at io.micronaut.data.mongodb.operations.DefaultMongoStoredQuery.replaceQueryParametersInBsonValue(DefaultMongoStoredQuery.java:506)
	at io.micronaut.data.mongodb.operations.DefaultMongoStoredQuery.replaceQueryParameters(DefaultMongoStoredQuery.java:352)

Examining the stack it appears that under the preferred methodology the io.micronaut.data.runtime.support.DefaultRuntimeEntityRegistry is called which only has access to populators implementing the io.micronaut.data.model.runtime.PropertyAutoPopulator interface. When running under save or update everything the io.micronaut.security.audit.UserAuditingEntityEventListener is called, but this class does not extend the io.micronaut.data.model.runtime.PropertyAutoPopulator interface.

The simple solution would be implementing the User Auditing listener with the populator interface but I have no idea what ramifications this would have.

For now I will just use the standard update all method, but it would be nice for this to be fixed

Steps To Reproduce

  1. Implement a simple MappedEntity with the @createdby and @UpdatedBy annotations used, a single field "String data" and an id field appropriate for the db of choice
  2. Implement security into the app
  3. Implement a controller
  4. POST endpoint to create the entity, observe the 2 fields are populated
  5. PUT endpoint to update the entity, sending the data object as different
  6. In the controller call the repository interface for the entity use updateDataById(id, data), observe it fail
  7. In the controller use "findById" from the repo, manually update the returned object's data field and use repository.update(entity). Observe it suceed

Environment Information

No response

Example Application

No response

Version

4.9.10 (security 4.13.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions