Skip to content
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
77 changes: 50 additions & 27 deletions src/main/java/graphql/util/Anonymizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import static graphql.Assert.assertNotNull;
Expand Down Expand Up @@ -314,7 +316,7 @@ public TraversalControl visitGraphQLArgument(GraphQLArgument graphQLArgument, Tr
GraphQLFieldDefinition fieldDefinition = (GraphQLFieldDefinition) parentNode;
String fieldName = fieldDefinition.getName();
GraphQLImplementingType implementingType = (GraphQLImplementingType) context.getParentContext().getParentNode();
List<GraphQLFieldDefinition> matchingInterfaceFieldDefinitions = getMatchingInterfaceFieldDefinitions(fieldName, implementingType.getInterfaces());
Set<GraphQLFieldDefinition> matchingInterfaceFieldDefinitions = getSameFields(fieldName, implementingType.getName(), interfaceToImplementations, schema);
String newName;
if (matchingInterfaceFieldDefinitions.size() == 0) {
newName = "argument" + argumentCounter.getAndIncrement();
Expand Down Expand Up @@ -367,17 +369,15 @@ public TraversalControl visitGraphQLEnumValueDefinition(GraphQLEnumValueDefiniti

@Override
public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition graphQLFieldDefinition, TraverserContext<GraphQLSchemaElement> context) {
String curName = graphQLFieldDefinition.getName();
String fieldName = graphQLFieldDefinition.getName();
GraphQLImplementingType parentNode = (GraphQLImplementingType) context.getParentNode();
List<GraphQLNamedOutputType> interfaces = parentNode.getInterfaces();
List<GraphQLFieldDefinition> sameFields = getMatchingInterfaceFieldDefinitions(curName, interfaces);
sameFields.addAll(getMatchingImplementationsFieldDefinitions(parentNode.getName(), curName, interfaceToImplementations));
Set<GraphQLFieldDefinition> sameFields = getSameFields(fieldName, parentNode.getName(), interfaceToImplementations, schema);
String newName;
if (sameFields.size() == 0) {
newName = "field" + fieldCounter.getAndIncrement();
} else {
if (newNameMap.containsKey(sameFields.get(0))) {
newName = newNameMap.get(sameFields.get(0));
if (newNameMap.containsKey(sameFields.iterator().next())) {
newName = newNameMap.get(sameFields.iterator().next());
} else {
newName = "field" + fieldCounter.getAndIncrement();
for (GraphQLFieldDefinition fieldDefinition : sameFields) {
Expand Down Expand Up @@ -453,39 +453,62 @@ public TraversalControl visitGraphQLUnionType(GraphQLUnionType graphQLUnionType,
return newNameMap;
}

private static List<GraphQLFieldDefinition> getMatchingInterfaceFieldDefinitions(
String curName,
List<GraphQLNamedOutputType> interfaces) {
List<GraphQLFieldDefinition> matchingInterfaceFieldDefinitions = new ArrayList<>();
for (GraphQLNamedOutputType iface : interfaces) {
GraphQLInterfaceType interfaceType = (GraphQLInterfaceType) iface;
if (interfaceType.getFieldDefinition(curName) != null) {
matchingInterfaceFieldDefinitions.add(interfaceType.getFieldDefinition(curName));
}
}
return matchingInterfaceFieldDefinitions;
private static Set<GraphQLFieldDefinition> getSameFields(String fieldName,
String objectOrInterfaceName,
Map<String, List<GraphQLImplementingType>> interfaceToImplementations,
GraphQLSchema schema
) {
Set<GraphQLFieldDefinition> result = new LinkedHashSet<>();
Set<String> alreadyChecked = new LinkedHashSet<>();
getSameFieldsImpl(fieldName, objectOrInterfaceName, interfaceToImplementations, schema, alreadyChecked, result);
return result;
}

private static List<GraphQLFieldDefinition> getMatchingImplementationsFieldDefinitions(
String interfaceName,
String fieldName,
Map<String, List<GraphQLImplementingType>> interfaceToImplementations) {
List<GraphQLFieldDefinition> result = new ArrayList<>();
List<GraphQLImplementingType> implementations = interfaceToImplementations.get(interfaceName);
private static void getSameFieldsImpl(String fieldName,
String curObjectOrInterface,
Map<String, List<GraphQLImplementingType>> interfaceToImplementations,
GraphQLSchema schema,
Set<String> alreadyChecked,
Set<GraphQLFieldDefinition> result) {
if (alreadyChecked.contains(curObjectOrInterface)) {
return;
}
alreadyChecked.add(curObjectOrInterface);

// "up": get all Interfaces
GraphQLImplementingType type = (GraphQLImplementingType) schema.getType(curObjectOrInterface);
List<GraphQLNamedOutputType> interfaces = type.getInterfaces();
getMatchingFieldDefinitions(fieldName, interfaces, result);
for (GraphQLNamedOutputType interfaze : interfaces) {
getSameFieldsImpl(fieldName, interfaze.getName(), interfaceToImplementations, schema, alreadyChecked, result);
}

// "down": get all Object or Interfaces
List<GraphQLImplementingType> implementations = interfaceToImplementations.get(curObjectOrInterface);
if (implementations == null) {
return Collections.emptyList();
return;
}
getMatchingFieldDefinitions(fieldName, implementations, result);
for (GraphQLImplementingType implementingType : implementations) {
getSameFieldsImpl(fieldName, implementingType.getName(), interfaceToImplementations, schema, alreadyChecked, result);
}
}

private static void getMatchingFieldDefinitions(
String fieldName,
List<? extends GraphQLType> interfaces,
Set<GraphQLFieldDefinition> result) {
for (GraphQLType iface : interfaces) {
GraphQLImplementingType implementingType = (GraphQLImplementingType) iface;
if (implementingType.getFieldDefinition(fieldName) != null) {
result.add(implementingType.getFieldDefinition(fieldName));
}
}
return result;
}

private static List<GraphQLArgument> getMatchingArgumentDefinitions(
String name,
List<GraphQLFieldDefinition> fieldDefinitions) {
Set<GraphQLFieldDefinition> fieldDefinitions) {
List<GraphQLArgument> result = new ArrayList<>();
for (GraphQLFieldDefinition fieldDefinition : fieldDefinitions) {
Optional.ofNullable(fieldDefinition.getArgument(name)).map(result::add);
Expand Down
42 changes: 32 additions & 10 deletions src/test/groovy/graphql/util/AnonymizerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -545,23 +545,33 @@ type Object1 {
def "same field across hierarchy"() {
def schema = TestUtil.schema("""
type Query {
foo: Object
foo: Interface2
}
interface Interface1 implements Interface2 & Interface3 {
id: ID!
}

interface Interface4 implements Interface1 & Interface2 & Interface3 {
id: ID!
}
interface Interface5 implements Interface1 & Interface2 & Interface3 & Interface6{
id: ID!
}
interface Interface2 {
id: ID!
}

interface Interface3 {
id: ID!
}
type Object implements Interface1 & Interface2 & Interface3 {
interface Interface6 {
id: ID!
}

interface Interface7 implements Interface6 {
id: ID!
}


""")
when:
def result = Anonymizer.anonymizeSchema(schema)
Expand All @@ -573,23 +583,35 @@ type Object implements Interface1 & Interface2 & Interface3 {
}

interface Interface1 implements Interface2 & Interface3 {
field2: ID!
field1: ID!
}

interface Interface2 {
field2: ID!
field1: ID!
}

interface Interface3 {
field2: ID!
field1: ID!
}

type Object1 {
field1: Object2
interface Interface4 implements Interface1 & Interface2 & Interface3 {
field1: ID!
}

type Object2 implements Interface1 & Interface2 & Interface3 {
field2: ID!
interface Interface5 implements Interface1 & Interface2 & Interface3 & Interface6 {
field1: ID!
}

interface Interface6 {
field1: ID!
}

interface Interface7 implements Interface6 {
field1: ID!
}

type Object1 {
field2: Interface2
}
"""
}
Expand Down