Skip to content

Commit 310424e

Browse files
committed
SchemaMappingInspector checks public fields
Closes gh-1101
1 parent 4a5aaf1 commit 310424e

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/SchemaMappingInspector.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.graphql.execution;
1818

1919
import java.beans.PropertyDescriptor;
20+
import java.lang.reflect.Field;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Modifier;
2223
import java.util.ArrayList;
@@ -185,6 +186,11 @@ private void checkFieldsContainer(
185186
checkField(fieldContainer, field, ResolvableType.forMethodParameter(returnType, resolvableType));
186187
continue;
187188
}
189+
Field javaField = getField(resolvableType, fieldName);
190+
if (javaField != null) {
191+
checkField(fieldContainer, field, ResolvableType.forField(javaField));
192+
continue;
193+
}
188194
// Kotlin function?
189195
Method method = getRecordLikeMethod(resolvableType, fieldName);
190196
if (method != null) {
@@ -268,6 +274,17 @@ private PropertyDescriptor getProperty(ResolvableType resolvableType, String fie
268274
}
269275
}
270276

277+
@Nullable
278+
private Field getField(ResolvableType resolvableType, String fieldName) {
279+
try {
280+
Class<?> clazz = resolvableType.resolve();
281+
return (clazz != null) ? clazz.getField(fieldName) : null;
282+
}
283+
catch (NoSuchFieldException ex) {
284+
return null;
285+
}
286+
}
287+
271288
@Nullable
272289
private static Method getRecordLikeMethod(ResolvableType resolvableType, String fieldName) {
273290
Class<?> clazz = resolvableType.resolve();

spring-graphql/src/test/java/org/springframework/graphql/Book.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class Book {
2626

2727
Author author;
2828

29+
public String publicField;
30+
2931
public Book() {
3032
}
3133

spring-graphql/src/test/java/org/springframework/graphql/execution/SchemaMappingInspectorTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@ void reportIsEmptyWhenFieldHasMatchingObjectProperty() {
327327
assertThatReport(report).hasUnmappedFieldCount(0).hasSkippedTypeCount(0);
328328
}
329329

330+
@Test
331+
void reportIsEmptyWhenFieldHasMatchingObjectField() {
332+
String schema = """
333+
type Query {
334+
bookById(id: ID): Book
335+
}
336+
type Book {
337+
id: ID
338+
name: String
339+
publicField: String
340+
}
341+
""";
342+
SchemaReport report = inspectSchema(schema, BookController.class);
343+
assertThatReport(report).hasUnmappedFieldCount(0).hasSkippedTypeCount(0);
344+
}
345+
330346
@Test
331347
void reportIsEmptyWhenFieldHasDataFetcherMapping() {
332348
String schema = """

0 commit comments

Comments
 (0)