|
2 | 2 |
|
3 | 3 | import io.swagger.v3.core.converter.AnnotatedType;
|
4 | 4 | import io.swagger.v3.core.converter.ModelConverterContextImpl;
|
| 5 | +import io.swagger.v3.core.converter.ModelConverters; |
5 | 6 | import io.swagger.v3.core.jackson.ModelResolver;
|
6 | 7 | import io.swagger.v3.core.matchers.SerializationMatchers;
|
7 | 8 | import io.swagger.v3.core.resolving.SwaggerTestBase;
|
|
10 | 11 | import io.swagger.v3.core.resolving.v31.model.AnnotatedArray;
|
11 | 12 | import io.swagger.v3.core.resolving.v31.model.ModelWithDependentSchema;
|
12 | 13 | import io.swagger.v3.core.resolving.v31.model.ModelWithOAS31Stuff;
|
13 |
| -import io.swagger.v3.core.resolving.v31.model.ModelWithOAS31StuffMinimal; |
14 |
| -import io.swagger.v3.core.util.Yaml31; |
15 | 14 | import io.swagger.v3.oas.models.media.Schema;
|
16 | 15 | import org.testng.annotations.Test;
|
| 16 | +import javax.validation.constraints.DecimalMax; |
| 17 | +import javax.validation.constraints.DecimalMin; |
| 18 | +import javax.validation.constraints.Pattern; |
| 19 | +import javax.validation.constraints.Size; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
17 | 22 |
|
18 | 23 | public class ModelResolverOAS31Test extends SwaggerTestBase {
|
19 | 24 |
|
@@ -231,4 +236,115 @@ public void testFieldArraySchemaAnnotation() {
|
231 | 236 | " type: string\n" +
|
232 | 237 | " maxItems: 10");
|
233 | 238 | }
|
| 239 | + |
| 240 | + @Test(description = "@Pattern correctly handled in type parameters of properties using collections when using oas 3.1.0") |
| 241 | + public void testModelUsingCollectionTypePropertyHandlesPatternAnnotationForOas31() { |
| 242 | + String expectedYaml = "ClassWithUsingPatternOnCollection:\n" + |
| 243 | + " type: object\n" + |
| 244 | + " properties:\n" + |
| 245 | + " myField:\n" + |
| 246 | + " type: array\n" + |
| 247 | + " items:\n" + |
| 248 | + " pattern: myPattern\n" + |
| 249 | + " type: string"; |
| 250 | + |
| 251 | + Map<String, Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(ClassWithUsingPatternOnCollection.class); |
| 252 | + SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml); |
| 253 | + } |
| 254 | + |
| 255 | + private static class ClassWithUsingPatternOnCollection { |
| 256 | + private List<@Pattern(regexp = "myPattern") String> myField; |
| 257 | + |
| 258 | + public List<String> getMyField() { |
| 259 | + return myField; |
| 260 | + } |
| 261 | + |
| 262 | + public void setMyField(List<String> myField) { |
| 263 | + this.myField = myField; |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + @Test(description = "@Size correctly handled in properties using collections when using oas 3.1.0") |
| 268 | + public void testModelUsingCollectionTypePropertyHandleSizeAnnotationForOas31() { |
| 269 | + String expectedYaml = "ClassWithUsingSizeOnCollection:\n" + |
| 270 | + " type: object\n" + |
| 271 | + " properties:\n" + |
| 272 | + " myField:\n" + |
| 273 | + " maxItems: 100\n" + |
| 274 | + " minItems: 1\n" + |
| 275 | + " type: array\n" + |
| 276 | + " items:\n" + |
| 277 | + " type: string"; |
| 278 | + |
| 279 | + Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(ClassWithUsingSizeOnCollection.class); |
| 280 | + SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml); |
| 281 | + } |
| 282 | + |
| 283 | + private static class ClassWithUsingSizeOnCollection { |
| 284 | + @Size(min = 1, max = 100) |
| 285 | + private List<String> myField; |
| 286 | + |
| 287 | + public List<String> getMyField() { |
| 288 | + return myField; |
| 289 | + } |
| 290 | + |
| 291 | + public void setMyField(List<String> myField) { |
| 292 | + this.myField = myField; |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + @Test(description = "@Size correctly handled for field type String using OAS 3.1.0") |
| 297 | + public void testSizeAnnotationOnFieldForOAS31() { |
| 298 | + String expectedYaml = "ClassWithUsingSizeOnField:\n" + |
| 299 | + " type: object\n" + |
| 300 | + " properties:\n" + |
| 301 | + " myField:\n" + |
| 302 | + " type: string\n" + |
| 303 | + " maxLength: 100\n" + |
| 304 | + " minLength: 1"; |
| 305 | + |
| 306 | + Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(ClassWithUsingSizeOnField.class); |
| 307 | + SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml); |
| 308 | + } |
| 309 | + |
| 310 | + private static class ClassWithUsingSizeOnField { |
| 311 | + @Size(min = 1, max = 100) |
| 312 | + private String myField; |
| 313 | + |
| 314 | + public String getMyField() { |
| 315 | + return myField; |
| 316 | + } |
| 317 | + |
| 318 | + public void setMyField(String myField) { |
| 319 | + this.myField = myField; |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + @Test(description = "@DecimalMax/Min annotations correctly handled for field type Number using OAS 3.1.0") |
| 324 | + public void testDecimalAnnotationsOnField() { |
| 325 | + String expectedYaml = "ClassWithUsingDecimalAnnotationsOnField:\n" + |
| 326 | + " type: object\n" + |
| 327 | + " properties:\n" + |
| 328 | + " myField:\n" + |
| 329 | + " type: number\n" + |
| 330 | + " maximum: 100\n" + |
| 331 | + " minimum: 1"; |
| 332 | + |
| 333 | + Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(ClassWithUsingDecimalAnnotationsOnField.class); |
| 334 | + SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml); |
| 335 | + } |
| 336 | + |
| 337 | + private static class ClassWithUsingDecimalAnnotationsOnField { |
| 338 | + @DecimalMin("1") |
| 339 | + @DecimalMax("100") |
| 340 | + private Number myField; |
| 341 | + |
| 342 | + public Number getMyField() { |
| 343 | + return myField; |
| 344 | + } |
| 345 | + |
| 346 | + public void setMyField(Number myField) { |
| 347 | + this.myField = myField; |
| 348 | + } |
| 349 | + } |
234 | 350 | }
|
0 commit comments