Skip to content

Add duplicated property test case #178

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

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 @@ -17,6 +17,7 @@
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -33,7 +34,7 @@
import com.introproventures.graphql.jpa.query.schema.impl.IntrospectionUtils.EntityIntrospectionResult.AttributePropertyDescriptor;

public class IntrospectionUtils {
private static final Logger log = LoggerFactory.getLogger(IntrospectionUtils.class);
private static final Logger LOGGER = LoggerFactory.getLogger(IntrospectionUtils.class);

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

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

this.fields = getClasses().flatMap(k -> Arrays.stream(k.getDeclaredFields()))
.filter(f -> map.containsKey(Introspector.decapitalize(f.getName())))
.collect(Collectors.toMap(Field::getName,
Function.identity()));
.collect(Collectors.toMap(Field::getName,
Function.identity(), fieldMergeFunction()));

this.descriptors = map.values()
.stream()
.map(AttributePropertyDescriptor::new)
.collect(Collectors.toMap(AttributePropertyDescriptor::getName,
Function.identity()));

}

private BinaryOperator<Field> fieldMergeFunction() {
return (field1, field2) -> {
LOGGER.warn("Duplicated field " + field1.getName() + " found in " + field1.getDeclaringClass() + " and " + field2.getDeclaringClass());
return field1;
};
}

public Collection<AttributePropertyDescriptor> getPropertyDescriptors() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.introproventures.graphql.jpa.query.schema.impl;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.when;

import java.util.Optional;
Expand Down Expand Up @@ -208,6 +209,28 @@ public void testUppercasePropertyNamesAreSupported() throws Exception {
.get())
.extracting(AttributePropertyDescriptor::isIgnored)
.isEqualTo(true);
}

}

@Test
public void shouldNotFailWhenPropertyIsDuplicatedInParentAndChild() {
// given
// There is a duplicated property in parent and child

// then
assertThatCode(() -> IntrospectionUtils.introspect(CalculatedEntity.class)).doesNotThrowAnyException();
}

@Test
public void shouldCorrectlyIntrospectPropertyDuplicatedInParentAndChild() {
// given
// There is a duplicated property in parent and child

// when
EntityIntrospectionResult introspectionResult = IntrospectionUtils.introspect(CalculatedEntity.class);

// then
Optional<AttributePropertyDescriptor> propertyOverriddenInChild = introspectionResult.getPropertyDescriptor("propertyDuplicatedInChild");
assertThat(propertyOverriddenInChild).isPresent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class CalculatedEntity extends ParentCalculatedEntity {
String hideField = "hideField";

String propertyIgnoredOnGetter;

String propertyDuplicatedInChild;

@Transient
@GraphQLDescription("i desc function")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public class ParentCalculatedEntity {

private String parentGraphQLIgnoreGetter;

private String parentTransientGraphQLIgnoreGetter;

private String parentTransientGraphQLIgnoreGetter;

private String propertyDuplicatedInChild;

@Transient // transient getter property
@GraphQLDescription("getParentTransientGetter")
public String getParentTransientGetter() {
Expand Down