Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of unknown generic types. #803

Merged
merged 2 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Map.Entry;
import java.util.function.Predicate;

import org.neo4j.ogm.exception.core.MappingException;
import org.neo4j.ogm.metadata.ClassInfo;
Expand All @@ -33,6 +34,7 @@
import org.neo4j.ogm.metadata.MetaData;
import org.neo4j.ogm.metadata.reflect.EntityAccessManager;
import org.neo4j.ogm.metadata.reflect.EntityFactory;
import org.neo4j.ogm.metadata.reflect.GenericUtils;
import org.neo4j.ogm.model.RowModel;
import org.neo4j.ogm.session.EntityInstantiator;
import org.neo4j.ogm.support.ClassUtils;
Expand Down Expand Up @@ -144,9 +146,19 @@ private void writeProperty(ClassInfo classInfo, Object instance, Map.Entry<Strin
elementType = DescriptorMappings.getType(writer.getTypeDescriptor());
}

boolean targetIsCollection = effectiveFieldType.isArray() || Iterable.class.isAssignableFrom(effectiveFieldType);
Predicate<Class<?>> isCollectionLike = c -> c != null && c.isArray() || Iterable.class.isAssignableFrom(c);
boolean targetIsCollection = isCollectionLike.test(effectiveFieldType);

Object value = property.getValue();

// In case we have not been able to determine a collection type from the the field
// but the field is generic and we received something collection like we treat
// the field as a collection anyway.
if (!targetIsCollection && GenericUtils.isGenericField(writer.getField())
&& value != null && isCollectionLike.test(value.getClass())) {
targetIsCollection = true;
}

if (metadata.classInfo(elementType) != null) {
value = mapKnownEntityType(elementType, key, value, targetIsCollection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.assertj.core.api.Condition;
Expand All @@ -38,6 +39,7 @@
import org.neo4j.ogm.domain.cineasts.minimum.SomeQueryResult;
import org.neo4j.ogm.domain.gh391.SomeContainer;
import org.neo4j.ogm.domain.gh551.AnotherThing;
import org.neo4j.ogm.domain.gh551.GenericQueryResultWrapper;
import org.neo4j.ogm.domain.gh551.ThingEntity;
import org.neo4j.ogm.domain.gh551.ThingResult;
import org.neo4j.ogm.domain.gh551.ThingResult2;
Expand Down Expand Up @@ -86,6 +88,78 @@ public static void oneTimeSetUp() {
session.save(new UserInfo("Foo", "Bar", "i@do.com"));
}

@Test
public void genericTargetTypesWithCollectionsOfUnkownThings() {

SingleUseEntityMapper entityMapper =
new SingleUseEntityMapper(sessionFactory.metaData(),
new ReflectionEntityInstantiator(sessionFactory.metaData()));

Iterable<Map<String, Object>> results;
GenericQueryResultWrapper<List<Long>> genericQueryResultWrapper;

results = sessionFactory.openSession()
.query("unwind range (0,1) as x return collect(x) as result", EMPTY_MAP)
.queryResults();
assertThat(results).hasSize(1);
genericQueryResultWrapper = entityMapper.map(GenericQueryResultWrapper.class, results.iterator().next());
assertThat(genericQueryResultWrapper.getResult())
.containsExactly(0L, 1L);

results = sessionFactory.openSession()
.query("unwind range (0,0) as x return collect(x) as result", EMPTY_MAP)
.queryResults();
assertThat(results).hasSize(1);
genericQueryResultWrapper = entityMapper.map(GenericQueryResultWrapper.class, results.iterator().next());
assertThat(genericQueryResultWrapper.getResult())
.containsExactly(0L);

results = sessionFactory.openSession()
.query("unwind range (0,-1) as x return collect(x) as result", EMPTY_MAP)
.queryResults();
assertThat(results).hasSize(1);
genericQueryResultWrapper = entityMapper.map(GenericQueryResultWrapper.class, results.iterator().next());
assertThat(genericQueryResultWrapper.getResult()).isEmpty();
}

@Test
public void genericTargetTypesWithCollectionsOfKownThings() {

SingleUseEntityMapper entityMapper =
new SingleUseEntityMapper(sessionFactory.metaData(),
new ReflectionEntityInstantiator(sessionFactory.metaData()));

Iterable<Map<String, Object>> results;
GenericQueryResultWrapper<List<ThingEntity>> genericQueryResultWrapper;

results = sessionFactory.openSession()
.query("match (r:ThingEntity) return collect (r) as result", EMPTY_MAP)
.queryResults();
assertThat(results).hasSize(1);
genericQueryResultWrapper = entityMapper.map(GenericQueryResultWrapper.class, results.iterator().next());
assertThat(genericQueryResultWrapper.getResult())
.hasSize(10)
.extracting(ThingEntity::getName)
.allSatisfy(s -> s.startsWith("Thing"));

results = sessionFactory.openSession()
.query("match (r:ThingEntity {name: 'Thing 1'}) return collect (r) as result", EMPTY_MAP)
.queryResults();
assertThat(results).hasSize(1);
genericQueryResultWrapper = entityMapper.map(GenericQueryResultWrapper.class, results.iterator().next());
assertThat(genericQueryResultWrapper.getResult())
.hasSize(1)
.extracting(ThingEntity::getName)
.allSatisfy(s -> s.startsWith("Thing"));

results = sessionFactory.openSession()
.query("unwind range (0,-1) as x return collect(x) as result", EMPTY_MAP)
.queryResults();
assertThat(results).hasSize(1);
genericQueryResultWrapper = entityMapper.map(GenericQueryResultWrapper.class, results.iterator().next());
assertThat(genericQueryResultWrapper.getResult()).isEmpty();
}

@Test // GH-551
public void singleUseEntityMapperShouldWorkWithNestedObjects() {

Expand Down Expand Up @@ -218,7 +292,8 @@ public void shouldWorkWithCustomConvertersOnListProperty() {
.queryResults();
assertThat(results).hasSize(1);
ThingResult3 thingResult = entityMapper.map(ThingResult3.class, results.iterator().next());
assertThat(thingResult.getFoobars()).extracting(ThingResult3.FooBar::getValue).containsExactlyInAnyOrder("foo", "bar");
assertThat(thingResult.getFoobars()).extracting(ThingResult3.FooBar::getValue)
.containsExactlyInAnyOrder("foo", "bar");
}

@Test // GH-718
Expand Down Expand Up @@ -294,7 +369,8 @@ public void shouldDealWithStaticInnerClasses() {

assertThat(results).hasSize(1);

SomeContainer.StaticInnerThingResult thingResult = entityMapper.map(SomeContainer.StaticInnerThingResult.class, results.iterator().next());
SomeContainer.StaticInnerThingResult thingResult = entityMapper
.map(SomeContainer.StaticInnerThingResult.class, results.iterator().next());
assertThat(thingResult.getSomething()).isEqualTo("a name");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2002-2020 "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.gh551;

/**
* @param <T>
* @author Michael J. Simons
*/
public class GenericQueryResultWrapper<T> {

T result;

public T getResult() {
return result;
}

public void setResult(T result) {
this.result = result;
}
}