Skip to content

Commit

Permalink
Support Validation for fields the type of which is not a PersistentEn…
Browse files Browse the repository at this point in the history
…tity
  • Loading branch information
f-cramer committed Apr 13, 2023
1 parent 88b6414 commit f9962fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Optional;

import org.springframework.beans.BeansException;
import org.springframework.beans.ConfigurablePropertyAccessor;
Expand All @@ -39,6 +40,7 @@
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Florian Cramer
*/
public class ValidationErrors extends AbstractPropertyBindingResult {

Expand Down Expand Up @@ -92,7 +94,12 @@ public Object getPropertyValue(String propertyName) throws BeansException {
*/
private Object lookupValueOn(Object value, String segment) {

PersistentProperty<?> property = entities.getPersistentEntity(value.getClass()) //
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(value.getClass());
if (!entity.isPresent()) {
return new DirectFieldAccessor(value).getPropertyValue(segment);
}

PersistentProperty<?> property = entity //
.map(it -> it.getPersistentProperty(PropertyAccessorUtils.getPropertyName(segment))) //
.orElseThrow(() -> new NotReadablePropertyException(value.getClass(), segment));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 original author or authors.
* Copyright 2016-2023 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.
Expand All @@ -24,14 +24,18 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.util.TypeInformation;
import org.springframework.validation.Errors;

/**
* Unit tests for {@link ValidationErrors}.
*
* @author Oliver Gierke
* @author Florian Cramer
*/
class ValidationErrorsUnitTests {

Expand All @@ -40,7 +44,7 @@ class ValidationErrorsUnitTests {
@BeforeEach
void setUp() {

KeyValueMappingContext<?, ?> context = new KeyValueMappingContext<>();
KeyValueMappingContext<?, ?> context = new TestKeyValueMappingContext<>();
context.getPersistentEntity(Foo.class);

this.entities = new PersistentEntities(Arrays.asList(context));
Expand Down Expand Up @@ -71,6 +75,14 @@ void returnsNullForPropertyValue() {
assertThat(errors.getFieldValue("bar")).isNull();
}

@Test // GH-2252
void getsTheNestedFieldsValueForNonPersistentEntity() {

ValidationErrors errors = new ValidationErrors(new Foo(), entities);

assertThat(errors.getFieldValue("qux.field")).isEqualTo("World");
}

private static void expectedErrorBehavior(Errors errors) {

assertThat(errors.getFieldValue("bars")).isNotNull();
Expand All @@ -88,9 +100,22 @@ private static void expectedErrorBehavior(Errors errors) {
static class Foo {
List<Bar> bars = Collections.singletonList(new Bar());
Bar bar = null;
Qux qux = new Qux();
}

static class Bar {
String field = "Hello";
}

static class Qux {
String field = "World";
}

static class TestKeyValueMappingContext<E extends KeyValuePersistentEntity<?, P>, P extends KeyValuePersistentProperty<P>> extends KeyValueMappingContext<E, P> {

@Override
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
return Qux.class != type.getType() && super.shouldCreatePersistentEntityFor(type);
}
}
}

0 comments on commit f9962fe

Please sign in to comment.