Skip to content

Commit e9f6894

Browse files
committed
Polishing contribution
Closes gh-737
1 parent ebb3ac6 commit e9f6894

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentValueExtractor.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2002-2023 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+
17+
package org.springframework.graphql.data.method.annotation.support;
18+
19+
import jakarta.validation.valueextraction.ExtractedValue;
20+
import jakarta.validation.valueextraction.UnwrapByDefault;
21+
import jakarta.validation.valueextraction.ValueExtractor;
22+
import org.springframework.graphql.data.ArgumentValue;
23+
24+
/**
25+
* {@link ValueExtractor} that enables {@code @Valid} with {@link ArgumentValue},
26+
* and helps to extract the value from it.
27+
*
28+
* @since 1.2.2
29+
*/
30+
@UnwrapByDefault
31+
public final class ArgumentValueValueExtractor implements ValueExtractor<ArgumentValue<@ExtractedValue ?>> {
32+
33+
@Override
34+
public void extractValues(ArgumentValue<?> argumentValue, ValueReceiver receiver) {
35+
if (argumentValue.isPresent()) {
36+
receiver.value(null, argumentValue.value());
37+
}
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
org.springframework.graphql.data.method.annotation.support.ArgumentValueExtractor
1+
org.springframework.graphql.data.method.annotation.support.ArgumentValueValueExtractor

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/ValidationHelperTests.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import jakarta.validation.ConstraintViolationException;
2727
import jakarta.validation.Valid;
2828
import jakarta.validation.Validation;
29+
import jakarta.validation.Validator;
2930
import jakarta.validation.constraints.Max;
3031
import jakarta.validation.constraints.NotBlank;
3132
import jakarta.validation.constraints.NotNull;
@@ -56,54 +57,54 @@ void shouldFailWithNullValidator() {
5657

5758
@Test
5859
void shouldIgnoreMethodsWithoutAnnotations() {
59-
BiConsumer<Object, Object[]> validator = createValidator(MyBean.class, "notValidatedMethod");
60-
assertThat(validator).isNull();
60+
assertThat(validateFunction(MyBean.class, "notValidatedMethod")).isNull();
6161
}
6262

6363
@Test
6464
void shouldRaiseValidationErrorForAnnotatedParams() {
6565
MyBean bean = new MyBean();
6666

67-
BiConsumer<Object, Object[]> validator1 = createValidator(MyBean.class, "myValidMethod");
67+
BiConsumer<Object, Object[]> validator1 = validateFunction(MyBean.class, "myValidMethod");
6868
assertViolation(() -> validator1.accept(bean, new Object[] {null, 2}), "myValidMethod.arg0");
6969
assertViolation(() -> validator1.accept(bean, new Object[] {"test", 12}), "myValidMethod.arg1");
7070

71-
BiConsumer<Object, Object[]> validator2 = createValidator(MyBean.class, "myValidatedParameterMethod");
71+
BiConsumer<Object, Object[]> validator2 = validateFunction(MyBean.class, "myValidatedParameterMethod");
7272
assertViolation(() -> validator2.accept(bean, new Object[] {new ConstrainedInput(100)}), "integerValue");
7373

74-
BiConsumer<Object, Object[]> validator3 = createValidator(MyBean.class, "myValidArgumentValue");
74+
BiConsumer<Object, Object[]> validator3 = validateFunction(MyBean.class, "myValidArgumentValue");
7575
assertViolation(() -> validator3.accept(bean, new Object[] {ArgumentValue.ofNullable("")}), "myValidArgumentValue.arg0");
7676
}
7777

7878
@Test
7979
void shouldRaiseValidationErrorForAnnotatedParamsWithGroups() {
8080
MyValidationGroupsBean bean = new MyValidationGroupsBean();
8181

82-
BiConsumer<Object, Object[]> validator1 = createValidator(MyValidationGroupsBean.class, "myValidMethodWithGroup");
82+
BiConsumer<Object, Object[]> validator1 = validateFunction(MyValidationGroupsBean.class, "myValidMethodWithGroup");
8383
assertViolation(() -> validator1.accept(bean, new Object[] {null}), "myValidMethodWithGroup.arg0");
8484

85-
BiConsumer<Object, Object[]> validator2 = createValidator(MyValidationGroupsBean.class, "myValidMethodWithGroupOnType");
85+
BiConsumer<Object, Object[]> validator2 = validateFunction(MyValidationGroupsBean.class, "myValidMethodWithGroupOnType");
8686
assertViolation(() -> validator2.accept(bean, new Object[] {null}), "myValidMethodWithGroupOnType.arg0");
8787
}
8888

8989
@Test
9090
void shouldRecognizeMethodsThatRequireValidation() {
91-
BiConsumer<Object, Object[]> validator1 = createValidator(RequiresValidationBean.class, "processConstrainedValue");
91+
BiConsumer<Object, Object[]> validator1 = validateFunction(RequiresValidationBean.class, "processConstrainedValue");
9292
assertThat(validator1).isNotNull();
9393

94-
BiConsumer<Object, Object[]> validator2 = createValidator(RequiresValidationBean.class, "processValidInput");
94+
BiConsumer<Object, Object[]> validator2 = validateFunction(RequiresValidationBean.class, "processValidInput");
9595
assertThat(validator2).isNotNull();
9696

97-
BiConsumer<Object, Object[]> validator3 = createValidator(RequiresValidationBean.class, "processValidatedInput");
97+
BiConsumer<Object, Object[]> validator3 = validateFunction(RequiresValidationBean.class, "processValidatedInput");
9898
assertThat(validator3).isNotNull();
9999

100-
BiConsumer<Object, Object[]> validator4 = createValidator(RequiresValidationBean.class, "processValue");
100+
BiConsumer<Object, Object[]> validator4 = validateFunction(RequiresValidationBean.class, "processValue");
101101
assertThat(validator4).isNull();
102102
}
103103

104-
private BiConsumer<Object, Object[]> createValidator(Class<?> handlerType, String methodName) {
105-
return ValidationHelper.create(Validation.buildDefaultValidatorFactory().getValidator())
106-
.getValidationHelperFor(findHandlerMethod(handlerType, methodName));
104+
private BiConsumer<Object, Object[]> validateFunction(Class<?> handlerType, String methodName) {
105+
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
106+
ValidationHelper helper = ValidationHelper.create(validator);
107+
return helper.getValidationHelperFor(findHandlerMethod(handlerType, methodName));
107108
}
108109

109110
private HandlerMethod findHandlerMethod(Class<?> handlerType, String methodName) {

0 commit comments

Comments
 (0)