spring-projects / spring-projects/spring-data-relational

Custom class is registered as simple type only if there is writing converter

Open
#1,194 6 comments 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Mar 10, 2022.

in: jdbc type: bug
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

I am using Spring Data JDBC repository to query data for report (hence, readonly). In one place I needed to convert from JSONB to a DTO. I registered a customer converter marked as @ReadingConverter for that purpose, but for some reason it was never called. Debugging the code, I noticed that my DTO is considered as an entity, although it should have been registered as a simple type. Debugging more I found out that only the source type of a @WritingConverter is registered as a simple type. Adding a dummy writing converter fixed the issue for me. However, this seems unexpected and the fix adds redundant code.

My code:

@Data
public class ReportDTO {

  private MyData myData;

}

@Configuration
public class JdbcConfig extends AbstractJdbcConfiguration

  private final ObjectMapper objectMapper = new ObjectMapper();

  @Bean
  @NonNull
  @Override
  public JdbcCustomConversions jdbcCustomConversions() {
    return new JdbcCustomConversions(
        List.of(
            new JsonbToMyData(objectMapper),
        )
    );
  }


  @ReadingConverter
  @RequiredArgsConstructor
  static class JsonbToMyData implements Converter<PGobject, MyData> {

    private final ObjectMapper objectMapper;

    @Override
    public MyData convert(PGobject source) {
      try {
        return objectMapper.readValue(source.getValue(), MyData.class);
      } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
      }
    }

  }

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.