Skip to content

Commit 9025ca3

Browse files
committed
Skip fenced comments in HQL, EQL and JPQL parsers.
Align with Hibernate and allow comments also in EQL and JPQL. Closes #3997
1 parent f50d356 commit 9025ca3

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Eql.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ reserved_word
883883

884884

885885
WS : [ \t\r\n] -> channel(HIDDEN) ;
886+
COMMENT : '/*' (~'*' | '*' ~'/' )* '*/' -> skip;
886887

887888
// Build up case-insentive tokens
888889

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ identifier
17791779

17801780

17811781
WS : [ \t\r\n] -> channel(HIDDEN);
1782+
COMMENT : '/*' (~'*' | '*' ~'/' )* '*/' -> skip;
17821783

17831784
// Build up case-insentive tokens
17841785

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Jpql.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ reserved_word
885885

886886

887887
WS : [ \t\r\n] -> channel(HIDDEN) ;
888+
COMMENT : '/*' (~'*' | '*' ~'/' )* '*/' -> skip;
888889

889890
// Build up case-insentive tokens
890891

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryEnhancer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
* @see HqlQueryParser
4747
* @see EqlQueryParser
4848
*/
49-
@SuppressWarnings("removal")
5049
class JpaQueryEnhancer<Q extends QueryInformation> implements QueryEnhancer {
5150

5251
private final ParserRuleContext context;
@@ -215,7 +214,8 @@ public String getProjection() {
215214
*/
216215
@Override
217216
public DeclaredQuery getQuery() {
218-
throw new UnsupportedOperationException();
217+
QueryTokenStream tokens = sortFunction.apply(Sort.unsorted(), this.queryInformation, null).visit(context);
218+
return DeclaredQuery.jpqlQuery(QueryRenderer.TokenRenderer.render(tokens));
219219
}
220220

221221
@Override
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.query;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.function.Function;
21+
import java.util.stream.Stream;
22+
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.MethodSource;
25+
26+
/**
27+
* Unit tests for {@link JpaQueryEnhancer}.
28+
*
29+
* @author Mark Paluch
30+
*/
31+
class JpaQueryEnhancerUnitTests {
32+
33+
@ParameterizedTest // GH-3997
34+
@MethodSource("queryEnhancers")
35+
void shouldRemoveCommentsFromJpql(Function<String, JpaQueryEnhancer<?>> enhancerFunction) {
36+
37+
QueryEnhancer enhancer = enhancerFunction
38+
.apply("SELECT /* foo */ some_alias FROM /* some other */ table_name some_alias");
39+
40+
assertThat(enhancer.getQuery().getQueryString())
41+
.isEqualToIgnoringCase("SELECT some_alias FROM table_name some_alias");
42+
43+
enhancer = enhancerFunction.apply("""
44+
SELECT /* multi
45+
line
46+
comment
47+
*/ some_alias FROM /* some other */ table_name some_alias
48+
""");
49+
50+
assertThat(enhancer.getQuery().getQueryString())
51+
.isEqualToIgnoringCase("SELECT some_alias FROM table_name some_alias");
52+
}
53+
54+
static Stream<Function<String, JpaQueryEnhancer<?>>> queryEnhancers() {
55+
return Stream.of(JpaQueryEnhancer::forHql, JpaQueryEnhancer::forEql, JpaQueryEnhancer::forJpql);
56+
}
57+
}

0 commit comments

Comments
 (0)