-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Problem
When generating OpenAPI 3.1.0 spec the @Pattern annotation on a type parameter of properties with a collection type are ignored. The expected behaviour is that the generated property has the pattern property set for the items schema but no pattern is found in the generated open api.
This issue is similar to #4702
Expected behaviour
@Pattern#regexp is used to generate pattern property for the items schema of the property
Reproducer
The issues is reproduced in the following test:
@Test(description = "Shows that @Pattern is not correctly handled in type parameters of properties using collections when using oas 3.1.0")
public void testModelUsingCollectionTypePropertyDoesNotHandlePatternAnnotationForOas31() {
String expectedYaml = "DtoUsingPatternOnCollection:\n" +
" type: object\n" +
" properties:\n" +
" myField:\n" +
" type: array\n" +
" items:\n" +
" pattern: myPattern\n" +
" type: string";
Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(DtoUsingPatternOnCollection.class);
// fails as pattern will not be added to the items property of myField for oas 3.1.0
SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml);
}
public class DtoUsingPatternOnCollection {
private List<@Pattern(regexp = "myPattern") String> myField;
public List<String> getMyField() {
return myField;
}
public void setMyField(List<String> myField) {
this.myField = myField;
}
}
Investigation with proposed solution
Looking at the ModelResolver.applyBeanValidatorAnnotations code (which is where information from bean validation annotations are processed), the @Pattern annotation is processed only if the property input parameter of type Schema is an instance of StringSchema or if property.getItems() is an instance of StringSchema. If the check succeeds then the information from @Pattern is propagated to the generated schema (using the pattern property). Nevertheless for open api 3.1.0 property.getItems() will be of type JsonSchema instead of StringSchema and therefore the information from @Pattern will not be used. A possible solution would be that instead of using doing an instanceOf check against StringSchema a check is performed on the type/types fields to ensure that the property is a string schema.