Skip to content

Commit

Permalink
fix: Be lenient with ambiguity when checking for resolved classes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michael-simons committed Nov 8, 2024
1 parent b109000 commit dcdabb9
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/org/neo4j/ogm/metadata/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -204,6 +205,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
var 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.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;
Expand Down Expand Up @@ -359,6 +360,20 @@ void shouldRaiseExceptionWhenAmbiguousClassLabelApplied() {
}
}

@Test // GH-1156
void multipleLabelsButOnlyOneConcreteClassShouldNotFail() {
Session sessionWithAmbiguousDomain = new SessionFactory(getDriver(), "org.neo4j.ogm.domain.gh1156")
.openSession();

Window window = new Window();

sessionWithAmbiguousDomain.save(window);
sessionWithAmbiguousDomain.clear();

var loaded = sessionWithAmbiguousDomain.load(Window.class, window.__id);
assertThat(loaded).isNotNull();
}

@Test
void shouldUpdateSessionContextAfterSaveForSingleAndMultiStatementCypherQueries() {

Expand Down

0 comments on commit dcdabb9

Please sign in to comment.