|
| 1 | +/* |
| 2 | + * Copyright 2014-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.restdocs.constraints; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +/** |
| 25 | + * Provides access to descriptions of a class's constraints. |
| 26 | + * |
| 27 | + * @author Dmytro Nosan |
| 28 | + */ |
| 29 | +public class GroupConstraintDescriptions { |
| 30 | + |
| 31 | + private final Class<?> clazz; |
| 32 | + |
| 33 | + private final ConstraintResolver constraintResolver; |
| 34 | + |
| 35 | + private final ConstraintDescriptionResolver descriptionResolver; |
| 36 | + |
| 37 | + /** |
| 38 | + * Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. |
| 39 | + * Constraints will be resolved using a {@link ValidatorConstraintResolver} and |
| 40 | + * descriptions will be resolved using a |
| 41 | + * {@link ResourceBundleConstraintDescriptionResolver}. |
| 42 | + * @param clazz the class |
| 43 | + */ |
| 44 | + public GroupConstraintDescriptions(Class<?> clazz) { |
| 45 | + this(clazz, new ValidatorConstraintResolver(), new ResourceBundleConstraintDescriptionResolver()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. |
| 50 | + * Constraints will be resolved using the given {@code constraintResolver} and |
| 51 | + * descriptions will be resolved using a |
| 52 | + * {@link ResourceBundleConstraintDescriptionResolver}. |
| 53 | + * @param clazz the class |
| 54 | + * @param constraintResolver the constraint resolver |
| 55 | + */ |
| 56 | + public GroupConstraintDescriptions(Class<?> clazz, ConstraintResolver constraintResolver) { |
| 57 | + this(clazz, constraintResolver, new ResourceBundleConstraintDescriptionResolver()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. |
| 62 | + * Constraints will be resolved using a {@link ValidatorConstraintResolver} and |
| 63 | + * descriptions will be resolved using the given {@code descriptionResolver}. |
| 64 | + * @param clazz the class |
| 65 | + * @param descriptionResolver the description resolver |
| 66 | + */ |
| 67 | + public GroupConstraintDescriptions(Class<?> clazz, ConstraintDescriptionResolver descriptionResolver) { |
| 68 | + this(clazz, new ValidatorConstraintResolver(), descriptionResolver); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. |
| 73 | + * Constraints will be resolved using the given {@code constraintResolver} and |
| 74 | + * descriptions will be resolved using the given {@code descriptionResolver}. |
| 75 | + * @param clazz the class |
| 76 | + * @param constraintResolver the constraint resolver |
| 77 | + * @param descriptionResolver the description resolver |
| 78 | + */ |
| 79 | + public GroupConstraintDescriptions(Class<?> clazz, ConstraintResolver constraintResolver, |
| 80 | + ConstraintDescriptionResolver descriptionResolver) { |
| 81 | + this.clazz = clazz; |
| 82 | + this.constraintResolver = constraintResolver; |
| 83 | + this.descriptionResolver = descriptionResolver; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns a list of the descriptions for the constraints on the given property. |
| 88 | + * @param property the property |
| 89 | + * @param groups list of groups targeted for constraints |
| 90 | + * @return the list of constraint descriptions |
| 91 | + */ |
| 92 | + public List<String> descriptionsForProperty(String property, Class<?>... groups) { |
| 93 | + List<Constraint> constraints = this.constraintResolver.resolveForProperty(property, this.clazz); |
| 94 | + List<String> descriptions = new ArrayList<>(); |
| 95 | + for (Constraint constraint : constraints) { |
| 96 | + if (includes(constraint, groups)) { |
| 97 | + descriptions.add(this.descriptionResolver.resolveDescription(constraint)); |
| 98 | + } |
| 99 | + } |
| 100 | + Collections.sort(descriptions); |
| 101 | + return descriptions; |
| 102 | + } |
| 103 | + |
| 104 | + private boolean includes(Constraint constraint, Class<?>[] groups) { |
| 105 | + if (groups.length == 0 && constraint.getGroups().isEmpty()) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + return Stream.of(groups).anyMatch((clazz) -> constraint.getGroups().contains(clazz)); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments