Improve check for duplicate unmapped field
Closes gh-961
This commit is contained in:
@@ -145,6 +145,10 @@ public final class SchemaMappingInspector {
|
||||
private void checkFieldsContainer(
|
||||
GraphQLFieldsContainer fieldContainer, @Nullable ResolvableType resolvableType) {
|
||||
|
||||
if (!this.inspectedTypes.add(fieldContainer.getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String typeName = fieldContainer.getName();
|
||||
Map<String, DataFetcher> dataFetcherMap = this.dataFetchers.getOrDefault(typeName, Collections.emptyMap());
|
||||
|
||||
@@ -196,10 +200,6 @@ public final class SchemaMappingInspector {
|
||||
|
||||
TypePair typePair = TypePair.resolveTypePair(parent, field, resolvableType, this.schema);
|
||||
|
||||
if (addAndCheckIfAlreadyInspected(typePair.outputType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
MultiValueMap<GraphQLType, ResolvableType> typePairs = new LinkedMultiValueMap<>();
|
||||
if (typePair.outputType() instanceof GraphQLUnionType unionType) {
|
||||
typePairs.putAll(this.interfaceUnionLookup.resolveUnion(unionType));
|
||||
@@ -250,10 +250,6 @@ public final class SchemaMappingInspector {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean addAndCheckIfAlreadyInspected(GraphQLType type) {
|
||||
return (type instanceof GraphQLNamedOutputType outputType && !this.inspectedTypes.add(outputType.getName()));
|
||||
}
|
||||
|
||||
private static boolean isNotScalarOrEnumType(GraphQLType type) {
|
||||
return !(type instanceof GraphQLScalarType || type instanceof GraphQLEnumType);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.graphql.execution;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -53,7 +54,7 @@ public class SchemaMappingInspectorInterfaceTests extends SchemaMappingInspector
|
||||
|
||||
|
||||
@Nested
|
||||
class InterfaceFieldsNotOnJavaInterface {
|
||||
class UnmappedFields {
|
||||
|
||||
@Test
|
||||
void reportUnmappedFields() {
|
||||
@@ -83,10 +84,10 @@ public class SchemaMappingInspectorInterfaceTests extends SchemaMappingInspector
|
||||
|
||||
|
||||
@Nested
|
||||
class GraphQlAndJavaTypeNameMismatch {
|
||||
class ClassNameAndClassResolver {
|
||||
|
||||
@Test
|
||||
void useClassNameFunction() {
|
||||
void classNameFunction() {
|
||||
|
||||
SchemaReport report = inspectSchema(schema,
|
||||
initializer -> initializer.classNameFunction(type -> type.getName() + "Impl"),
|
||||
@@ -100,13 +101,12 @@ public class SchemaMappingInspectorInterfaceTests extends SchemaMappingInspector
|
||||
}
|
||||
|
||||
@Test
|
||||
void useClassNameTypeResolver() {
|
||||
void classNameTypeResolver() {
|
||||
|
||||
ClassNameTypeResolver typeResolver = new ClassNameTypeResolver();
|
||||
typeResolver.addMapping(CarImpl.class, "Car");
|
||||
Map<Class<?>, String> mappings = Map.of(CarImpl.class, "Car");
|
||||
|
||||
SchemaReport report = inspectSchema(schema,
|
||||
initializer -> initializer.classResolver(ClassResolver.create(typeResolver.getMappings())),
|
||||
initializer -> initializer.classResolver(ClassResolver.create(mappings)),
|
||||
VehicleController.class);
|
||||
|
||||
assertThatReport(report)
|
||||
@@ -131,6 +131,47 @@ public class SchemaMappingInspectorInterfaceTests extends SchemaMappingInspector
|
||||
}
|
||||
|
||||
|
||||
@Nested // gh-961
|
||||
class UnmappedFieldsReportedOnlyOnce {
|
||||
|
||||
@Test
|
||||
void reportUnmappedFields() {
|
||||
|
||||
String schema = SchemaMappingInspectorInterfaceTests.schema + """
|
||||
extend type Query {
|
||||
cars: [Car]
|
||||
}
|
||||
""";
|
||||
|
||||
SchemaReport report = inspectSchema(schema, VehicleController.class);
|
||||
assertThatReport(report)
|
||||
.hasSkippedTypeCount(0)
|
||||
.hasUnmappedFieldCount(2)
|
||||
.containsUnmappedFields("Car", "price", "engineType");
|
||||
}
|
||||
|
||||
interface Vehicle {
|
||||
String name();
|
||||
}
|
||||
record Car(String name) implements Vehicle { }
|
||||
record Bike(String name, int price) implements Vehicle { }
|
||||
|
||||
@Controller
|
||||
static class VehicleController {
|
||||
|
||||
@QueryMapping
|
||||
List<Vehicle> vehicles() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
List<Car> cars() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class SkippedTypes {
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.graphql.execution;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -48,7 +49,7 @@ public class SchemaMappingInspectorUnionTests extends SchemaMappingInspectorTest
|
||||
|
||||
|
||||
@Nested
|
||||
class InterfaceFieldsNotOnJavaInterface {
|
||||
class UnmappedFields {
|
||||
|
||||
@Test
|
||||
void reportUnmappedFieldsByCheckingReturnTypePackage() {
|
||||
@@ -97,10 +98,10 @@ public class SchemaMappingInspectorUnionTests extends SchemaMappingInspectorTest
|
||||
|
||||
|
||||
@Nested
|
||||
class GraphQlAndJavaTypeNameMismatch {
|
||||
class ClassNameAndClassResolver {
|
||||
|
||||
@Test
|
||||
void useClassNameFunction() {
|
||||
void classNameFunction() {
|
||||
|
||||
SchemaReport report = inspectSchema(schema,
|
||||
initializer -> initializer.classNameFunction(type -> type.getName() + "Impl"),
|
||||
@@ -114,13 +115,12 @@ public class SchemaMappingInspectorUnionTests extends SchemaMappingInspectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void useClassNameTypeResolver() {
|
||||
void classNameTypeResolver() {
|
||||
|
||||
ClassNameTypeResolver typeResolver = new ClassNameTypeResolver();
|
||||
typeResolver.addMapping(PhotoImpl.class, "Photo");
|
||||
Map<Class<?>, String> mappings = Map.of(PhotoImpl.class, "Photo");
|
||||
|
||||
SchemaReport report = inspectSchema(schema,
|
||||
initializer -> initializer.classResolver(ClassResolver.create(typeResolver.getMappings())),
|
||||
initializer -> initializer.classResolver(ClassResolver.create(mappings)),
|
||||
SearchController.class);
|
||||
|
||||
assertThatReport(report)
|
||||
|
||||
Reference in New Issue
Block a user