Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support validation non PersistentEntity types #2252

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}