Skip to content

Commit

Permalink
Fix NullPointerException in EntityGraphMapper::mapEntityReference (
Browse files Browse the repository at this point in the history
…#1134)

---------

Co-authored-by: Michael Simons <[email protected]>
  • Loading branch information
oxisto and michael-simons authored Aug 19, 2024
1 parent 480c2d8 commit bb9e29a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ private void mapEntityReferences(final Object entity, NodeBuilder nodeBuilder, i

if (isRelationshipEntity(relatedObject)) {
ClassInfo declaredObjectInfo = metaData.classInfo(relationshipType);
if (declaredObjectInfo.isAbstract()) {
if (declaredObjectInfo != null && declaredObjectInfo.isAbstract()) {
final ClassInfo relatedObjectClassInfo = metaData.classInfo(relatedObject);
if (!relatedObjectClassInfo.neo4jName().equals(directedRelationship.type())) {
directedRelationship = new DirectedRelationship(relatedObjectClassInfo.neo4jName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.neo4j.ogm.context.EntityMapper;
import org.neo4j.ogm.context.MappingContext;
import org.neo4j.ogm.cypher.compiler.Compiler;
import org.neo4j.ogm.cypher.query.CypherQuery;
import org.neo4j.ogm.domain.blog.Post;
import org.neo4j.ogm.domain.education.Course;
import org.neo4j.ogm.domain.education.School;
Expand All @@ -58,6 +59,11 @@
import org.neo4j.ogm.persistence.examples.versioned_rel_entity.A;
import org.neo4j.ogm.persistence.examples.versioned_rel_entity.B;
import org.neo4j.ogm.persistence.examples.versioned_rel_entity.R;
import org.neo4j.ogm.persistence.model.gh1134.RelEntity;
import org.neo4j.ogm.persistence.model.gh1134.NodeWithSomewhatInvalidRelationshipDeclaration;
import org.neo4j.ogm.request.OptimisticLockingConfig;
import org.neo4j.ogm.request.Statement;
import org.neo4j.ogm.request.StatementFactory;
import org.neo4j.ogm.request.Statements;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
Expand Down Expand Up @@ -943,6 +949,37 @@ void shouldNotMessUpMixedTypedLists() {
});
}

@Test
void mappingEntityRefMustCheckForNullRel() {

mappingMetadata = new MetaData("org.neo4j.ogm.persistence.model.gh1134");
mappingContext = new MappingContext(mappingMetadata);

EntityGraphMapper mapper = new EntityGraphMapper(mappingMetadata, mappingContext);
NodeWithSomewhatInvalidRelationshipDeclaration start = new NodeWithSomewhatInvalidRelationshipDeclaration();
start.setId("s");
NodeWithSomewhatInvalidRelationshipDeclaration end = new NodeWithSomewhatInvalidRelationshipDeclaration();
end.setId("e");
RelEntity rel = new RelEntity(start, end);
start.setRelatedObject(rel);

Compiler compiler = mapper.map(start).getCompiler();
compiler.useStatementFactory(new StatementFactory() {
@Override
public Statement statement(String statement, Map<String, Object> parameters) {
return new CypherQuery(statement, parameters);
}

@Override
public Statement statement(String statement, Map<String, Object> parameters,
OptimisticLockingConfig config) {
return new CypherQuery(statement, parameters);
}
});
assertThat(compiler.createRelationshipsStatements().stream().map(Statement::getStatement)).first()
.isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`whatever`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
}

@Test // GH-902
@SuppressWarnings("unchecked")
void nestedListsWithDomainModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.persistence.model.gh1134;

import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;

@NodeEntity
public class NodeWithSomewhatInvalidRelationshipDeclaration {
@Id
private String id;

@Relationship(type = "whatever")
private RelEntity relatedObject;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public RelEntity getRelatedObject() {
return relatedObject;
}

public void setRelatedObject(RelEntity relatedObject) {
this.relatedObject = relatedObject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.persistence.model.gh1134;

import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;

@RelationshipEntity
public class RelEntity {

public Long id;

@StartNode
private NodeWithSomewhatInvalidRelationshipDeclaration start;

@EndNode
private NodeWithSomewhatInvalidRelationshipDeclaration end;

public RelEntity(
NodeWithSomewhatInvalidRelationshipDeclaration start, NodeWithSomewhatInvalidRelationshipDeclaration end) {
this.end = end;
this.start = start;
}
}

0 comments on commit bb9e29a

Please sign in to comment.