Skip to content

Commit d163240

Browse files
committed
Reactive support for Errors argument
Issue: SPR-14542
1 parent 816e328 commit d163240

File tree

3 files changed

+284
-3
lines changed

3 files changed

+284
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2002-2016 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+
* http://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.web.reactive.result.method.annotation;
17+
18+
import reactor.core.publisher.Mono;
19+
20+
import org.springframework.core.MethodParameter;
21+
import org.springframework.core.ReactiveAdapter;
22+
import org.springframework.core.ReactiveAdapterRegistry;
23+
import org.springframework.core.ResolvableType;
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.ClassUtils;
26+
import org.springframework.util.StringUtils;
27+
import org.springframework.validation.BindingResult;
28+
import org.springframework.validation.Errors;
29+
import org.springframework.web.bind.annotation.ModelAttribute;
30+
import org.springframework.web.reactive.result.method.BindingContext;
31+
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
32+
import org.springframework.web.server.ServerWebExchange;
33+
34+
/**
35+
* Resolve {@link Errors} or {@link BindingResult} method arguments.
36+
* An {@code Errors} argument is expected to appear immediately after the
37+
* model attribute in the method signature.
38+
*
39+
* @author Rossen Stoyanchev
40+
* @since 5.0
41+
*/
42+
public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolver {
43+
44+
private final ReactiveAdapterRegistry adapterRegistry;
45+
46+
47+
/**
48+
* Class constructor.
49+
* @param registry for adapting to other reactive types from and to Mono
50+
*/
51+
public ErrorsMethodArgumentResolver(ReactiveAdapterRegistry registry) {
52+
Assert.notNull(registry, "'ReactiveAdapterRegistry' is required.");
53+
this.adapterRegistry = registry;
54+
}
55+
56+
57+
/**
58+
* Return the configured {@link ReactiveAdapterRegistry}.
59+
*/
60+
public ReactiveAdapterRegistry getAdapterRegistry() {
61+
return this.adapterRegistry;
62+
}
63+
64+
65+
@Override
66+
public boolean supportsParameter(MethodParameter parameter) {
67+
Class<?> clazz = parameter.getParameterType();
68+
return Errors.class.isAssignableFrom(clazz);
69+
}
70+
71+
72+
@Override
73+
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext context,
74+
ServerWebExchange exchange) {
75+
76+
String name = getModelAttributeName(parameter);
77+
Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);
78+
79+
Mono<?> errorsMono;
80+
if (Mono.class.isAssignableFrom(errors.getClass())) {
81+
errorsMono = (Mono<?>) errors;
82+
}
83+
else if (Errors.class.isAssignableFrom(errors.getClass())) {
84+
errorsMono = Mono.just(errors);
85+
}
86+
else {
87+
throw new IllegalStateException(
88+
"Unexpected Errors/BindingResult type: " + errors.getClass().getName());
89+
}
90+
91+
return errorsMono.cast(Object.class);
92+
}
93+
94+
private String getModelAttributeName(MethodParameter parameter) {
95+
96+
Assert.isTrue(parameter.getParameterIndex() > 0,
97+
"Errors argument must be immediately after a model attribute argument.");
98+
99+
int index = parameter.getParameterIndex() - 1;
100+
MethodParameter attributeParam = new MethodParameter(parameter.getMethod(), index);
101+
Class<?> attributeType = attributeParam.getParameterType();
102+
103+
ResolvableType type = ResolvableType.forMethodParameter(attributeParam);
104+
ReactiveAdapter adapterTo = getAdapterRegistry().getAdapterTo(type.resolve());
105+
106+
Assert.isNull(adapterTo, "Errors/BindingResult cannot be used with an async model attribute. " +
107+
"Either declare the model attribute without the async wrapper type " +
108+
"or handle WebExchangeBindException through the async type.");
109+
110+
ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class);
111+
if (annot != null && StringUtils.hasText(annot.value())) {
112+
return annot.value();
113+
}
114+
// TODO: Conventions does not deal with async wrappers
115+
return ClassUtils.getShortNameAsProperty(attributeType);
116+
}
117+
118+
}

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext co
152152
}
153153

154154
private String getAttributeName(Class<?> valueType, MethodParameter parameter) {
155-
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
156-
String name = (ann != null ? ann.value() : null);
155+
ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class);
156+
if (annot != null && StringUtils.hasText(annot.value())) {
157+
return annot.value();
158+
}
157159
// TODO: Conventions does not deal with async wrappers
158-
return StringUtils.hasText(name) ? name : ClassUtils.getShortNameAsProperty(valueType);
160+
return ClassUtils.getShortNameAsProperty(valueType);
159161
}
160162

161163
private Mono<?> getAttributeMono(String attributeName, Class<?> attributeType,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2002-2016 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+
* http://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.web.reactive.result.method.annotation;
17+
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import reactor.core.publisher.Mono;
21+
import reactor.core.publisher.MonoProcessor;
22+
23+
import org.springframework.core.MethodParameter;
24+
import org.springframework.core.ReactiveAdapterRegistry;
25+
import org.springframework.core.ResolvableType;
26+
import org.springframework.http.HttpMethod;
27+
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
28+
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
29+
import org.springframework.validation.BindingResult;
30+
import org.springframework.validation.Errors;
31+
import org.springframework.web.bind.WebExchangeDataBinder;
32+
import org.springframework.web.bind.annotation.ModelAttribute;
33+
import org.springframework.web.reactive.result.ResolvableMethod;
34+
import org.springframework.web.reactive.result.method.BindingContext;
35+
import org.springframework.web.server.ServerWebExchange;
36+
import org.springframework.web.server.adapter.DefaultServerWebExchange;
37+
import org.springframework.web.server.session.MockWebSessionManager;
38+
import org.springframework.web.server.session.WebSessionManager;
39+
40+
import static junit.framework.TestCase.assertFalse;
41+
import static org.junit.Assert.assertSame;
42+
import static org.junit.Assert.assertTrue;
43+
import static org.springframework.core.ResolvableType.forClass;
44+
import static org.springframework.core.ResolvableType.forClassWithGenerics;
45+
46+
/**
47+
* Unit tests for {@link ErrorsMethodArgumentResolver}.
48+
* @author Rossen Stoyanchev
49+
*/
50+
public class ErrorsArgumentResolverTests {
51+
52+
private ErrorsMethodArgumentResolver resolver ;
53+
54+
private final BindingContext bindingContext = new BindingContext();
55+
56+
private BindingResult bindingResult;
57+
58+
private ServerWebExchange exchange;
59+
60+
private final ResolvableMethod testMethod = ResolvableMethod.onClass(this.getClass()).name("handle");
61+
62+
63+
@Before
64+
public void setUp() throws Exception {
65+
this.resolver = new ErrorsMethodArgumentResolver(new ReactiveAdapterRegistry());
66+
67+
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.POST, "/path");
68+
MockServerHttpResponse response = new MockServerHttpResponse();
69+
WebSessionManager manager = new MockWebSessionManager();
70+
this.exchange = new DefaultServerWebExchange(request, response, manager);
71+
72+
Foo foo = new Foo();
73+
WebExchangeDataBinder binder = this.bindingContext.createDataBinder(this.exchange, foo, "foo");
74+
this.bindingResult = binder.getBindingResult();
75+
}
76+
77+
78+
@Test
79+
public void supports() throws Exception {
80+
81+
MethodParameter parameter = parameter(forClass(Errors.class));
82+
assertTrue(this.resolver.supportsParameter(parameter));
83+
84+
parameter = parameter(forClass(BindingResult.class));
85+
assertTrue(this.resolver.supportsParameter(parameter));
86+
87+
parameter = parameter(forClassWithGenerics(Mono.class, Errors.class));
88+
assertFalse(this.resolver.supportsParameter(parameter));
89+
90+
parameter = parameter(forClass(String.class));
91+
assertFalse(this.resolver.supportsParameter(parameter));
92+
}
93+
94+
@Test
95+
public void resolveErrors() throws Exception {
96+
testResolve(this.bindingResult);
97+
}
98+
99+
@Test
100+
public void resolveErrorsMono() throws Exception {
101+
MonoProcessor<BindingResult> monoProcessor = MonoProcessor.create();
102+
monoProcessor.onNext(this.bindingResult);
103+
testResolve(monoProcessor);
104+
}
105+
106+
@Test(expected = IllegalArgumentException.class)
107+
public void resolveErrorsAfterMonoModelAttribute() throws Exception {
108+
MethodParameter parameter = parameter(forClass(BindingResult.class));
109+
this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange).blockMillis(5000);
110+
}
111+
112+
113+
private void testResolve(Object bindingResult) {
114+
115+
String key = BindingResult.MODEL_KEY_PREFIX + "foo";
116+
this.bindingContext.getModel().asMap().put(key, bindingResult);
117+
118+
MethodParameter parameter = parameter(forClass(Errors.class));
119+
120+
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
121+
.blockMillis(5000);
122+
123+
assertSame(this.bindingResult, actual);
124+
}
125+
126+
127+
private MethodParameter parameter(ResolvableType type) {
128+
return this.testMethod.resolveParam(type);
129+
}
130+
131+
132+
private static class Foo {
133+
134+
private String name;
135+
136+
public Foo() {
137+
}
138+
139+
public Foo(String name) {
140+
this.name = name;
141+
}
142+
143+
public String getName() {
144+
return name;
145+
}
146+
147+
public void setName(String name) {
148+
this.name = name;
149+
}
150+
}
151+
152+
@SuppressWarnings("unused")
153+
void handle(
154+
@ModelAttribute Foo foo,
155+
Errors errors,
156+
@ModelAttribute Mono<Foo> fooMono,
157+
BindingResult bindingResult,
158+
Mono<Errors> errorsMono,
159+
String string) {}
160+
161+
}

0 commit comments

Comments
 (0)