SchemaMappingInspector checks public fields

Closes gh-1101
This commit is contained in:
rstoyanchev
2025-02-18 16:52:58 +00:00
parent 4a5aaf182d
commit 310424e793
3 changed files with 36 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2024 the original author or authors.
* Copyright 2020-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.graphql.execution;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -185,6 +186,11 @@ public final class SchemaMappingInspector {
checkField(fieldContainer, field, ResolvableType.forMethodParameter(returnType, resolvableType));
continue;
}
Field javaField = getField(resolvableType, fieldName);
if (javaField != null) {
checkField(fieldContainer, field, ResolvableType.forField(javaField));
continue;
}
// Kotlin function?
Method method = getRecordLikeMethod(resolvableType, fieldName);
if (method != null) {
@@ -268,6 +274,17 @@ public final class SchemaMappingInspector {
}
}
@Nullable
private Field getField(ResolvableType resolvableType, String fieldName) {
try {
Class<?> clazz = resolvableType.resolve();
return (clazz != null) ? clazz.getField(fieldName) : null;
}
catch (NoSuchFieldException ex) {
return null;
}
}
@Nullable
private static Method getRecordLikeMethod(ResolvableType resolvableType, String fieldName) {
Class<?> clazz = resolvableType.resolve();

View File

@@ -26,6 +26,8 @@ public class Book {
Author author;
public String publicField;
public Book() {
}

View File

@@ -327,6 +327,22 @@ class SchemaMappingInspectorTests extends SchemaMappingInspectorTestSupport {
assertThatReport(report).hasUnmappedFieldCount(0).hasSkippedTypeCount(0);
}
@Test
void reportIsEmptyWhenFieldHasMatchingObjectField() {
String schema = """
type Query {
bookById(id: ID): Book
}
type Book {
id: ID
name: String
publicField: String
}
""";
SchemaReport report = inspectSchema(schema, BookController.class);
assertThatReport(report).hasUnmappedFieldCount(0).hasSkippedTypeCount(0);
}
@Test
void reportIsEmptyWhenFieldHasDataFetcherMapping() {
String schema = """