Validation of subclass property fails prior to being persisted for the first time
@odrotbohm đang làm issue này rồi.
Từ ngày 7/11/2022.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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);
}
- Ngôn ngữ chính
- Java
- Star
- 957
- Fork
- 571
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của spring-projects/spring-data-rest
-
Release 5.0.9 (2025.1.9)Đang mởtype: task
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
spring-projects/spring-data-rest#2624 ·
-
Release 5.1.3 (2026.0.3)Đang mởtype: task
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 35/100
spring-projects/spring-data-rest#2623 ·
-
Release 5.2.0 (2026.1.0)Đang mởtype: task
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 15/100
spring-projects/spring-data-rest#2622 ·
-
Merge patch drops linkable associations of existing array elements since 4.3Có thể đã có người làm @odrotbohm đã nhận 41 ngày trước. Đang mởstatus: feedback-provided status: waiting-for-triage
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 78/100
spring-projects/spring-data-rest#2596 · 3 bình luận · 1 người được giao ·
-
Querydsl filtering silently ignores nested property paths since GH-2572Có thể đã có người làm @odrotbohm đã nhận 41 ngày trước. Đang mởtype: regression
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 76/100
spring-projects/spring-data-rest#2579 · 1 reaction · 1 người được giao ·
Tất cả issue của spring-projects/spring-data-rest
Issue tương tự
-
finsim-core good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
commons-app/apps-android-commons#7275 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
sonatype-nexus-community/central-publishing-maven-plugin#13 ·
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
liberation-data/drivine4j#682 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Document and enforce pull request review guidelinesCó thể đã có người làm @dashorst đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày