Repo id and domain class id being a number excluded

This commit is contained in:
aboyko
2023-12-22 09:47:11 -05:00
parent 8a740236bc
commit 1665b3f51b
2 changed files with 59 additions and 13 deletions

View File

@@ -46,6 +46,15 @@ import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemTy
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,
@@ -253,7 +262,10 @@ public class EntityIdForRepoReconciler implements JdtAstReconciler {
}
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);
}
});

View File

@@ -199,18 +199,7 @@ public class EntityIdForRepoReconcilerTest extends BaseReconcilerTest {
""";
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
@@ -1118,4 +1107,49 @@ public class EntityIdForRepoReconcilerTest extends BaseReconcilerTest {
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());
}
}