|
| 1 | +package io.swagger.v3.core.resolving; |
| 2 | + |
| 3 | +import io.swagger.v3.core.converter.ModelConverters; |
| 4 | +import io.swagger.v3.oas.models.media.IntegerSchema; |
| 5 | +import io.swagger.v3.oas.models.media.Schema; |
| 6 | +import org.testng.annotations.Test; |
| 7 | + |
| 8 | +import javax.validation.Constraint; |
| 9 | +import javax.validation.OverridesAttribute; |
| 10 | +import javax.validation.Payload; |
| 11 | +import javax.validation.constraints.Max; |
| 12 | +import javax.validation.constraints.Min; |
| 13 | +import javax.validation.constraints.NotNull; |
| 14 | +import javax.validation.constraints.Pattern; |
| 15 | +import javax.validation.constraints.Size; |
| 16 | +import java.lang.annotation.ElementType; |
| 17 | +import java.lang.annotation.Retention; |
| 18 | +import java.lang.annotation.RetentionPolicy; |
| 19 | +import java.lang.annotation.Target; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import static org.testng.Assert.assertEquals; |
| 23 | +import static org.testng.Assert.assertNotNull; |
| 24 | + |
| 25 | +public class ComposedConstraintMetaAnnotationTest { |
| 26 | + |
| 27 | + @Min(0) |
| 28 | + @Max(999) |
| 29 | + @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) |
| 30 | + @Retention(RetentionPolicy.RUNTIME) |
| 31 | + @Constraint(validatedBy = {}) |
| 32 | + public @interface ValidStoreId { |
| 33 | + String message() default "Invalid store ID"; |
| 34 | + Class<?>[] groups() default {}; |
| 35 | + Class<? extends Payload>[] payload() default {}; |
| 36 | + } |
| 37 | + |
| 38 | + @Size(min = 1, max = 50) |
| 39 | + @Target({ElementType.FIELD, ElementType.PARAMETER}) |
| 40 | + @Retention(RetentionPolicy.RUNTIME) |
| 41 | + @Constraint(validatedBy = {}) |
| 42 | + public @interface ValidName { |
| 43 | + String message() default "Invalid name"; |
| 44 | + Class<?>[] groups() default {}; |
| 45 | + Class<? extends Payload>[] payload() default {}; |
| 46 | + } |
| 47 | + |
| 48 | + @Pattern(regexp = "^[a-z0-9._%+\\-]+@[a-z0-9.\\-]+\\.[a-z]{2,}$") |
| 49 | + @Target({ElementType.FIELD, ElementType.PARAMETER}) |
| 50 | + @Retention(RetentionPolicy.RUNTIME) |
| 51 | + @Constraint(validatedBy = {}) |
| 52 | + public @interface ValidEmail { |
| 53 | + String message() default "Invalid email"; |
| 54 | + Class<?>[] groups() default {}; |
| 55 | + Class<? extends Payload>[] payload() default {}; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Mimics how Hibernate Validator's @Range works: meta-annotations @Min/@Max carry default |
| 60 | + * values, while the actual per-use values are meant to be applied via @OverridesAttribute. |
| 61 | + * Our implementation reads meta-annotations from the annotation *type definition*, so it |
| 62 | + * always sees the defaults (min=0, max=Long.MAX_VALUE) — not whatever the caller passes |
| 63 | + * as @ValidRange(min=5, max=50). This is a known limitation documented by the test below. |
| 64 | + */ |
| 65 | + @Min(0) |
| 66 | + @Max(Long.MAX_VALUE) |
| 67 | + @Target({ElementType.FIELD, ElementType.PARAMETER}) |
| 68 | + @Retention(RetentionPolicy.RUNTIME) |
| 69 | + @Constraint(validatedBy = {}) |
| 70 | + public @interface ValidRange { |
| 71 | + @OverridesAttribute(constraint = Min.class, name = "value") |
| 72 | + long min() default 0; |
| 73 | + |
| 74 | + @OverridesAttribute(constraint = Max.class, name = "value") |
| 75 | + long max() default Long.MAX_VALUE; |
| 76 | + |
| 77 | + String message() default "Out of range"; |
| 78 | + Class<?>[] groups() default {}; |
| 79 | + Class<? extends Payload>[] payload() default {}; |
| 80 | + } |
| 81 | + |
| 82 | + @ValidStoreId |
| 83 | + @Target({ElementType.FIELD, ElementType.PARAMETER}) |
| 84 | + @Retention(RetentionPolicy.RUNTIME) |
| 85 | + @Constraint(validatedBy = {}) |
| 86 | + public @interface ValidStoreIdNested { |
| 87 | + String message() default "Invalid store ID (nested)"; |
| 88 | + Class<?>[] groups() default {}; |
| 89 | + Class<? extends Payload>[] payload() default {}; |
| 90 | + } |
| 91 | + |
| 92 | + static class TestStoreDto { |
| 93 | + @Min(0) |
| 94 | + @Max(999) |
| 95 | + @NotNull |
| 96 | + private Short storeId; |
| 97 | + |
| 98 | + @ValidStoreId |
| 99 | + @NotNull |
| 100 | + private Short metaStoreId; |
| 101 | + |
| 102 | + @ValidName |
| 103 | + private String name; |
| 104 | + |
| 105 | + @ValidEmail |
| 106 | + private String email; |
| 107 | + |
| 108 | + @ValidRange(min = 5, max = 50) |
| 109 | + private Short rangeField; |
| 110 | + |
| 111 | + @ValidStoreIdNested |
| 112 | + private Short nestedStoreId; |
| 113 | + |
| 114 | + @ValidStoreId |
| 115 | + @Max(100) |
| 116 | + private Short priorityStoreId; |
| 117 | + |
| 118 | + public Short getStoreId() { return storeId; } |
| 119 | + public void setStoreId(Short storeId) { this.storeId = storeId; } |
| 120 | + public Short getMetaStoreId() { return metaStoreId; } |
| 121 | + public void setMetaStoreId(Short metaStoreId) { this.metaStoreId = metaStoreId; } |
| 122 | + public String getName() { return name; } |
| 123 | + public void setName(String name) { this.name = name; } |
| 124 | + public String getEmail() { return email; } |
| 125 | + public void setEmail(String email) { this.email = email; } |
| 126 | + public Short getRangeField() { return rangeField; } |
| 127 | + public void setRangeField(Short rangeField) { this.rangeField = rangeField; } |
| 128 | + public Short getNestedStoreId() { return nestedStoreId; } |
| 129 | + public void setNestedStoreId(Short nestedStoreId) { this.nestedStoreId = nestedStoreId; } |
| 130 | + public Short getPriorityStoreId() { return priorityStoreId; } |
| 131 | + public void setPriorityStoreId(Short priorityStoreId) { this.priorityStoreId = priorityStoreId; } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void readsComposedMinMaxConstraintOnDtoField() { |
| 136 | + Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestStoreDto.class); |
| 137 | + Schema model = schemas.get("TestStoreDto"); |
| 138 | + assertNotNull(model, "Model should be resolved"); |
| 139 | + Schema meta = (Schema) model.getProperties().get("metaStoreId"); |
| 140 | + assertNotNull(meta, "metaStoreId property should exist"); |
| 141 | + assertEquals(((IntegerSchema) meta).getMinimum().intValue(), 0); |
| 142 | + assertEquals(((IntegerSchema) meta).getMaximum().intValue(), 999); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void dtoFieldParityWithDirectAnnotations() { |
| 147 | + Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestStoreDto.class); |
| 148 | + Schema model = schemas.get("TestStoreDto"); |
| 149 | + Schema direct = (Schema) model.getProperties().get("storeId"); |
| 150 | + Schema meta = (Schema) model.getProperties().get("metaStoreId"); |
| 151 | + assertEquals(meta.getMinimum(), direct.getMinimum(), "minimum should match direct annotation"); |
| 152 | + assertEquals(meta.getMaximum(), direct.getMaximum(), "maximum should match direct annotation"); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void readsComposedSizeConstraintOnDtoField() { |
| 157 | + Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestStoreDto.class); |
| 158 | + Schema model = schemas.get("TestStoreDto"); |
| 159 | + Schema name = (Schema) model.getProperties().get("name"); |
| 160 | + assertNotNull(name, "name property should exist"); |
| 161 | + assertEquals((int) name.getMinLength(), 1); |
| 162 | + assertEquals((int) name.getMaxLength(), 50); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void readsComposedPatternConstraintOnDtoField() { |
| 167 | + Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestStoreDto.class); |
| 168 | + Schema model = schemas.get("TestStoreDto"); |
| 169 | + Schema email = (Schema) model.getProperties().get("email"); |
| 170 | + assertNotNull(email, "email property should exist"); |
| 171 | + assertNotNull(email.getPattern(), "pattern should be set"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void readsDeepNestedComposedConstraintOnDtoField() { |
| 176 | + Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestStoreDto.class); |
| 177 | + Schema model = schemas.get("TestStoreDto"); |
| 178 | + Schema nested = (Schema) model.getProperties().get("nestedStoreId"); |
| 179 | + assertNotNull(nested, "nestedStoreId property should exist"); |
| 180 | + assertEquals(((IntegerSchema) nested).getMinimum().intValue(), 0); |
| 181 | + assertEquals(((IntegerSchema) nested).getMaximum().intValue(), 999); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void directAnnotationTakesPriorityOverMetaAnnotation() { |
| 186 | + Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestStoreDto.class); |
| 187 | + Schema model = schemas.get("TestStoreDto"); |
| 188 | + Schema priority = (Schema) model.getProperties().get("priorityStoreId"); |
| 189 | + assertNotNull(priority, "priorityStoreId property should exist"); |
| 190 | + assertEquals(((IntegerSchema) priority).getMinimum().intValue(), 0, "min from @ValidStoreId meta should be set"); |
| 191 | + assertEquals(((IntegerSchema) priority).getMaximum().intValue(), 100, "direct @Max(100) should win over @ValidStoreId's @Max(999)"); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Documents a known limitation: for @Range-style constraints that rely on @OverridesAttribute |
| 196 | + * to propagate per-use values (e.g. @ValidRange(min=5, max=50)) into their meta-annotations |
| 197 | + * (@Min/@Max), our implementation reads constraints from the annotation *type definition* |
| 198 | + * and therefore always sees the default values (min=0, max=Long.MAX_VALUE), not the |
| 199 | + * caller-supplied ones. Handling @OverridesAttribute is not yet supported. |
| 200 | + */ |
| 201 | + @Test |
| 202 | + public void rangeStyleConstraintUsesDefaultsNotOverriddenValues() { |
| 203 | + Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestStoreDto.class); |
| 204 | + Schema model = schemas.get("TestStoreDto"); |
| 205 | + Schema range = (Schema) model.getProperties().get("rangeField"); |
| 206 | + assertNotNull(range, "rangeField property should exist"); |
| 207 | + // We pick up the *default* values from @Min(0) and @Max(Long.MAX_VALUE) on the type |
| 208 | + // definition of @ValidRange, NOT the caller-supplied @ValidRange(min=5, max=50). |
| 209 | + assertEquals(range.getMinimum().longValue(), 0L, |
| 210 | + "expected default @Min(0) from type definition, not overridden min=5"); |
| 211 | + assertEquals(range.getMaximum().longValue(), Long.MAX_VALUE, |
| 212 | + "expected default @Max(Long.MAX_VALUE) from type definition, not overridden max=50"); |
| 213 | + } |
| 214 | +} |
0 commit comments