spring-projects / spring-projects/spring-data-rest

not support AbstractAuditable(Optional<MyEntity> getter) [DATAREST-1490]

Open
#1,849 0 comments 1 reaction 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

john-huang-onon opened DATAREST-1490 and commented

if my Entity associate with other Entity,
like extends org.springframework.data.jpa.domain.AbstractAuditable that contain createdBy(or lastModifiedBy) field,
and the getter of field is defined as "public Optional<OtherEntity> getCreatedBy()",
will occur "java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class java.util.Optional!"

 

then I check and try:

  • org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration

mapper has registerModule(jdk8Module)

 

  • config to registerModule SimpleModule(.addSerializer(Optional.class)), all invalid

implements org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer
RepositoryRestConfigurerAdapter is deprecated
extends org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration

 

  • at runtime, com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField() will invoke ser.serialize()

this ser is not assign as jdk8Module or configured-my-custom-serializer, is org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.NestedEntitySerializer

 

  • as stated before, I find out

org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.AssociationOmittingSerializerModifier.changeProperties() will check .isEntity() then .assignSerializer(this.nestedEntitySerializer)
so Serializer always not use jdk8Module or configured-my-custom-serializer
so if the field is String, and getter is Optional<String> will work normally

 

  • use @JsonSerialize(using = MyOptionalJsonSerializer.class) to specify at getter

will occur "java.lang.IllegalStateException: Cannot override _serializer: had a my.package.config.MyOptionalJsonSerializer, trying to set to org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$NestedEntitySerializer"
cause the logic in "com.fasterxml.jackson.databind.ser.BeanPropertyWriter.assignSerializer()", is not allow change _serializer when _serializer is not null (_serializer allready assign as NestedEntitySerializer)

 

I suggest:

  • adopt a unified, don't use Optional-return-getter on associate-entity in entity
  • make Spring Data REST support
  • tell me the right way to resolve it, please forgive me for being foolish

No further details from DATAREST-1490

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.