Fixing NPE in AbstractNamedValueMethodArgumentResolver

See gh-23882
This commit is contained in:
Dekel Pilli
2019-10-29 12:10:48 +11:00
committed by Rossen Stoyanchev
parent fd9678833f
commit 64f2beb9bf
4 changed files with 13 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.ValueConstants; import org.springframework.messaging.handler.annotation.ValueConstants;
@@ -72,10 +73,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
* @param beanFactory a bean factory for resolving {@code ${...}} * @param beanFactory a bean factory for resolving {@code ${...}}
* placeholders and {@code #{...}} SpEL expressions in default values * placeholders and {@code #{...}} SpEL expressions in default values
*/ */
protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionService, protected AbstractNamedValueMethodArgumentResolver(@Nullable ConversionService conversionService,
@Nullable ConfigurableBeanFactory beanFactory) { @Nullable ConfigurableBeanFactory beanFactory) {
this.conversionService = conversionService; this.conversionService = conversionService != null ? conversionService : DefaultConversionService.getSharedInstance();
this.configurableBeanFactory = beanFactory; this.configurableBeanFactory = beanFactory;
this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null); this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
} }

View File

@@ -41,7 +41,7 @@ public class DestinationVariableMethodArgumentResolver extends AbstractNamedValu
DestinationVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables"; DestinationVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables";
public DestinationVariableMethodArgumentResolver(ConversionService conversionService) { public DestinationVariableMethodArgumentResolver(@Nullable ConversionService conversionService) {
super(conversionService, null); super(conversionService, null);
} }

View File

@@ -49,7 +49,7 @@ public class HeaderMethodArgumentResolver extends AbstractNamedValueMethodArgume
public HeaderMethodArgumentResolver( public HeaderMethodArgumentResolver(
ConversionService conversionService, @Nullable ConfigurableBeanFactory beanFactory) { @Nullable ConversionService conversionService, @Nullable ConfigurableBeanFactory beanFactory) {
super(conversionService, beanFactory); super(conversionService, beanFactory);
} }

View File

@@ -145,6 +145,14 @@ public class HeaderMethodArgumentResolverTests {
assertThat(result).isEqualTo(Optional.of("bar")); assertThat(result).isEqualTo(Optional.of("bar"));
} }
@Test
public void resolveOptionalHeaderWithValueFromNullConversionServiceInput() throws Exception {
GenericApplicationContext context = new GenericApplicationContext();
context.refresh();
resolver = new HeaderMethodArgumentResolver(null, context.getBeanFactory());
resolveOptionalHeaderWithValue();
}
@Test @Test
public void resolveOptionalHeaderAsEmpty() throws Exception { public void resolveOptionalHeaderAsEmpty() throws Exception {
Message<String> message = MessageBuilder.withPayload("foo").build(); Message<String> message = MessageBuilder.withPayload("foo").build();