GH-2622 - Throw exception on immutable self-reference.

This slipped through the check before:
A has dependency on A defined within the constructor.
Because of a "same" entity check, we skipped the `inCreation` check
and did not throw the MappingException.

Closes #2622
This commit is contained in:
Gerrit Meier
2023-01-23 15:54:27 +01:00
parent 28cc45a3fb
commit c4a3f1f42d
4 changed files with 110 additions and 8 deletions

View File

@@ -313,14 +313,6 @@ final class DefaultNeo4jEntityConverter implements Neo4jEntityConverter {
String internalId = IdentitySupport.getInternalId(queryResult, direction);
Supplier<ET> mappedObjectSupplier = () -> {
if (knownObjects.isInCreation(internalId)) {
throw new MappingException(
String.format(
"The node with id %s has a logical cyclic mapping dependency; " +
"its creation caused the creation of another node that has a reference to this",
internalId.substring(1))
);
}
knownObjects.setInCreation(internalId);
List<String> allLabels = getLabels(queryResult, nodeDescription);
@@ -945,6 +937,14 @@ final class DefaultNeo4jEntityConverter implements Neo4jEntityConverter {
}
try {
read.lock();
if (isInCreation(internalId)) {
throw new MappingException(
String.format(
"The node with id %s has a logical cyclic mapping dependency; " +
"its creation caused the creation of another node that has a reference to this",
internalId.substring(1))
);
}
return internalIdStore.get(internalId);
} finally {
read.unlock();

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.neo4j.integration.issues;
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.tuple;
@@ -57,6 +58,7 @@ import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
import org.springframework.data.neo4j.core.Neo4jTemplate;
@@ -128,6 +130,8 @@ import org.springframework.data.neo4j.integration.issues.gh2579.TableNode;
import org.springframework.data.neo4j.integration.issues.gh2579.TableRepository;
import org.springframework.data.neo4j.integration.issues.gh2583.GH2583Node;
import org.springframework.data.neo4j.integration.issues.gh2583.GH2583Repository;
import org.springframework.data.neo4j.integration.issues.gh2622.GH2622Repository;
import org.springframework.data.neo4j.integration.issues.gh2622.MePointingTowardsMe;
import org.springframework.data.neo4j.integration.issues.gh2639.Company;
import org.springframework.data.neo4j.integration.issues.gh2639.CompanyPerson;
import org.springframework.data.neo4j.integration.issues.gh2639.CompanyRepository;
@@ -991,6 +995,20 @@ class IssuesIT extends TestBase {
);
}
@Test
@Tag("GH-2622")
void throwCyclicMappingDependencyExceptionOnSelfReference(@Autowired GH2622Repository repository) {
MePointingTowardsMe entity = new MePointingTowardsMe("me", new ArrayList<>());
entity.others.add(entity);
repository.save(entity);
assertThatExceptionOfType(MappingException.class).isThrownBy(repository::findAll)
.withRootCauseInstanceOf(MappingException.class)
.extracting(Throwable::getCause, as(InstanceOfAssertFactories.THROWABLE))
.hasMessageContaining("has a logical cyclic mapping dependency");
}
@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(namedQueriesLocation = "more-custom-queries.properties")

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2011-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.integration.issues.gh2622;
import org.springframework.data.neo4j.repository.Neo4jRepository;
/**
* @author Gerrit Meier
*/
public interface GH2622Repository extends Neo4jRepository<MePointingTowardsMe, Long> { }

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2011-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.integration.issues.gh2622;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import java.util.List;
import java.util.Objects;
/**
* @author Gerrit Meier
*/
@Node
public class MePointingTowardsMe {
@Id
@GeneratedValue
Long id;
final String name;
@Relationship
public final List<MePointingTowardsMe> others;
public MePointingTowardsMe(String name, List<MePointingTowardsMe> others) {
this.name = name;
this.others = others;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MePointingTowardsMe that = (MePointingTowardsMe) o;
return name.equals(that.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}