Skip to content

Commit

Permalink
Ensure that new line chars don't break Panache projection
Browse files Browse the repository at this point in the history
Fixes: quarkusio#29838
(cherry picked from commit 756568a)
  • Loading branch information
geoand authored and gsmet committed Jan 9, 2023
1 parent 63e8b33 commit d3db1d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Expand Up @@ -54,6 +54,8 @@ public void close() {

private Map<String, Map<String, Object>> filters;

private final String lineSeparator = System.getProperty("line.separator");

public CommonPanacheQueryImpl(EntityManager em, String query, String orderBy, Object paramsArrayOrMap) {
this.em = em;
this.query = query;
Expand Down Expand Up @@ -82,7 +84,7 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
throw new PanacheQueryException("Unable to perform a projection on a named query");
}

String lowerCasedTrimmedQuery = query.trim().toLowerCase();
String lowerCasedTrimmedQuery = query.trim().replace(lineSeparator, " ").toLowerCase();
if (lowerCasedTrimmedQuery.startsWith("select new ")) {
throw new PanacheQueryException("Unable to perform a projection on a 'select new' query: " + query);
}
Expand All @@ -93,7 +95,7 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
// New query: SELECT new org.acme.ProjectionClass(e.field1, e.field2) from EntityClass e
if (lowerCasedTrimmedQuery.startsWith("select ")) {
int endSelect = lowerCasedTrimmedQuery.indexOf(" from ");
String trimmedQuery = query.trim();
String trimmedQuery = query.trim().replace(lineSeparator, " ");
// 7 is the length of "select "
String selectClause = trimmedQuery.substring(7, endSelect);
String from = trimmedQuery.substring(endSelect);
Expand Down
Expand Up @@ -44,6 +44,7 @@
@Path("test")
public class TestEndpoint {

private static final String LINE_SEPARATOR = System.getProperty("line.separator");
// fake unused injection point to force ArC to not remove this otherwise I can't mock it in the tests
@Inject
MockablePersonRepository mockablePersonRepository;
Expand Down Expand Up @@ -1187,6 +1188,13 @@ public String testProjection() {
person = Person.find("name = ?1", "2").project(PersonName.class).firstResult();
Assertions.assertEquals("2", person.name);

person = Person.find(String.format(
"select uniqueName, name%sfrom io.quarkus.it.panache.Person%swhere name = ?1",
LINE_SEPARATOR, LINE_SEPARATOR), "2")
.project(PersonName.class)
.firstResult();
Assertions.assertEquals("2", person.name);

person = Person.find("name = :name", Parameters.with("name", "2")).project(PersonName.class).firstResult();
Assertions.assertEquals("2", person.name);

Expand Down

0 comments on commit d3db1d7

Please sign in to comment.