From 7042d1f83beee33aef42dc98ce96299a99af2ed3 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Fri, 24 Dec 2021 15:04:40 +0000 Subject: [PATCH] Rework shellConversionService - Replace old ConversionService impl with something more aligned with current boot code with partially shameless copy as shell is most likely working outside of web or webflux env by creating FormattingConversionService and hooking convertes from DefaultConversionService. - This should work on most cases and giving user a hook to define their own service. --- .../ParameterResolverAutoConfiguration.java | 3 +- .../boot/SpringShellAutoConfiguration.java | 30 ++++++------------- .../samples/standard/ConversionExample.java | 5 ++++ 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java index c25e6b95..d394e918 100644 --- a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java +++ b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java @@ -4,7 +4,6 @@ import java.util.Set; import java.util.stream.Collectors; import org.springframework.beans.factory.ObjectProvider; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.ConversionService; @@ -16,7 +15,7 @@ import org.springframework.shell.standard.ValueProvider; public class ParameterResolverAutoConfiguration { @Bean - public ParameterResolver standardParameterResolver(@Qualifier("spring-shell") ConversionService conversionService, + public ParameterResolver standardParameterResolver(ConversionService conversionService, ObjectProvider valueProviders) { Set collect = valueProviders.orderedStream().collect(Collectors.toSet()); return new StandardParameterResolver(conversionService, collect); diff --git a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java index 8ed73f37..ea53129e 100644 --- a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java +++ b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java @@ -16,19 +16,17 @@ package org.springframework.shell.boot; -import java.util.Collection; import java.util.Set; -import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.convert.ApplicationConversionService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.convert.ConversionService; -import org.springframework.core.convert.converter.Converter; -import org.springframework.core.convert.converter.ConverterFactory; -import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.format.support.FormattingConversionService; import org.springframework.shell.CommandRegistry; import org.springframework.shell.ResultHandler; import org.springframework.shell.ResultHandlerService; @@ -44,23 +42,13 @@ import org.springframework.shell.result.ResultHandlerConfig; public class SpringShellAutoConfiguration { @Bean - @Qualifier("spring-shell") + @ConditionalOnMissingBean(ConversionService.class) public ConversionService shellConversionService(ApplicationContext applicationContext) { - Collection converters = applicationContext.getBeansOfType(Converter.class).values(); - Collection genericConverters = applicationContext.getBeansOfType(GenericConverter.class).values(); - Collection converterFactories = applicationContext.getBeansOfType(ConverterFactory.class).values(); - - DefaultConversionService defaultConversionService = new DefaultConversionService(); - for (Converter converter : converters) { - defaultConversionService.addConverter(converter); - } - for (GenericConverter genericConverter : genericConverters) { - defaultConversionService.addConverter(genericConverter); - } - for (ConverterFactory converterFactory : converterFactories) { - defaultConversionService.addConverterFactory(converterFactory); - } - return defaultConversionService; + FormattingConversionService service = new FormattingConversionService(); + DefaultConversionService.addDefaultConverters(service); + DefaultConversionService.addCollectionConverters(service); + ApplicationConversionService.addBeans(service, applicationContext); + return service; } @Bean diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/ConversionExample.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/ConversionExample.java index 86dbe2c5..e8d8642d 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/ConversionExample.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/ConversionExample.java @@ -41,6 +41,11 @@ class DomainObject { public String getValue() { return value; } + + @Override + public String toString() { + return "DomainObject [value=" + value + "]"; + } } @Component