DATAREST-798 - Fixed invalid implementation of ValidationErrors.

Changed the implementation of ValidationErrors to be based on AbstractBeanPropertyBindingResult to consider the nesting implemented in superclasses and using a PersistentPropertyAccessor to lookup the property values.

ValidatingRepositoryEventListener now uses this implementation if a PersistentEntity can be obtained for the type under consideration, falling back to a DirectFieldBindingResult otherwise.
This commit is contained in:
Oliver Gierke
2016-04-04 15:12:21 +02:00
parent 9e58ab92c5
commit b530cbe471
5 changed files with 143 additions and 65 deletions

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.core;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
/**
* Unit tests for {@link ValidationErrors}.
*
* @author Oliver Gierke
*/
public class ValidationErrorsUnitTests {
KeyValueMappingContext context = new KeyValueMappingContext();
/**
* @see DATAREST-798
*/
@Test
public void exposesNestedViolationsCorrectly() {
ValidationErrors errors = new ValidationErrors(new Foo(), context.getPersistentEntity(Foo.class));
errors.pushNestedPath("bars[0]");
errors.rejectValue("field", "asdf");
errors.popNestedPath();
assertThat(errors.getFieldError().getField(), is("bars[0].field"));
}
static class Foo {
List<Bar> bars = new ArrayList<Bar>();
}
static class Bar {
String field;
}
}

View File

@@ -23,6 +23,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.RepositoryConstraintViolationException;
import org.springframework.data.rest.core.RepositoryTestsConfig;
@@ -59,10 +60,13 @@ public class ValidatorIntegrationTests {
}
@Autowired ConfigurableApplicationContext context;
@Autowired KeyValueMappingContext mappingContext;
@Test(expected = RepositoryConstraintViolationException.class)
public void shouldValidateLastName() throws Exception {
mappingContext.getPersistentEntity(Person.class);
// Empty name should be rejected by PersonNameValidator
context.publishEvent(new BeforeSaveEvent(new Person("Dave", "")));
}