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: #29838
  • Loading branch information
geoand committed Dec 14, 2022
1 parent 057e847 commit dac7347
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
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).trim();
String from = trimmedQuery.substring(endSelect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,11 @@ public String testProjection() {
person = Person.find("name = ?1", "2").project(PersonName.class).firstResult();
Assertions.assertEquals("2", person.name);

person = Person.find("select uniqueName, name\nfrom io.quarkus.it.panache.Person\nwhere name = ?1", "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 dac7347

Please sign in to comment.