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

Unable to create custom array codecs

Open
#1,296 4 comments 0 reactions 1 assignee View on GitHub

@mp911de is already working on this.

Since Jul 8, 2024.

in: r2dbc status: feedback-provided status: waiting-for-triage
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

Hi,

I'm using spring-data-r2dbc and the extension mechanism from r2dbc-postgresql to create a codec for a custom Postgres range type:

CREATE TYPE timetzrange AS RANGE (subtype = timetz);

Since there's no native support for Postgres range types on Java's side, I created my own class OffsetTimeRange, and registered a custom codec OffsetTimeRangeCodec which implements the interface Codec<OffsetTimeRange>.

So far, that worked perfectly fine, but I wanted to add the custom codec for its corresponding multirange type. From Postgres range types documentation, we know that:

When you define your own range you automatically get a corresponding multirange type.

So I registered another codec OffsetTimeRangeArrayCodec which implements the interface Codec<OffsetTimeRange[]>, but this raised the following exception upon reading/writing from the database:

java.lang.IllegalArgumentException: Unsupported array type: com.example.util.OffsetTimeRange
The problem

I did some debugging, and I found the exception is thrown in the following check (line #164):
https://github.com/spring-projects/spring-data-relational/blob/b691af7b305adfb0a4f8ea7585dc8f7c0abc80e1/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/dialect/PostgresDialect.java#L156-L168

The problem seems to be that the this.simpleTypeHolder.isSimpleType(typeToUse) method checks against the local simple types, which are missing any type introduced by a custom codec.

As always, I'll be happy to help with a PR if someone can point me in the right direction 🙂

Cheers!

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.