Skip to content

Commit f32deb4

Browse files
anotenderigdianov
authored andcommitted
Add duplicated property test case (#178)
1 parent b89eb5f commit f32deb4

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/IntrospectionUtils.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Optional;
1818
import java.util.Spliterator;
1919
import java.util.Spliterators;
20+
import java.util.function.BinaryOperator;
2021
import java.util.function.Function;
2122
import java.util.stream.Collectors;
2223
import java.util.stream.Stream;
@@ -33,7 +34,7 @@
3334
import com.introproventures.graphql.jpa.query.schema.impl.IntrospectionUtils.EntityIntrospectionResult.AttributePropertyDescriptor;
3435

3536
public class IntrospectionUtils {
36-
private static final Logger log = LoggerFactory.getLogger(IntrospectionUtils.class);
37+
private static final Logger LOGGER = LoggerFactory.getLogger(IntrospectionUtils.class);
3738

3839
private static final Map<Class<?>, EntityIntrospectionResult> map = new LinkedHashMap<>();
3940

@@ -115,15 +116,21 @@ public EntityIntrospectionResult(Class<?> entity) {
115116

116117
this.fields = getClasses().flatMap(k -> Arrays.stream(k.getDeclaredFields()))
117118
.filter(f -> map.containsKey(Introspector.decapitalize(f.getName())))
118-
.collect(Collectors.toMap(Field::getName,
119-
Function.identity()));
120-
119+
.collect(Collectors.toMap(Field::getName,
120+
Function.identity(), fieldMergeFunction()));
121+
121122
this.descriptors = map.values()
122123
.stream()
123124
.map(AttributePropertyDescriptor::new)
124125
.collect(Collectors.toMap(AttributePropertyDescriptor::getName,
125126
Function.identity()));
126-
127+
}
128+
129+
private BinaryOperator<Field> fieldMergeFunction() {
130+
return (field1, field2) -> {
131+
LOGGER.warn("Duplicated field " + field1.getName() + " found in " + field1.getDeclaringClass() + " and " + field2.getDeclaringClass());
132+
return field1;
133+
};
127134
}
128135

129136
public Collection<AttributePropertyDescriptor> getPropertyDescriptors() {

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/impl/IntrospectionUtilsTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.introproventures.graphql.jpa.query.schema.impl;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatCode;
45
import static org.mockito.Mockito.when;
56

67
import java.util.Optional;
@@ -208,6 +209,28 @@ public void testUppercasePropertyNamesAreSupported() throws Exception {
208209
.get())
209210
.extracting(AttributePropertyDescriptor::isIgnored)
210211
.isEqualTo(true);
211-
}
212-
212+
}
213+
214+
@Test
215+
public void shouldNotFailWhenPropertyIsDuplicatedInParentAndChild() {
216+
// given
217+
// There is a duplicated property in parent and child
218+
219+
// then
220+
assertThatCode(() -> IntrospectionUtils.introspect(CalculatedEntity.class)).doesNotThrowAnyException();
221+
}
222+
223+
@Test
224+
public void shouldCorrectlyIntrospectPropertyDuplicatedInParentAndChild() {
225+
// given
226+
// There is a duplicated property in parent and child
227+
228+
// when
229+
EntityIntrospectionResult introspectionResult = IntrospectionUtils.introspect(CalculatedEntity.class);
230+
231+
// then
232+
Optional<AttributePropertyDescriptor> propertyOverriddenInChild = introspectionResult.getPropertyDescriptor("propertyDuplicatedInChild");
233+
assertThat(propertyOverriddenInChild).isPresent();
234+
}
235+
213236
}

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/model/calculated/CalculatedEntity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public class CalculatedEntity extends ParentCalculatedEntity {
7575
String hideField = "hideField";
7676

7777
String propertyIgnoredOnGetter;
78+
79+
String propertyDuplicatedInChild;
7880

7981
@Transient
8082
@GraphQLDescription("i desc function")

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/model/calculated/ParentCalculatedEntity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public class ParentCalculatedEntity {
3535

3636
private String parentGraphQLIgnoreGetter;
3737

38-
private String parentTransientGraphQLIgnoreGetter;
39-
38+
private String parentTransientGraphQLIgnoreGetter;
39+
40+
private String propertyDuplicatedInChild;
41+
4042
@Transient // transient getter property
4143
@GraphQLDescription("getParentTransientGetter")
4244
public String getParentTransientGetter() {

0 commit comments

Comments
 (0)