Skip to content

Commit 32baac9

Browse files
committed
Fix ConversionService in auto-config
- Now using qualifier for one we expect which should work if some other services are in a context. - Properly use shellConversionService in defined MethodArgumentResolver beans. - Fixes #400
1 parent 1a1864f commit 32baac9

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import org.springframework.beans.factory.annotation.Qualifier;
67
import org.springframework.context.annotation.Bean;
78
import org.springframework.context.annotation.Configuration;
8-
import org.springframework.core.convert.support.DefaultConversionService;
9+
import org.springframework.core.convert.ConversionService;
910
import org.springframework.messaging.handler.annotation.support.HeadersMethodArgumentResolver;
1011
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
1112
import org.springframework.shell.command.ArgumentHeaderMethodArgumentResolver;
@@ -24,12 +25,13 @@ public CompletionResolver defaultCompletionResolver() {
2425
}
2526

2627
@Bean
27-
public CommandExecutionHandlerMethodArgumentResolvers commandExecutionHandlerMethodArgumentResolvers() {
28+
public CommandExecutionHandlerMethodArgumentResolvers commandExecutionHandlerMethodArgumentResolvers(
29+
@Qualifier("shellConversionService") ConversionService shellConversionService) {
2830
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
29-
resolvers.add(new ArgumentHeaderMethodArgumentResolver(new DefaultConversionService(), null));
31+
resolvers.add(new ArgumentHeaderMethodArgumentResolver(shellConversionService, null));
3032
resolvers.add(new HeadersMethodArgumentResolver());
3133
resolvers.add(new CommandContextMethodArgumentResolver());
32-
resolvers.add(new ShellOptionMethodArgumentResolver(new DefaultConversionService(), null));
34+
resolvers.add(new ShellOptionMethodArgumentResolver(shellConversionService, null));
3335
return new CommandExecutionHandlerMethodArgumentResolvers(resolvers);
3436
}
3537
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.boot;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.boot.autoconfigure.AutoConfigurations;
21+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.core.convert.ConversionService;
25+
import org.springframework.core.convert.support.DefaultConversionService;
26+
import org.springframework.shell.command.CommandExecution.CommandExecutionHandlerMethodArgumentResolvers;
27+
import org.springframework.shell.completion.CompletionResolver;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
public class ParameterResolverAutoConfigurationTests {
32+
33+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
34+
.withConfiguration(AutoConfigurations.of(ParameterResolverAutoConfiguration.class));
35+
36+
@Test
37+
void defaultCompletionResolverExists() {
38+
this.contextRunner.withUserConfiguration(CustomShellConversionServiceConfiguration.class)
39+
.run((context) -> {
40+
assertThat(context).hasSingleBean(CompletionResolver.class);
41+
});
42+
}
43+
44+
@Test
45+
void defaultCommandExecutionHandlerMethodArgumentResolversExists() {
46+
this.contextRunner.withUserConfiguration(CustomShellConversionServiceConfiguration.class)
47+
.run((context) -> {
48+
assertThat(context).hasSingleBean(CommandExecutionHandlerMethodArgumentResolvers.class);
49+
});
50+
}
51+
52+
@Test
53+
void multipleConversionServiceBeans() {
54+
this.contextRunner
55+
.withUserConfiguration(CustomShellConversionServiceConfiguration.class,
56+
ExtraConversionServiceConfiguration.class)
57+
.run((context) -> {
58+
assertThat(context).hasSingleBean(CommandExecutionHandlerMethodArgumentResolvers.class);
59+
});
60+
}
61+
62+
@Configuration
63+
static class CustomShellConversionServiceConfiguration {
64+
65+
@Bean
66+
ConversionService shellConversionService() {
67+
return new DefaultConversionService();
68+
}
69+
}
70+
71+
@Configuration
72+
static class ExtraConversionServiceConfiguration {
73+
74+
@Bean
75+
ConversionService conversionService() {
76+
return new DefaultConversionService();
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)