Skip to content

Commit

Permalink
Validate mapping super types (smallrye#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Apr 14, 2023
1 parent 9992a6e commit f50db35
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int getSuperTypeCount() {
return superTypes.length;
}

ConfigMappingInterface[] getSuperTypes() {
public ConfigMappingInterface[] getSuperTypes() {
return superTypes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ default void validateMappingInterface(
final Object mappingObject,
final List<Problem> problems) {

for (ConfigMappingInterface superType : mappingInterface.getSuperTypes()) {
for (Property property : superType.getProperties()) {
validateProperty(property, currentPath, namingStrategy, mappingObject, false, problems);
}
}

for (Property property : mappingInterface.getProperties()) {
validateProperty(property, currentPath, namingStrategy, mappingObject, false, problems);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,34 @@ interface Nested {
}
}

@Test
void hierarchy() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withValidator(new BeanValidationConfigValidatorImpl())
.withMapping(Child.class)
.withSources(config("validator.child.number", "1"))
.build();

ConfigValidationException validationException = assertThrows(ConfigValidationException.class,
() -> config.getConfigMapping(Child.class));
List<String> validations = new ArrayList<>();
for (int i = 0; i < validationException.getProblemCount(); i++) {
validations.add(validationException.getProblem(i).getMessage());
}
assertEquals(1, validations.size());
assertTrue(validations.contains("validator.child.number must be greater than or equal to 10"));
}

public interface Parent {
@Min(10)
Integer number();
}

@ConfigMapping(prefix = "validator.child")
public interface Child extends Parent {

}

private static void assertValidationsEqual(List<String> validations, String... expectedProblemMessages) {
List<String> remainingActual = new ArrayList<>(validations);
List<String> remainingExpected = Stream.of(expectedProblemMessages).collect(Collectors.toList());
Expand Down

0 comments on commit f50db35

Please sign in to comment.