Callbacks for ConversionService customization

Closes gh-271
This commit is contained in:
rstoyanchev
2022-02-03 10:16:31 +00:00
parent d2cad053ac
commit 0b449d89e1
2 changed files with 25 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2022 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.
@@ -166,6 +166,7 @@ public class GraphQlArgumentInitializer {
MutablePropertyValues propertyValues = extractPropertyValues(arguments);
target = BeanUtils.instantiateClass(ctor);
DataBinder dataBinder = new DataBinder(target);
dataBinder.setConversionService(this.typeConverter.getConversionService());
dataBinder.bind(propertyValues);
return target;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -48,6 +48,9 @@ import org.springframework.core.MethodIntrospector;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.FormatterRegistrar;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.graphql.data.GraphQlArgumentInitializer;
import org.springframework.graphql.data.method.HandlerMethod;
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
@@ -109,20 +112,33 @@ public class AnnotatedControllerConfigurer
@Nullable
private HandlerMethodInputValidator validator;
@Nullable
private ConversionService conversionService;
private FormattingConversionService conversionService = new DefaultFormattingConversionService();
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
/**
* Add a {@code FormatterRegistrar} to customize the {@link ConversionService}
* that assists in binding GraphQL arguments onto
* {@link org.springframework.graphql.data.method.annotation.Argument @Argument}
* annotated method parameters.
*/
public void addFormatterRegistrar(FormatterRegistrar registrar) {
registrar.registerFormatters(this.conversionService);
}
/**
* Configure the {@link ConversionService} used for binding handler arguments.
* @deprecated in favor of using {@link #addFormatterRegistrar(FormatterRegistrar)}
* to customize the built-in ConversionService instance.
*/
@Deprecated
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
Assert.isInstanceOf(FormattingConversionService.class, "FormattingConversionService is required");
this.conversionService = (FormattingConversionService) conversionService;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
protected final ApplicationContext obtainApplicationContext() {