Skip to content

Commit

Permalink
Repo id and domain class id being a number excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Dec 22, 2023
1 parent 8a74023 commit 1665b3f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblemImpl;

public class EntityIdForRepoReconciler implements JdtAstReconciler {

private static final List<String> NUMBER_CLASS_NAMES = List.of(
Integer.class.getName(),
Long.class.getName(),
Short.class.getName(),
Float.class.getName(),
Double.class.getName(),
Byte.class.getName()
);

@Override
public void reconcile(IJavaProject project, URI docUri, CompilationUnit cu, IProblemCollector problemCollector,
Expand Down Expand Up @@ -253,7 +262,10 @@ private List<ITypeBinding> findIdType(ITypeBinding type) {
}

private boolean isValidRepoIdType(ITypeBinding repoIdType, ITypeBinding idType) {
return repoIdType.isAssignmentCompatible(idType) || idType.isAssignmentCompatible(repoIdType);
if (NUMBER_CLASS_NAMES.contains(repoIdType.getQualifiedName()) && NUMBER_CLASS_NAMES.contains(idType.getQualifiedName())) {
return true;
}
return repoIdType.isCastCompatible(idType) || idType.isCastCompatible(repoIdType);
}

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,7 @@ interface EmployeeRepository {}
""";
List<ReconcileProblem> problems = reconcile("EmployeeRepository.java", source, false, employeeSource);

assertEquals(1, problems.size());

ReconcileProblem problem = problems.get(0);

assertEquals(Boot2JavaProblemType.DOMAIN_ID_FOR_REPOSITORY, problem.getType());

String markedStr = source.substring(problem.getOffset(), problem.getOffset() + problem.getLength());
assertEquals("Double.class", markedStr);
assertEquals("Expected Domain ID type is 'java.lang.Integer'", problem.getMessage());

assertEquals(0, problem.getQuickfixes().size());

assertEquals(0, problems.size());
}

@Test
Expand Down Expand Up @@ -1118,4 +1107,49 @@ interface CustomerRepository extends Repository<Customer, Long> {}
assertEquals("Expected Domain ID type is 'demo.CustomerId'", problem.getMessage());

}

@Test
void gh1159() throws Exception {
Path roleSource = createFile("Role.java", """
package demo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "roles")
public class Role {
@Id
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
""");

String source = """
package demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RoleRepository extends JpaRepository<Role, Long> {
}
""";
List<ReconcileProblem> problems = reconcile("CustomerRepository.java", source, false, roleSource);

assertEquals(0, problems.size());

}
}

0 comments on commit 1665b3f

Please sign in to comment.