use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.
the class HandlerMethodInputValidatorTests method shouldRaiseValidationErrorForAnnotatedParams.
@Test
void shouldRaiseValidationErrorForAnnotatedParams() throws Exception {
HandlerMethod method = findHandlerMethod(MyValidBean.class, "myValidMethod");
assertViolations(() -> validator.validate(method, new Object[] { null, 2 })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethod.arg0"));
assertViolations(() -> validator.validate(method, new Object[] { "test", 12 })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethod.arg1"));
}
use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.
the class HandlerMethodInputValidatorTests method shouldRaiseValidationErrorForAnnotatedParamsWithGroups.
@Test
void shouldRaiseValidationErrorForAnnotatedParamsWithGroups() throws Exception {
HandlerMethod myValidMethodWithGroup = findHandlerMethod(MyValidBeanWithGroup.class, "myValidMethodWithGroup");
assertViolations(() -> validator.validate(myValidMethodWithGroup, new Object[] { null })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethodWithGroup.arg0"));
HandlerMethod myValidMethodWithGroupOnType = findHandlerMethod(MyValidBeanWithGroup.class, "myValidMethodWithGroupOnType");
assertViolations(() -> validator.validate(myValidMethodWithGroupOnType, new Object[] { null })).anyMatch(violation -> violation.getPropertyPath().toString().equals("myValidMethodWithGroupOnType.arg0"));
}
use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.
the class AnnotatedControllerConfigurer method findHandlerMethods.
/**
* Scan beans in the ApplicationContext, detect and prepare a map of handler methods.
*/
private Collection<MappingInfo> findHandlerMethods() {
ApplicationContext context = obtainApplicationContext();
Map<FieldCoordinates, MappingInfo> result = new HashMap<>();
for (String beanName : context.getBeanNamesForType(Object.class)) {
if (beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
continue;
}
Class<?> beanType = null;
try {
beanType = context.getType(beanName);
} catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isTraceEnabled()) {
logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
}
}
if (beanType == null || !AnnotatedElementUtils.hasAnnotation(beanType, Controller.class)) {
continue;
}
Class<?> beanClass = context.getType(beanName);
findHandlerMethods(beanName, beanClass).forEach((info) -> {
HandlerMethod handlerMethod = info.getHandlerMethod();
MappingInfo existing = result.put(info.getCoordinates(), info);
if (existing != null && !existing.getHandlerMethod().equals(handlerMethod)) {
throw new IllegalStateException("Ambiguous mapping. Cannot map '" + handlerMethod.getBean() + "' method \n" + handlerMethod + "\nto " + info.getCoordinates() + ": There is already '" + existing.getHandlerMethod().getBean() + "' bean method\n" + existing + " mapped.");
}
});
}
return result.values();
}
use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.
the class AnnotatedControllerConfigurer method registerBatchLoader.
private String registerBatchLoader(MappingInfo info) {
if (!info.isBatchMapping()) {
throw new IllegalArgumentException("Not a @BatchMapping method: " + info);
}
String dataLoaderKey = info.getCoordinates().toString();
BatchLoaderRegistry registry = obtainApplicationContext().getBean(BatchLoaderRegistry.class);
HandlerMethod handlerMethod = info.getHandlerMethod();
BatchLoaderHandlerMethod invocable = new BatchLoaderHandlerMethod(handlerMethod);
Class<?> clazz = handlerMethod.getReturnType().getParameterType();
if (clazz.equals(Flux.class) || Collection.class.isAssignableFrom(clazz)) {
registry.forName(dataLoaderKey).registerBatchLoader(invocable::invokeForIterable);
} else if (clazz.equals(Mono.class) || clazz.equals(Map.class)) {
registry.forName(dataLoaderKey).registerMappedBatchLoader(invocable::invokeForMap);
} else {
throw new IllegalStateException("@BatchMapping method is expected to return " + "Flux<V>, List<V>, Mono<Map<K, V>>, or Map<K, V>: " + handlerMethod);
}
return dataLoaderKey;
}
use of org.springframework.graphql.data.method.HandlerMethod in project spring-graphql by spring-projects.
the class HandlerMethodInputValidatorTests method shouldIgnoreMethodsWithoutAnnotations.
@Test
void shouldIgnoreMethodsWithoutAnnotations() throws Exception {
HandlerMethod method = findHandlerMethod(MyValidBean.class, "notValidatedMethod");
assertThatNoException().isThrownBy(() -> validator.validate(method, new Object[] { "test", 12 }));
}
Aggregations