Skip to content

Commit c8abdf0

Browse files
committed
DATAREST-805 - ValidationError nor exposes rejected value as is.
Previously ValidationError captured the toString() variant of the rejected value. We now return the rejected value as is.
1 parent 5f3305d commit c8abdf0

File tree

2 files changed

+111
-35
lines changed

2 files changed

+111
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,11 +15,14 @@
1515
*/
1616
package org.springframework.data.rest.webmvc.support;
1717

18+
import lombok.Value;
19+
1820
import java.util.ArrayList;
1921
import java.util.List;
2022

2123
import org.springframework.context.support.MessageSourceAccessor;
2224
import org.springframework.data.rest.core.RepositoryConstraintViolationException;
25+
import org.springframework.util.Assert;
2326
import org.springframework.validation.FieldError;
2427

2528
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -32,15 +35,22 @@ public class RepositoryConstraintViolationExceptionMessage {
3235

3336
private final List<ValidationError> errors = new ArrayList<ValidationError>();
3437

35-
public RepositoryConstraintViolationExceptionMessage(RepositoryConstraintViolationException violationException,
38+
/**
39+
* Creates a new {@link RepositoryConstraintViolationExceptionMessage} for the given
40+
* {@link RepositoryConstraintViolationException} and {@link MessageSourceAccessor}.
41+
*
42+
* @param exception must not be {@literal null}.
43+
* @param accessor must not be {@literal null}.
44+
*/
45+
public RepositoryConstraintViolationExceptionMessage(RepositoryConstraintViolationException exception,
3646
MessageSourceAccessor accessor) {
3747

38-
for (FieldError fieldError : violationException.getErrors().getFieldErrors()) {
39-
40-
String message = accessor.getMessage(fieldError);
48+
Assert.notNull(exception, "RepositoryConstraintViolationException must not be null!");
49+
Assert.notNull(accessor, "MessageSourceAccessor must not be null!");
4150

42-
this.errors.add(new ValidationError(fieldError.getObjectName(), message,
43-
String.format("%s", fieldError.getRejectedValue()), fieldError.getField()));
51+
for (FieldError fieldError : exception.getErrors().getFieldErrors()) {
52+
this.errors.add(ValidationError.of(fieldError.getObjectName(), fieldError.getField(),
53+
fieldError.getRejectedValue(), accessor.getMessage(fieldError)));
4454
}
4555
}
4656

@@ -49,34 +59,10 @@ public List<ValidationError> getErrors() {
4959
return errors;
5060
}
5161

62+
@Value(staticConstructor = "of")
5263
public static class ValidationError {
53-
54-
private final String entity;
55-
private final String message;
56-
private final String invalidValue;
57-
private final String property;
58-
59-
public ValidationError(String entity, String message, String invalidValue, String property) {
60-
this.entity = entity;
61-
this.message = message;
62-
this.invalidValue = invalidValue;
63-
this.property = property;
64-
}
65-
66-
public String getEntity() {
67-
return entity;
68-
}
69-
70-
public String getMessage() {
71-
return message;
72-
}
73-
74-
public String getInvalidValue() {
75-
return invalidValue;
76-
}
77-
78-
public String getProperty() {
79-
return property;
80-
}
64+
String entity, property;
65+
Object invalidValue;
66+
String message;
8167
}
8268
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 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.data.rest.webmvc.support;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.List;
24+
import java.util.Locale;
25+
import java.util.Map;
26+
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.springframework.context.support.MessageSourceAccessor;
30+
import org.springframework.context.support.StaticMessageSource;
31+
import org.springframework.data.rest.core.RepositoryConstraintViolationException;
32+
import org.springframework.data.rest.webmvc.support.RepositoryConstraintViolationExceptionMessage.ValidationError;
33+
import org.springframework.validation.Errors;
34+
import org.springframework.validation.MapBindingResult;
35+
36+
/**
37+
* Unit tests for {@link RepositoryConstraintViolationExceptionMessage}
38+
*
39+
* @author Oliver Gierke
40+
*/
41+
public class RepositoryConstraintViolationExceptionMessageUnitTests {
42+
43+
MessageSourceAccessor accessor;
44+
RepositoryConstraintViolationException exception;
45+
46+
@Before
47+
public void setUp() {
48+
49+
StaticMessageSource messageSource = new StaticMessageSource();
50+
messageSource.addMessage("code", Locale.ENGLISH, "message");
51+
52+
this.accessor = new MessageSourceAccessor(messageSource, Locale.ENGLISH);
53+
this.exception = new RepositoryConstraintViolationException(new MapBindingResult(Collections.emptyMap(), "object"));
54+
}
55+
56+
@Test(expected = IllegalArgumentException.class)
57+
public void rejectsNullException() {
58+
new RepositoryConstraintViolationExceptionMessage(null, accessor);
59+
}
60+
61+
@Test(expected = IllegalArgumentException.class)
62+
public void rejectsNullAccessor() {
63+
new RepositoryConstraintViolationExceptionMessage(exception, null);
64+
}
65+
66+
@Test
67+
public void calidationErrorsCaptureRejectedValueAsIs() {
68+
69+
assertRejectedValue("stringValue", "string");
70+
assertRejectedValue("intValue", 1);
71+
assertRejectedValue("nullValue", null);
72+
}
73+
74+
private void assertRejectedValue(String key, Object value) {
75+
76+
Map<String, Object> map = new HashMap<String, Object>();
77+
map.put(key, value);
78+
79+
Errors errors = new MapBindingResult(map, "object");
80+
errors.rejectValue(key, "code");
81+
82+
RepositoryConstraintViolationExceptionMessage message = new RepositoryConstraintViolationExceptionMessage(
83+
new RepositoryConstraintViolationException(errors), accessor);
84+
85+
List<ValidationError> result = message.getErrors();
86+
87+
assertThat(result, hasSize(1));
88+
assertThat(result.get(0).getInvalidValue(), is(value));
89+
}
90+
}

0 commit comments

Comments
 (0)