Skip to content

Commit

Permalink
Support default visibility when validating ConfigMappings (#955)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jul 5, 2023
1 parent cd0a22e commit 1da49be
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.smallrye.config.validator;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -206,9 +207,20 @@ default void validatePropertyValue(
final List<Problem> problems) {

try {
Method methodToInvoke;
if (property.getMethod().canAccess(mappingObject)) {
methodToInvoke = property.getMethod();
} else {
try {
methodToInvoke = mappingObject.getClass().getMethod(property.getMethod().getName());
} catch (NoSuchMethodException e) {
// This never happens, because we generated the class, and we know the method exists
throw new RuntimeException(e);
}
}

Set<ConstraintViolation<Object>> violations = getValidator().forExecutables().validateReturnValue(mappingObject,
property.getMethod(),
property.getMethod().invoke(mappingObject));
property.getMethod(), methodToInvoke.invoke(mappingObject));
for (ConstraintViolation<Object> violation : violations) {
problems.add(new Problem(interpolateMessage(currentPath, namingStrategy, property, violation)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.smallrye.config.validator.external;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.hibernate.validator.constraints.Length;
import org.junit.jupiter.api.Test;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.validator.BeanValidationConfigValidatorImpl;

public class ValidationVisibilityTest {
@Test
void visibility() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withDefaultValue("default.visibility.value", "12345678")
.withValidator(new BeanValidationConfigValidatorImpl())
.withMapping(DefaultVisibility.class)
.build();

DefaultVisibility mapping = config.getConfigMapping(DefaultVisibility.class);

assertEquals("12345678", mapping.value());
}

@ConfigMapping(prefix = "default.visibility")
interface DefaultVisibility {
@Length(max = 10)
String value();
}
}

0 comments on commit 1da49be

Please sign in to comment.