Skip to content

Commit 8762570

Browse files
committed
SchemaMappingInspector supports Boolean is<PropertyName>
Closes gh-1332
1 parent e655f6c commit 8762570

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public final class SchemaMappingInspector {
9494
(!method.getDeclaringClass().equals(Object.class) && !method.getReturnType().equals(Void.class) &&
9595
method.getParameterCount() == 0 && Modifier.isPublic(method.getModifiers()));
9696

97+
9798
private final GraphQLSchema schema;
9899

99100
private final Map<String, Map<String, DataFetcher>> dataFetchers;
@@ -191,8 +192,8 @@ private void checkFieldsContainer(
191192
checkField(fieldContainer, field, ResolvableType.forField(javaField));
192193
continue;
193194
}
194-
// Kotlin function?
195-
Method method = getRecordLikeMethod(resolvableType, fieldName);
195+
// Kotlin function, Boolean is<PropertyName>
196+
Method method = getOtherAccessor(resolvableType, fieldName);
196197
if (method != null) {
197198
MethodParameter returnType = new MethodParameter(method, -1);
198199
checkField(fieldContainer, field, ResolvableType.forMethodParameter(returnType, resolvableType));
@@ -286,13 +287,18 @@ private Field getField(ResolvableType resolvableType, String fieldName) {
286287
}
287288

288289
@Nullable
289-
private static Method getRecordLikeMethod(ResolvableType resolvableType, String fieldName) {
290+
private static Method getOtherAccessor(ResolvableType resolvableType, String fieldName) {
290291
Class<?> clazz = resolvableType.resolve();
291292
if (clazz != null) {
292293
for (Method method : clazz.getDeclaredMethods()) {
293294
if (recordLikePredicate.test(method) && fieldName.equals(StringUtils.uncapitalize(method.getName()))) {
294295
return method;
295296
}
297+
// JavaBean introspection supports boolean property only
298+
if (method.getReturnType().equals(Boolean.class) &&
299+
method.getName().equals("is" + StringUtils.capitalize(fieldName))) {
300+
return method;
301+
}
296302
}
297303
}
298304
return null;

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,20 @@ void reportIsEmptyWhenFieldHasBatchMapping() {
380380
assertThatReport(report).hasUnmappedFieldCount(0).hasSkippedTypeCount(0);
381381
}
382382

383+
@Test // gh-1332
384+
void reportIsEmptyWithBooleanProperty() {
385+
String schema = """
386+
type Query {
387+
user: User!
388+
}
389+
type User {
390+
allowed: Boolean!
391+
}
392+
""";
393+
SchemaReport report = inspectSchema(schema, UserController.class);
394+
assertThatReport(report).hasUnmappedFieldCount(0).hasSkippedTypeCount(0);
395+
}
396+
383397
@Test
384398
void reportHasUnmappedField() {
385399
String schema = """
@@ -736,4 +750,29 @@ record ListContainer<T>(List<T> items) {
736750

737751
}
738752

753+
754+
@Controller
755+
static class UserController {
756+
757+
@QueryMapping
758+
User user() {
759+
return new User(true);
760+
}
761+
762+
}
763+
764+
765+
static class User {
766+
767+
private final Boolean allowed;
768+
769+
User(Boolean allowed) {
770+
this.allowed = allowed;
771+
}
772+
773+
public Boolean isAllowed() {
774+
return allowed;
775+
}
776+
}
777+
739778
}

0 commit comments

Comments
 (0)