Skip to content

AggregateReference conversion does not honor custom converters #1750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kota65535 opened this issue Mar 11, 2024 · 2 comments
Closed

AggregateReference conversion does not honor custom converters #1750

kota65535 opened this issue Mar 11, 2024 · 2 comments
Assignees
Labels
type: bug A general bug

Comments

@kota65535
Copy link

kota65535 commented Mar 11, 2024

Given MySQL table schema, entity class and custom converter...

  • Table schema
CREATE TABLE task
(
  id          int          NOT NULL AUTO_INCREMENT,
  owner       binary(16)   NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (owner) REFERENCES user (id)
);
  • Entity class
@Data
@Table("task")
public class TaskEntity {

  @Id
  private Integer id;

  private AggregateReference<UserEntity, UUID> owner;
}
  • Custom converter
@Component
public class UuidFromBin implements Converter<byte[], UUID> {

  @Override
  public UUID convert(byte[] source) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(source);
    long high = byteBuffer.getLong();
    long low = byteBuffer.getLong();
    return new UUID(high, low);
  }
}

This should convert value of the binary id column into UUID id field of TaskEntity when the table is read, but the following exception is thrown.

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Byte] to type [org.springframework.data.jdbc.core.mapping.AggregateReference<jp.co.sre.aip.samplebasic.core.app.repository.UserEntity, java.util.UUID>] for value [-121]
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:182)
	at org.springframework.core.convert.support.ArrayToObjectConverter.convert(ArrayToObjectConverter.java:68)
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:182)

...

Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Byte] to type [java.util.UUID]

	at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:294)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:185)
	at org.springframework.data.jdbc.core.convert.AggregateReferenceConverters$SimpleTypeToAggregateReferenceConverter.convert(AggregateReferenceConverters.java:131)
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 11, 2024
@mp911de
Copy link
Member

mp911de commented Mar 11, 2024

Can you provide a minimal yet complete sample that reproduces the problem?
You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Mar 11, 2024
@kota65535
Copy link
Author

@mp911de Sure, here it is.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 11, 2024
schauder added a commit that referenced this issue May 2, 2024
Let the AggregateReference converters now receive also custom converters as delegates.
Remove the ArrayToObjectConverter which just uses the first element of the array from the DefaultConversion service.

Closes #1750
schauder added a commit that referenced this issue May 2, 2024
Remove redundant code.

See #1750
schauder added a commit that referenced this issue May 7, 2024
Let the AggregateReference converters now receive also custom converters as delegates.
Remove the ArrayToObjectConverter which just uses the first element of the array from the DefaultConversion service.

This does NOT solve the underlying problem, of having two DefaultConversionServices at work.
One is in the store conversion, one constructed in the AbstractRelationalConverter.
Although it looks like they get merged, the former contains converters, using that ConversionService as a delegate.

Attempts were made to move AggregateReferenceConverters out of the store converters and just register them directly,
but then no custom read/write targets are detected, leading to failed conversions.
The same problem prevents a registration in CustomConversions as a Function<ConversionService, GenericConverter> or similar, which would allow late registration.
The problem here is that custom read/write targets require the function to get evaluated, before custom read/write targets can be determined.

Closes #1750
schauder added a commit that referenced this issue May 7, 2024
Remove redundant code.

See #1750
schauder added a commit that referenced this issue May 7, 2024
Let the AggregateReference converters now receive also custom converters as delegates.
Remove the ArrayToObjectConverter which just uses the first element of the array from the DefaultConversion service.

This does NOT solve the underlying problem, of having two DefaultConversionServices at work.
One is in the store conversion, one constructed in the AbstractRelationalConverter.
Although it looks like they get merged, the former contains converters, using that ConversionService as a delegate.

Attempts were made to move AggregateReferenceConverters out of the store converters and just register them directly,
but then no custom read/write targets are detected, leading to failed conversions.
The same problem prevents a registration in CustomConversions as a Function<ConversionService, GenericConverter> or similar, which would allow late registration.
The problem here is that custom read/write targets require the function to get evaluated, before custom read/write targets can be determined.

Closes #1750
schauder added a commit that referenced this issue May 7, 2024
Remove redundant code.

See #1750
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels May 15, 2024
@mp911de mp911de added this to the 3.2.6 (2023.1.6) milestone May 15, 2024
mp911de pushed a commit that referenced this issue May 15, 2024
Let the AggregateReference converters now receive also custom converters as delegates.
Remove the ArrayToObjectConverter which just uses the first element of the array from the DefaultConversion service.

This does NOT solve the underlying problem, of having two DefaultConversionServices at work.
One is in the store conversion, one constructed in the AbstractRelationalConverter.
Although it looks like they get merged, the former contains converters, using that ConversionService as a delegate.

Attempts were made to move AggregateReferenceConverters out of the store converters and just register them directly,
but then no custom read/write targets are detected, leading to failed conversions.
The same problem prevents a registration in CustomConversions as a Function<ConversionService, GenericConverter> or similar, which would allow late registration.
The problem here is that custom read/write targets require the function to get evaluated, before custom read/write targets can be determined.

Closes #1750
Original pull request: #1785
mp911de added a commit that referenced this issue May 15, 2024
mp911de added a commit that referenced this issue May 15, 2024
Reduce test element visibility.

See #1750
Original pull request: #1785
mp911de added a commit that referenced this issue May 15, 2024
mp911de added a commit that referenced this issue May 15, 2024
Reduce test element visibility.

See #1750
Original pull request: #1785
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants