-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for disabling constraints
- Loading branch information
1 parent
a7f51b3
commit 6122767
Showing
22 changed files
with
1,666 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...dation-api/src/main/java/net/croz/nrich/validation/api/constraint/DisableConstraints.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.api.constraint; | ||
|
||
import jakarta.validation.Payload; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* Adds support for disabling constraints. Constraints can be disabled either on property, method or type level. | ||
*/ | ||
@SuppressWarnings("unused") | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Repeatable(DisableConstraints.List.class) | ||
@Documented | ||
public @interface DisableConstraints { | ||
|
||
/** | ||
* Array of constraint types to disable. | ||
* @return array of constraint types to disable | ||
*/ | ||
Class<? extends Annotation>[] value(); | ||
|
||
/** | ||
* Property name for which to disable constraints (only applicable on type). | ||
* @return property name for which to disable constraints | ||
*/ | ||
String propertyName() default ""; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
/** | ||
* Defines several {@link DisableConstraints} annotations on the same element. | ||
* | ||
* @see DisableConstraints | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
DisableConstraints[] value(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...et/croz/nrich/validation/constraint/support/disableconstraints/BeanDescriptorAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.constraint.support.disableconstraints; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import jakarta.validation.metadata.BeanDescriptor; | ||
import jakarta.validation.metadata.ConstraintDescriptor; | ||
import jakarta.validation.metadata.ConstructorDescriptor; | ||
import jakarta.validation.metadata.MethodDescriptor; | ||
import jakarta.validation.metadata.MethodType; | ||
import jakarta.validation.metadata.PropertyDescriptor; | ||
import java.lang.annotation.Annotation; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
public class BeanDescriptorAdapter implements BeanDescriptor { | ||
|
||
private final BeanDescriptor target; | ||
|
||
private final Map<String, List<Class<? extends Annotation>>> disabledConstraintsPathMap; | ||
|
||
@Override | ||
public PropertyDescriptor getConstraintsForProperty(String propertyName) { | ||
String path = PathUtil.getPath(target.getElementClass(), propertyName); | ||
|
||
return new PropertyDescriptorAdapter(target.getConstraintsForProperty(propertyName), disabledConstraintsPathMap.getOrDefault(path, Collections.emptyList())); | ||
} | ||
|
||
@Override | ||
public Set<PropertyDescriptor> getConstrainedProperties() { | ||
return target.getConstrainedProperties().stream() | ||
.map(propertyDescriptor -> getConstraintsForProperty(propertyDescriptor.getPropertyName())) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public ConstraintFinder findConstraints() { | ||
ConstraintFinder finder = target.findConstraints(); | ||
String path = PathUtil.getPath(target.getElementClass(), null); | ||
|
||
return new ConstraintFinderAdapter(finder, disabledConstraintsPathMap.getOrDefault(path, Collections.emptyList())); | ||
} | ||
|
||
@Override | ||
public boolean isBeanConstrained() { | ||
return target.isBeanConstrained(); | ||
} | ||
|
||
@Override | ||
public MethodDescriptor getConstraintsForMethod(String methodName, Class<?>... parameterTypes) { | ||
return target.getConstraintsForMethod(methodName, parameterTypes); | ||
} | ||
|
||
@Override | ||
public Set<MethodDescriptor> getConstrainedMethods(MethodType methodType, MethodType... methodTypes) { | ||
return target.getConstrainedMethods(methodType, methodTypes); | ||
} | ||
|
||
@Override | ||
public ConstructorDescriptor getConstraintsForConstructor(Class<?>... parameterTypes) { | ||
return target.getConstraintsForConstructor(parameterTypes); | ||
} | ||
|
||
@Override | ||
public Set<ConstructorDescriptor> getConstrainedConstructors() { | ||
return target.getConstrainedConstructors(); | ||
} | ||
|
||
@Override | ||
public boolean hasConstraints() { | ||
return target.hasConstraints(); | ||
} | ||
|
||
@Override | ||
public Class<?> getElementClass() { | ||
return target.getElementClass(); | ||
} | ||
|
||
@Override | ||
public Set<ConstraintDescriptor<?>> getConstraintDescriptors() { | ||
return target.getConstraintDescriptors(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../croz/nrich/validation/constraint/support/disableconstraints/ConstraintFinderAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.constraint.support.disableconstraints; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import jakarta.validation.metadata.ConstraintDescriptor; | ||
import jakarta.validation.metadata.ElementDescriptor; | ||
import jakarta.validation.metadata.Scope; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.annotation.ElementType; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
public class ConstraintFinderAdapter implements ElementDescriptor.ConstraintFinder { | ||
|
||
private final ElementDescriptor.ConstraintFinder target; | ||
|
||
private final List<Class<? extends Annotation>> disabledConstraintTypes; | ||
|
||
@Override | ||
public Set<ConstraintDescriptor<?>> getConstraintDescriptors() { | ||
Set<ConstraintDescriptor<?>> constraintDescriptors = target.getConstraintDescriptors(); | ||
|
||
return constraintDescriptors.stream().filter(constraintDescriptor -> !disabledConstraintTypes.contains(constraintDescriptor.getAnnotation().annotationType())) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public ElementDescriptor.ConstraintFinder unorderedAndMatchingGroups(Class<?>... groups) { | ||
target.unorderedAndMatchingGroups(groups); | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public ElementDescriptor.ConstraintFinder lookingAt(Scope scope) { | ||
target.lookingAt(scope); | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public ElementDescriptor.ConstraintFinder declaredOn(ElementType... types) { | ||
target.declaredOn(types); | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public boolean hasConstraints() { | ||
return target.hasConstraints(); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...lidation/constraint/support/disableconstraints/DisableConstraintsAnnotationProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.constraint.support.disableconstraints; | ||
|
||
import net.croz.nrich.validation.api.constraint.DisableConstraints; | ||
import org.springframework.util.ReflectionUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class DisableConstraintsAnnotationProcessor { | ||
|
||
private static final Pattern GETTER_METHOD_PATTERN = Pattern.compile("^(get|is)([A-Z].*)$"); | ||
|
||
private final ConcurrentMap<Class<?>, Map<String, List<Class<? extends Annotation>>>> disableConstraintsHolderMap = new ConcurrentHashMap<>(); | ||
|
||
public Map<String, List<Class<? extends Annotation>>> getDisabledConstraintForType(Class<?> type) { | ||
return disableConstraintsHolderMap.computeIfAbsent(type, this::createDisableConstraintsPathMap); | ||
} | ||
|
||
private Map<String, List<Class<? extends Annotation>>> createDisableConstraintsPathMap(Class<?> type) { | ||
Map<String, List<Class<? extends Annotation>>> pathHolderMap = new HashMap<>(); | ||
|
||
ReflectionUtils.doWithFields(type, field -> { | ||
DisableConstraints[] disableConstraints = field.getAnnotationsByType(DisableConstraints.class); | ||
|
||
registerDisableConstraints(pathHolderMap, disableConstraints, PathUtil.getPath(type, field.getName()), false); | ||
}); | ||
|
||
ReflectionUtils.doWithMethods(type, method -> { | ||
DisableConstraints[] disableConstraints = method.getAnnotationsByType(DisableConstraints.class); | ||
Matcher matcher = GETTER_METHOD_PATTERN.matcher(method.getName()); | ||
|
||
if (matcher.matches()) { | ||
String propertyName = StringUtils.uncapitalize(matcher.group(2)); | ||
|
||
registerDisableConstraints(pathHolderMap, disableConstraints, PathUtil.getPath(type, propertyName), false); | ||
} | ||
}); | ||
|
||
DisableConstraints[] disableConstraints = type.getAnnotationsByType(DisableConstraints.class); | ||
|
||
registerDisableConstraints(pathHolderMap, disableConstraints, PathUtil.getPath(type, null), true); | ||
|
||
return pathHolderMap; | ||
} | ||
|
||
private void registerDisableConstraints(Map<String, List<Class<? extends Annotation>>> pathHolderMap, DisableConstraints[] disableConstraints, String path, boolean isTypeAnnotation) { | ||
Arrays.stream(disableConstraints).forEach(disableConstraint -> { | ||
if (StringUtils.hasText(disableConstraint.propertyName()) && !isTypeAnnotation) { | ||
throw new IllegalArgumentException("Property name not allowed on method or property annotation."); | ||
} | ||
|
||
String fullPath = PathUtil.getPath(path, disableConstraint.propertyName()); | ||
|
||
pathHolderMap.put(fullPath, List.of(disableConstraint.value())); | ||
}); | ||
} | ||
} |
Oops, something went wrong.