Skip to content

Commit

Permalink
Only apply declared join specs to write operations. Fixes #398 (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Feb 13, 2020
1 parent dffd9b8 commit 80a7008
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 6 deletions.
Expand Up @@ -139,7 +139,14 @@ public MethodMatchInfo buildMatchInfo(@NonNull MethodMatchContext matchContext)
}
}

List<AnnotationValue<Join>> joinSpecs = matchContext.getAnnotationMetadata().getAnnotationValuesByType(Join.class);
List<AnnotationValue<Join>> joinSpecs;

final MethodMatchInfo.OperationType operationType = getOperationType();
if (operationType != MethodMatchInfo.OperationType.QUERY) {
joinSpecs = matchContext.getAnnotationMetadata().getDeclaredAnnotationValuesByType(Join.class);
} else {
joinSpecs = matchContext.getAnnotationMetadata().getAnnotationValuesByType(Join.class);
}
if (CollectionUtils.isNotEmpty(joinSpecs)) {
if (query == null) {
query = QueryModel.from(rootEntity);
Expand Down
Expand Up @@ -513,6 +513,13 @@ protected RawQuery buildRawQuery(@NonNull MethodMatchContext matchContext) {
return new RawQuery(matchContext.getRootEntity(), parameterBinding);
}

/**
* @return The operation type
*/
protected @NonNull MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.QUERY;
}

/**
* Internally used for dynamically defining a class element.
*/
Expand Down
Expand Up @@ -44,6 +44,12 @@ public DeleteByMethod() {
super(PREFIXES);
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.DELETE;
}

@Override
public boolean isMethodMatch(MethodElement methodElement, MatchContext matchContext) {
return super.isMethodMatch(methodElement, matchContext) && TypeUtils.isValidBatchUpdateReturnType(methodElement); // void return
Expand Down
Expand Up @@ -62,6 +62,12 @@ public boolean isMethodMatch(MethodElement methodElement, MatchContext matchCont
TypeUtils.isValidBatchUpdateReturnType(methodElement);
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.DELETE;
}

@Nullable
@Override
public MethodMatchInfo buildMatchInfo(@NonNull MethodMatchContext matchContext) {
Expand All @@ -88,7 +94,7 @@ public MethodMatchInfo buildMatchInfo(@NonNull MethodMatchContext matchContext)
null,
null,
getInterceptorElement(matchContext, interceptor),
MethodMatchInfo.OperationType.DELETE
getOperationType()
);
} else {
QueryModel queryModel = QueryModel.from(rootEntity);
Expand All @@ -107,7 +113,7 @@ public MethodMatchInfo buildMatchInfo(@NonNull MethodMatchContext matchContext)
null,
queryModel,
getInterceptorElement(matchContext, interceptor),
MethodMatchInfo.OperationType.DELETE
getOperationType()
);
}
}
Expand All @@ -124,14 +130,14 @@ protected MethodMatchInfo buildInfo(@NonNull MethodMatchContext matchContext, @N
null,
query,
getInterceptorElement(matchContext, interceptor),
MethodMatchInfo.OperationType.DELETE
getOperationType()
);
} else {
return new MethodMatchInfo(
null,
matchContext.supportsImplicitQueries() ? null : QueryModel.from(matchContext.getRootEntity()),
getInterceptorElement(matchContext, interceptor),
MethodMatchInfo.OperationType.DELETE
getOperationType()
);
}
}
Expand Down
Expand Up @@ -273,7 +273,15 @@ public MethodMatchInfo buildMatchInfo(@NonNull MethodMatchContext matchContext)
QueryModel query = QueryModel.from(entity);
ClassElement queryResultType = entity.getClassElement();

List<AnnotationValue<Join>> joinSpecs = methodElement.getAnnotationValuesByType(Join.class);
List<AnnotationValue<Join>> joinSpecs;

final MethodMatchInfo.OperationType operationType = getOperationType();
if (operationType != MethodMatchInfo.OperationType.QUERY) {
joinSpecs = matchContext.getAnnotationMetadata().getDeclaredAnnotationValuesByType(Join.class);
} else {
joinSpecs = matchContext.getAnnotationMetadata().getAnnotationValuesByType(Join.class);
}

if (CollectionUtils.isNotEmpty(joinSpecs)) {
if (applyJoinSpecs(matchContext, query, entity, joinSpecs)) {
return null;
Expand Down
Expand Up @@ -47,6 +47,12 @@ public SaveAllMethod() {
super(Pattern.compile(METHOD_PATTERN));
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.INSERT;
}

@Override
public boolean isMethodMatch(MethodElement methodElement, MatchContext matchContext) {
ParameterElement[] parameters = methodElement.getParameters();
Expand Down
Expand Up @@ -51,6 +51,12 @@ public SaveEntityMethod() {
super(METHOD_PATTERN);
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.INSERT;
}

@Override
public boolean isMethodMatch(MethodElement methodElement, MatchContext matchContext) {
ParameterElement[] parameters = matchContext.getParameters();
Expand Down
Expand Up @@ -52,6 +52,12 @@ public SaveOneMethod() {
super(SaveEntityMethod.METHOD_PATTERN);
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.INSERT;
}

@Override
public boolean isMethodMatch(@NonNull MethodElement methodElement, @NonNull MatchContext matchContext) {
ParameterElement[] parameters = matchContext.getParameters();
Expand Down
Expand Up @@ -47,6 +47,12 @@ public UpdateByMethod() {
super("update");
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.UPDATE;
}

@Override
public boolean isMethodMatch(MethodElement methodElement, MatchContext matchContext) {
return super.isMethodMatch(methodElement, matchContext) && TypeUtils.isValidBatchUpdateReturnType(methodElement);
Expand Down
Expand Up @@ -42,6 +42,12 @@ public UpdateEntityMethod() {
super(METHOD_PATTERN);
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.UPDATE;
}

@Override
public int getOrder() {
return DEFAULT_POSITION - 100;
Expand Down
Expand Up @@ -53,6 +53,12 @@ public UpdateMethod() {
super(Pattern.compile("^update\\w*$"));
}

@NonNull
@Override
protected MethodMatchInfo.OperationType getOperationType() {
return MethodMatchInfo.OperationType.UPDATE;
}

@Override
public boolean isMethodMatch(MethodElement methodElement, MatchContext matchContext) {
return super.isMethodMatch(methodElement, matchContext) &&
Expand Down
Expand Up @@ -9,6 +9,42 @@ import spock.lang.Unroll

class BuildQuerySpec extends AbstractDataSpec {

void "test to-many join on repository type that inherits from CrudRepository"() {
given:
def repository = buildRepository('test.MyInterface', """
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.tck.entities.*;
@JdbcRepository(dialect= Dialect.MYSQL)
@Join("books")
interface MyInterface extends CrudRepository<Author, Long> {
}
"""
)

expect:"The repository to compile"
repository != null
}

void "test to-one join on repository type that inherits from CrudRepository"() {
given:
def repository = buildRepository('test.MyInterface', """
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.tck.entities.Book;
@JdbcRepository(dialect= Dialect.MYSQL)
@Join("author")
interface MyInterface extends CrudRepository<Book, Long> {
}
"""
)

expect:"The repository to compile"
repository != null
}

void "test join query on collection with custom ID name"() {
given:
def repository = buildRepository('test.MealRepository', """
Expand Down
Expand Up @@ -26,6 +26,22 @@ import spock.lang.Unroll
class JpaJoinSpec extends AbstractDataSpec {


void "test join on repository type that inherits from CrudRepository"() {
given:
def repository = buildRepository('test.MyInterface', """
import io.micronaut.data.tck.entities.Book;
@Repository
@Join("author")
interface MyInterface extends CrudRepository<Book, Long> {
}
"""
)

expect:"The repository to compile"
repository != null
}

@Unroll
void "test JPA projection across nested property path for #method"() {
given:
Expand Down

0 comments on commit 80a7008

Please sign in to comment.