Skip to content

Method parameters validation fails for @Converted collections used as scalars [DATAJPA-1682] #1981

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

Open
spring-projects-issues opened this issue Feb 13, 2020 · 2 comments
Assignees
Labels
type: regression A regression from a previous release

Comments

@spring-projects-issues
Copy link

ahaczewski opened DATAJPA-1682 and commented

One of the uses of Spring Data JPA in my company is to store plain Protocol Buffers messages and in @Entity we have a field of type ByteString, which is Iterable, we also have a custom @Converter created for it, which converts ByteString to Base-64 string.

Here is ByteString declaration (from ByteString JavaDoc):

public abstract class ByteString
extends Object
implements Iterable<Byte>, Serializable

Here is a custom converter that we use:

@Converter
public class ByteStringConverter implements AttributeConverter<ByteString, String> {

  @Override
  public String convertToDatabaseColumn(ByteString attribute) {
    return BaseEncoding.base64().encode(attribute.toByteArray());
  }

  @Override
  public ByteString convertToEntityAttribute(String dbData) {
    byte[] decodedBytes = BaseEncoding.base64().decode(dbData);
    return ByteString.copyFrom(decodedBytes);
  }
}

Here is our field declaration in the entity:

@Column(updatable = false, unique = true)
@Convert(converter = ByteStringConverter.class)
private ByteString signature;

Here is our repository method:

Optional<SignatureEntity> findBySignature(ByteString featuresSignature);

This setup worked perfectly fine in Spring Data JPA 2.1, but now fails with the following exception:

Caused by: java.lang.IllegalStateException: Operator SIMPLE_PROPERTY on signature requires a scalar argument, found class com.google.protobuf.ByteString in method public abstract java.util.Optional com.company.project.SignatureEntityRepository.findBySignature(com.google.protobuf.ByteString).
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.throwExceptionOnArgumentMismatch(PartTreeJpaQuery.java:171)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTreeJpaQuery.java:147)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:90)
	... 119 common frames omitted

Affects: 2.2.4 (Moore SR4)

Reference URL: #228 (comment)

3 votes, 4 watchers

@spring-projects-issues
Copy link
Author

Andreas Klein commented

We are faced with much the same regression issue.

Our persistence model makes ample use of Vlad Mihalcea's com.vladmihalcea.hibernate.type.json.``JsonNodeBinaryType, which maps a com.fasterxml.jackson.databind.JsonNode object onto a Postgres jsonb column. This has been working beautifully so far, but after attempting the update to the latest Spring Data JPA 2.2.4, the new org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTree, JpaParameters, String) method is spitting the dummy because JsonNode happens to implement Iterable.

On the whole, the validation of repo method signatures isn't a bad idea, but it should of course not mess with anybody's mappings the way it does now. It'll likely be difficult to achieve both to just work out of the box, but maybe an annotation to explicitly suppress this validation for advanced custom user types and converters could be a way out of the dead end

@spring-projects-issues
Copy link
Author

Jens Schauder commented

For expected scalar values we should check first if the parameter is assignable to the relevant property

@spring-projects-issues spring-projects-issues added the type: regression A regression from a previous release label Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

2 participants