From 78d47ba75f4e5ddea8a622e458ea069e260db9f1 Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Fri, 8 Nov 2024 14:14:54 +0100 Subject: [PATCH] fix: Be lenient with ambiguity when checking for resolved classes. If several types are resolved and only one of them is a concrete class and the rest interfaces implemented in that class, we can savely assume that it is exactly that what the combination of labels asked for. Fixes #1156. --- .../java/org/neo4j/ogm/metadata/MetaData.java | 8 ++ .../org/neo4j/ogm/domain/gh1156/Canvas.java | 36 ++++++ .../org/neo4j/ogm/domain/gh1156/UObject.java | 34 +++++ .../org/neo4j/ogm/domain/gh1156/Window.java | 120 ++++++++++++++++++ .../examples/pizza/PizzaIntegrationTest.java | 15 +++ 5 files changed, 213 insertions(+) create mode 100644 neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Canvas.java create mode 100644 neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/UObject.java create mode 100644 neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Window.java diff --git a/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java b/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java index c2468e5d4..9e8914f77 100644 --- a/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java +++ b/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.neo4j.ogm.annotation.NodeEntity; import org.neo4j.ogm.annotation.RelationshipEntity; @@ -205,6 +206,13 @@ public ClassInfo resolve(String... taxa) { } } if (resolved.size() > 1) { + // if all resolved classes are found in the taxa, and only one of them is a concrete class, we can assume + // that one matches the thing to be loaded + Map> interfacesAndClasses = resolved.stream().collect(Collectors.partitioningBy(ClassInfo::isInterface)); + if (interfacesAndClasses.get(false).size() == 1) { + return interfacesAndClasses.get(false).get(0); + } + // Sort so we always get the same order String[] sorted = Arrays.copyOf(taxa, taxa.length); Arrays.sort(sorted); diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Canvas.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Canvas.java new file mode 100644 index 000000000..23957fa9a --- /dev/null +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Canvas.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2002-2024 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * 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 + * + * http://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.neo4j.ogm.domain.gh1156; + +import java.io.Serializable; +import java.util.Collection; +import org.neo4j.ogm.annotation.NodeEntity; + +@NodeEntity +public interface Canvas extends Serializable, UObject { + + public void setSuid(java.lang.String suid); + + public String getSuid(); + + public void setCanvasName(java.lang.String canvasName); + + public String getCanvasName(); + +} diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/UObject.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/UObject.java new file mode 100644 index 000000000..5bfe41243 --- /dev/null +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/UObject.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2002-2024 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * 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 + * + * http://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.neo4j.ogm.domain.gh1156; + +import java.io.Serializable; +import org.neo4j.ogm.annotation.NodeEntity; + +@NodeEntity +public interface UObject extends Serializable { + + public void setSuid(java.lang.String suid); + + public String getSuid(); + + public void setObjName(java.lang.String objName); + + public String getObjName(); +} diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Window.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Window.java new file mode 100644 index 000000000..632f8621a --- /dev/null +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Window.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2002-2024 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * 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 + * + * http://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.neo4j.ogm.domain.gh1156; + +import java.io.Serializable; +import org.neo4j.ogm.annotation.Id; +import org.neo4j.ogm.annotation.GeneratedValue; +import org.neo4j.ogm.annotation.Version; +import org.neo4j.ogm.annotation.Property; +import org.neo4j.ogm.annotation.NodeEntity; + +@NodeEntity +public class Window implements Serializable, Canvas { + + private static final long serialVersionUID = 1L; + + private String suid_ = java.util.UUID.randomUUID().toString(); + + public Class getTypeClazz() { + return Window.class; + } + + @Id @GeneratedValue public Long __id; + + @Version + @Property(name = "V_WINDOW") + private long vWindow; + + public long getVwindow() { + return vWindow; + } + + public void setVwindow(long vWindow) { + this.vWindow = vWindow; + } + + @Property(name = "UID") + private String uid; + + public void setUid(java.lang.String uid) { + this.uid = uid; + } + + public String getUid() { + return uid; + } + + @Property(name = "NAME") + private String name; + + public void setName(java.lang.String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setSuidCanvas(java.lang.String suid) { + this.suid_ = suid; + } + + public String getSuidCanvas() { + return this.suid_; + } + + public void setSuid(java.lang.String suid) { + this.suid_ = suid; + } + + public String getSuid() { + return this.suid_; + } + + @Property(name = "CANVAS_NAME") + private String canvasName; + + public void setCanvasName(java.lang.String canvasName) { + this.canvasName = canvasName; + } + + public String getCanvasName() { + return canvasName; + } + + public void setSuidUObject(java.lang.String suid) { + this.suid_ = suid; + } + + public String getSuidUObject() { + return this.suid_; + } + + @Property(name = "OBJ_NAME") + private String objName; + + public void setObjName(java.lang.String objName) { + this.objName = objName; + } + + public String getObjName() { + return objName; + } +} diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java index 9a9e6caec..666d27198 100644 --- a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java @@ -31,6 +31,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.neo4j.ogm.domain.gh1156.Window; import org.neo4j.ogm.domain.pizza.*; import org.neo4j.ogm.exception.core.MappingException; import org.neo4j.ogm.model.Result; @@ -355,6 +356,20 @@ public void shouldRaiseExceptionWhenAmbiguousClassLabelApplied() { } } + @Test // GH-1156 + public void multipleLabelsButOnlyOneConcreteClassShouldNotFail() { + Session sessionWithAmbiguousDomain = new SessionFactory(getDriver(), "org.neo4j.ogm.domain.gh1156") + .openSession(); + + Window window = new Window(); + + sessionWithAmbiguousDomain.save(window); + sessionWithAmbiguousDomain.clear(); + + Window loaded = sessionWithAmbiguousDomain.load(Window.class, window.__id); + assertThat(loaded).isNotNull(); + } + @Test public void shouldUpdateSessionContextAfterSaveForSingleAndMultiStatementCypherQueries() {