|
10 | 10 | import java.nio.ByteBuffer; |
11 | 11 | import java.time.Instant; |
12 | 12 | import java.util.ArrayList; |
13 | | -import java.util.Arrays; |
14 | 13 | import java.util.List; |
15 | 14 | import java.util.ServiceLoader; |
16 | 15 | import java.util.function.BiConsumer; |
| 16 | +import java.util.function.Supplier; |
17 | 17 | import software.amazon.smithy.java.core.serde.ListSerializer; |
18 | 18 | import software.amazon.smithy.java.core.serde.MapSerializer; |
19 | 19 | import software.amazon.smithy.java.core.serde.SerializationException; |
@@ -127,30 +127,22 @@ static final class ShapeValidator implements ShapeSerializer, MapSerializer { |
127 | 127 | private static final List<CustomConstraint>[] CUSTOM_CONSTRAINTS_BY_TYPE = new List[ShapeType.values().length]; |
128 | 128 | private static final boolean HAS_CUSTOM_CONSTRAINTS; |
129 | 129 |
|
130 | | - private static void addConstraint(int index, CustomConstraint constraint) { |
131 | | - if (CUSTOM_CONSTRAINTS_BY_TYPE[index] == null) { |
132 | | - CUSTOM_CONSTRAINTS_BY_TYPE[index] = new ArrayList<>(); |
133 | | - } |
134 | | - CUSTOM_CONSTRAINTS_BY_TYPE[index].add(constraint); |
135 | | - } |
136 | | - |
137 | 130 | static { |
138 | 131 | // Load all custom constraints at startup and organize by type |
139 | 132 | var loader = ServiceLoader.load(CustomConstraint.class, CustomConstraint.class.getClassLoader()); |
| 133 | + var anyConstraintsFound = false; |
140 | 134 | for (var constraint : loader) { |
141 | 135 | var types = constraint.appliesTo(); |
142 | | - if (types.isEmpty()) { |
143 | | - // Add the constraint to all types if the enum set is empty |
144 | | - for (var i = 0; i < CUSTOM_CONSTRAINTS_BY_TYPE.length; i++) { |
145 | | - addConstraint(i, constraint); |
146 | | - } |
147 | | - } else { |
148 | | - for (var type : types) { |
149 | | - addConstraint(type.ordinal(), constraint); |
| 136 | + for (var type : types) { |
| 137 | + var index = type.ordinal(); |
| 138 | + if (CUSTOM_CONSTRAINTS_BY_TYPE[index] == null) { |
| 139 | + CUSTOM_CONSTRAINTS_BY_TYPE[index] = new ArrayList<>(); |
150 | 140 | } |
| 141 | + CUSTOM_CONSTRAINTS_BY_TYPE[index].add(constraint); |
| 142 | + anyConstraintsFound = true; |
151 | 143 | } |
152 | 144 | } |
153 | | - HAS_CUSTOM_CONSTRAINTS = Arrays.stream(CUSTOM_CONSTRAINTS_BY_TYPE).anyMatch(java.util.Objects::nonNull); |
| 145 | + HAS_CUSTOM_CONSTRAINTS = anyConstraintsFound; |
154 | 146 | } |
155 | 147 |
|
156 | 148 | private final int maxAllowedErrors; |
@@ -546,8 +538,9 @@ private void applyCustomConstraints(Schema schema, Object value) { |
546 | 538 | // Get constraints for this specific type |
547 | 539 | var typeConstraints = CUSTOM_CONSTRAINTS_BY_TYPE[schema.type().ordinal()]; |
548 | 540 | if (typeConstraints != null) { |
| 541 | + Supplier<String> pathSupplier = this::createPath; |
549 | 542 | for (var constraint : typeConstraints) { |
550 | | - var validationErrors = constraint.validate(schema, value, this::createPath); |
| 543 | + var validationErrors = constraint.validate(schema, value, pathSupplier); |
551 | 544 | for (var error : validationErrors) { |
552 | 545 | addError(error); |
553 | 546 | } |
|
0 commit comments