-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add config option to disable query validation #3642
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.Query; | ||
|
||
import org.springframework.core.SpringProperties; | ||
import org.springframework.data.jpa.repository.QueryRewriter; | ||
import org.springframework.data.repository.query.RepositoryQuery; | ||
import org.springframework.data.repository.query.ValueExpressionDelegate; | ||
|
@@ -45,8 +46,8 @@ final class SimpleJpaQuery extends AbstractStringBasedJpaQuery { | |
* @param valueExpressionDelegate must not be {@literal null} | ||
*/ | ||
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, @Nullable String countQueryString, | ||
QueryRewriter queryRewriter, ValueExpressionDelegate valueExpressionDelegate) { | ||
this(method, em, method.getRequiredAnnotatedQuery(), countQueryString, queryRewriter, valueExpressionDelegate); | ||
QueryRewriter queryRewriter, ValueExpressionDelegate valueExpressionDelegate, boolean validation) { | ||
this(method, em, method.getRequiredAnnotatedQuery(), countQueryString, queryRewriter, valueExpressionDelegate, validation); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a |
||
} | ||
|
||
/** | ||
|
@@ -60,15 +61,17 @@ public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, @Nullable String | |
* @param valueExpressionDelegate must not be {@literal null} | ||
*/ | ||
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryString, @Nullable String countQueryString, QueryRewriter queryRewriter, | ||
ValueExpressionDelegate valueExpressionDelegate) { | ||
ValueExpressionDelegate valueExpressionDelegate, boolean validation) { | ||
|
||
super(method, em, queryString, countQueryString, queryRewriter, valueExpressionDelegate); | ||
|
||
validateQuery(getQuery().getQueryString(), "Validation failed for query for method %s", method); | ||
if(validation) { | ||
validateQuery(getQuery().getQueryString(), "Validation failed for query for method %s", method); | ||
|
||
if (method.isPageQuery()) { | ||
validateQuery(getCountQuery().getQueryString(), | ||
if (method.isPageQuery()) { | ||
validateQuery(getCountQuery().getQueryString(), | ||
String.format("Count query validation failed for method %s", method)); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import org.springframework.beans.BeanUtils; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.core.SpringProperties; | ||
import org.springframework.dao.InvalidDataAccessApiUsageException; | ||
import org.springframework.data.jpa.projection.CollectionAwareProjectionFactory; | ||
import org.springframework.data.jpa.provider.PersistenceProvider; | ||
|
@@ -90,6 +91,7 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport { | |
private EscapeCharacter escapeCharacter = EscapeCharacter.DEFAULT; | ||
private JpaQueryMethodFactory queryMethodFactory; | ||
private QueryRewriterProvider queryRewriterProvider; | ||
private boolean queryValidation; | ||
|
||
/** | ||
* Creates a new {@link JpaRepositoryFactory}. | ||
|
@@ -120,6 +122,8 @@ public JpaRepositoryFactory(EntityManager entityManager) { | |
} | ||
|
||
this.crudMethodMetadata = crudMethodMetadataPostProcessor.getCrudMethodMetadata(); | ||
String p = SpringProperties.getProperty("spring.data.jpa.query.validate"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not apply any defaulting from inside our library. |
||
this.queryValidation = p == null || Boolean.parseBoolean(p); | ||
} | ||
|
||
@Override | ||
|
@@ -167,6 +171,15 @@ public void setEscapeCharacter(EscapeCharacter escapeCharacter) { | |
this.escapeCharacter = escapeCharacter; | ||
} | ||
|
||
/** | ||
* Configures if spring based queries should be validated on creation | ||
* | ||
* @param queryValidation a character used for escaping in certain like expressions. | ||
* @since 3.4 | ||
*/ | ||
public void setQueryValidation(boolean queryValidation) { | ||
this.queryValidation = queryValidation; | ||
} | ||
/** | ||
* Configures the {@link JpaQueryMethodFactory} to be used. Defaults to {@link DefaultJpaQueryMethodFactory}. | ||
* | ||
|
@@ -240,7 +253,7 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable Key key | |
ValueExpressionDelegate valueExpressionDelegate) { | ||
return Optional.of(JpaQueryLookupStrategy.create(entityManager, queryMethodFactory, key, | ||
new CachingValueExpressionDelegate(valueExpressionDelegate), | ||
queryRewriterProvider, escapeCharacter)); | ||
queryRewriterProvider, escapeCharacter, queryValidation)); | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need that one since we're not updating
@EnableJpaRepositories