spring-data-rest
spring-data-rest copied to clipboard
Validation of subclass property fails prior to being persisted for the first time
I am working with a Spring Boot/Spring Data MongoDB project that exposes its repositories via Spring Data REST. One of the model classes has a polymorphic field that may be one of several subtypes. Before saving, beans are validated using the Spring Data REST ValidatingRepositoryEventListener. One of the properties being validated is one of the properties that is only present on a subtype.
When I initially start my Spring Boot application, attempts to persist documents with an invalid subtype-only properties fail due to a NotReadablePropertyException. However, after I have successfully persisted a document that uses the given property, future attempts to persist documents with that subtype-only property are correctly rejected as 400 responses containing the expected validation failure. Restarting the application causes validations to error once again until a type is once again persisted.
Stack trace
org.springframework.beans.NotReadablePropertyException: Invalid property 'a' of bean class [com.example.TypeA]: Bean property 'a' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.data.rest.core.ValidationErrors$1.lambda$lookupValueOn$1(ValidationErrors.java:101) ~[spring-data-rest-core-3.7.3.jar:3.7.3]
at java.base/java.util.Optional.orElseThrow(Optional.java:403) ~[na:na]
at org.springframework.data.rest.core.ValidationErrors$1.lookupValueOn(ValidationErrors.java:101) ~[spring-data-rest-core-3.7.3.jar:3.7.3]
at org.springframework.data.rest.core.ValidationErrors$1.getPropertyValue(ValidationErrors.java:85) ~[spring-data-rest-core-3.7.3.jar:3.7.3]
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.validation.AbstractBindingResult.getRawFieldValue(AbstractBindingResult.java:284) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.getRejectedValue(SpringValidatorAdapter.java:318) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:174) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:109) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:89) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:56) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.data.rest.core.event.ValidatingRepositoryEventListener.validate(ValidatingRepositoryEventListener.java:174) ~[spring-data-rest-core-3.7.3.jar:3.7.3]
at org.springframework.data.rest.core.event.ValidatingRepositoryEventListener.onBeforeCreate(ValidatingRepositoryEventListener.java:96) ~[spring-data-rest-core-3.7.3.jar:3.7.3]
at org.springframework.data.rest.core.event.AbstractRepositoryEventListener.onApplicationEvent(AbstractRepositoryEventListener.java:50) ~[spring-data-rest-core-3.7.3.jar:3.7.3]
at org.springframework.data.rest.core.event.AbstractRepositoryEventListener.onApplicationEvent(AbstractRepositoryEventListener.java:29) ~[spring-data-rest-core-3.7.3.jar:3.7.3]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) ~[spring-context-5.3.23.jar:5.3.23]
at org.springframework.data.rest.webmvc.RepositoryEntityController.createAndReturn(RepositoryEntityController.java:473) ~[spring-data-rest-webmvc-3.7.3.jar:3.7.3]
at org.springframework.data.rest.webmvc.RepositoryEntityController.postCollectionResource(RepositoryEntityController.java:266) ~[spring-data-rest-webmvc-3.7.3.jar:3.7.3]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.23.jar:5.3.23]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071) ~[spring-webmvc-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) ~[spring-webmvc-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.23.jar:5.3.23]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.23.jar:5.3.23]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.65.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.23.jar:5.3.23]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.65.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.23.jar:5.3.23]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.23.jar:5.3.23]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.23.jar:5.3.23]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Analysis
It looks like the underlying culprit here is ValidationErrors used by ValidatingRepositoryEventListener. It delegates to PersistentEntities to access the property. Prior to persisting the property that first time, the underlying PersistentEntity does not appear to have a property for that subtype's property. After persisting the property, it does.
Example
build.gradle
plugins {
id 'org.springframework.boot' version '2.7.4'
id 'io.spring.dependency-management' version '1.0.14.RELEASE'
id 'java'
}
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Configuration
public class RestRepositoryValidatorConfig {
@Bean
public ValidatingRepositoryEventListener beforeSaveValidator(
Validator validator,
ObjectFactory<PersistentEntities> persistentEntitiesFactory) {
ValidatingRepositoryEventListener listener =
new ValidatingRepositoryEventListener(persistentEntitiesFactory);
listener.addValidator("beforeCreate", validator);
listener.addValidator("beforeSave", validator);
return listener;
}
}
import org.springframework.data.mongodb.core.mapping.Document;
import javax.validation.Valid;
@Document
public class MyDocument {
@Valid
private PolymorphicType value;
public MyDocument() {}
public MyDocument(PolymorphicType value) {
this.value = value;
}
public PolymorphicType getValue() {return value;}
public void setValue(PolymorphicType value) {this.value = value;}
}
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.springframework.data.annotation.Id;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({@JsonSubTypes.Type(TypeA.class), @JsonSubTypes.Type(TypeB.class)})
public abstract class PolymorphicType {
@Id
private String id;
public abstract String getType();
public String getId() {return id;}
public void setId(String id) {this.id = id;}
}
import javax.validation.constraints.NotNull;
public class TypeA extends PolymorphicType {
@NotNull private String a;
public TypeA() {}
public TypeA(String a) {this.a = a;}
@Override public String getType() {return "TypeA";}
public String getA() {return a;}
public void setA(String a) {this.a = a;}
}
import javax.validation.constraints.NotNull;
public class TypeB extends PolymorphicType {
@NotNull private String b;
public TypeB() {}
public TypeB(String b) {this.b = b;}
@Override public String getType() {return "TypeB";}
public String getB() {return b;}
public void setB(String b) {this.b = b;}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class RestTest {
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private ObjectMapper objectMapper;
// This test fails with the error
@Test
public void validateSubtype() throws JsonProcessingException {
MyDocument myDocument = new MyDocument(new TypeA());
ResponseEntity<String> response = testRestTemplate
.postForEntity("/myDocuments", myDocument, String.class);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
JsonNode responseJson = objectMapper.readTree(response.getBody());
assertEquals(1, responseJson.at("/errors").size());
assertEquals("value.a", responseJson.at("/errors/0/property").textValue());
}
// This test succeeds
@Test
public void validateSubtypeAfterSavingType() throws JsonProcessingException {
MyDocument myDocument = new MyDocument(new TypeA());
testRestTemplate.postForEntity("/myDocuments",
new MyDocument(new TypeA("aValue")), String.class);
ResponseEntity<String> response = testRestTemplate
.postForEntity("/myDocuments", myDocument, String.class);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
JsonNode responseJson = objectMapper.readTree(response.getBody());
assertEquals(1, responseJson.at("/errors").size());
assertEquals("value.a", responseJson.at("/errors/0/property").textValue());
}
}
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.ValidationErrors;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class ValidatorTest {
@Autowired
private Validator validator;
@Autowired
private MyDocumentRepository repository;
@Autowired
private PersistentEntities persistentEntities;
// This test fails with the error
@Test
public void validateSubtype() {
MyDocument myDocument = new MyDocument(new TypeA());
Errors errors = new ValidationErrors(myDocument, persistentEntities);
validator.validate(myDocument, errors);
assertEquals(1, errors.getFieldErrorCount());
assertEquals("value.a", errors.getFieldError().getField());
assertEquals("NotNull", errors.getFieldError().getCode());
}
// This test succeeds
@Test
public void validateSubtypeAfterSavingType() {
MyDocument myDocument = new MyDocument(new TypeA());
Errors errors = new ValidationErrors(myDocument, persistentEntities);
repository.save(myDocument);
validator.validate(myDocument, errors);
assertEquals(1, errors.getFieldErrorCount());
assertEquals("value.a", errors.getFieldError().getField());
assertEquals("NotNull", errors.getFieldError().getCode());
}
// This test succeeds
@Test
public void validateSubtypeUsingBeanPropertyBindingResult() {
MyDocument myDocument = new MyDocument(new TypeA());
Errors errors = new BeanPropertyBindingResult(myDocument, "myDocument");
validator.validate(myDocument, errors);
assertEquals(1, errors.getFieldErrorCount());
assertEquals("value.a", errors.getFieldError().getField());
assertEquals("NotNull", errors.getFieldError().getCode());
}
}
Workaround
A workaround is to manually add the subtypes as PersistentEntites prior to first access.
@Autowired
private PersistentEntities persistentEntities;
@PostConstruct
public void workAroundSubclassPropertiesValidationIssue() {
Arrays.stream(PolymorphicType.class.getAnnotation(JsonSubTypes.class).value())
.map(JsonSubTypes.Type::value)
.forEach(persistentEntities::getRequiredPersistentEntity);
}
What advantage does the custom ValidationErrors type buy over just using the standard BeanPropertyBindingResult?