Closed
Description
Hello,
I wanted to fetch data from the database by a specific enum. It works fine if I don't use the custom @Query
annotation.
But if I do so, then I get a this error:
Cannot encode parameter of type com.example.demo.MyType
It does not work in CoroutineCrudRepository
nor does it in ReactiveCrudRepository
.
Here is an example:
// enum
public enum MyType {
HOUSE,
STREET,
ROAD,
CITY
}
// model
@Table("my_table")
public class MyEntity {
@Id
private Integer id;
private MyType type;
// getters and setters
}
// repository
interface MyRepository extends ReactiveCrudRepository<MyEntity, Integer> {
Flux<MyEntity> findByType(MyType type); // works fine
@Query("SELECT * FROM my_table WHERE type = :type")
Flux<MyEntity> findCustom(MyType type); // error
}
// controller
@RestController
public class MyController {
private final MyRepository repository;
public MyController(MyRepository repository) {
this.repository = repository;
}
@GetMapping("/a")
public Flux<MyEntity> getA() {
return repository.findByType(MyType.HOUSE);
}
@GetMapping("/b")
public Flux<MyEntity> getB() {
return repository.findCustom(MyType.HOUSE); // error
}
}
-- table
CREATE TABLE my_table (
id SERIAL PRIMARY KEY,
type TEXT NOT NULL
);
Version: 1.2.0-RC1
Database: PostgreSQL
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
mp911de commentedon Sep 21, 2020
Can you provide also a stack trace?
Tienisto commentedon Sep 21, 2020
Sure
mp911de commentedon Sep 21, 2020
Thanks a lot! We apply input-value to R2DBC driver-compliant value conversion for derived queries. For String-based queries, we don't apply that type of value conversion. We should fix that.
[-]Decode enums in @Query[/-][+]Enum values used through @Query not converted to String[/+]parikshitdutta commentedon Sep 22, 2020
@mp911de can I take this one?
mp911de commentedon Sep 22, 2020
Sure, feel free to submit a pull request.
Tienisto commentedon Oct 7, 2020
Any updates here? It would be nice if I could use this in the next spring release :)
mp911de commentedon Oct 7, 2020
What a coincidence. I just opened this ticket and started building a test case since there was no movement for two weeks.
#465 - Convert bind values for String-based queries to their native t…
#465 - Polishing.
mp911de commentedon Oct 7, 2020
That's fixed now.