Skip to content

Commit

Permalink
refs-#4703/#4702-@Pattern/@SiZe annotations handling on collections
Browse files Browse the repository at this point in the history
  • Loading branch information
micryc authored and frantuma committed Sep 16, 2024
1 parent 290c78d commit ca9a21e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,11 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
ArraySchema sp = (ArraySchema) property;
sp.setMinItems(size.min());
sp.setMaxItems(size.max());
}else if(openapi31 && property instanceof JsonSchema){
JsonSchema sp = (JsonSchema) property;
sp.setMinItems(size.min());
sp.setMaxItems(size.max());

}
}
if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
Expand All @@ -1540,13 +1545,22 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
if (annos.containsKey("javax.validation.constraints.Pattern")) {
Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");

if (property instanceof StringSchema) {
property.setPattern(pattern.regexp());
if(openapi31){
if (property instanceof JsonSchema) {
property.setPattern(pattern.regexp());
}
if(property.getItems() != null && property.getItems() instanceof JsonSchema) {
property.getItems().setPattern(pattern.regexp());
}
}else {
if (property instanceof StringSchema) {
property.setPattern(pattern.regexp());
}
if(property.getItems() != null && property.getItems() instanceof StringSchema) {
property.getItems().setPattern(pattern.regexp());
}
}

if(property.getItems() != null && property.getItems() instanceof StringSchema) {
property.getItems().setPattern(pattern.regexp());
}

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverterContextImpl;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.resolving.SwaggerTestBase;
Expand All @@ -15,6 +16,11 @@
import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.Test;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;

public class ModelResolverOAS31Test extends SwaggerTestBase {

@Test
Expand Down Expand Up @@ -231,4 +237,62 @@ public void testFieldArraySchemaAnnotation() {
" type: string\n" +
" maxItems: 10");
}

@Test(description = "@Pattern 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, Schema> stringSchemaMap = ModelConverters.getInstance(false).readAll(ClassWithUsingPatternOnCollection.class);
SerializationMatchers.assertEqualsToYaml(stringSchemaMap, expectedYaml);
}

public class ClassWithUsingPatternOnCollection {
private List<@Pattern(regexp = "myPattern") String> myField;

public List<String> getMyField() {
return myField;
}

public void setMyField(List<String> myField) {
this.myField = myField;
}
}

@Test(description = "@Size correctly handled in properties using collections when using oas 3.1.0")
public void testModelUsingCollectionTypePropertyDoesNotHandleSizeAnnotationForOas31() {

String expectedYaml = "DtoUsingSizeOnCollection:\n" +
" type: object\n" +
" properties:\n" +
" myField:\n" +
" maxItems: 100\n" +
" minItems: 1\n" +
" type: array\n" +
" items:\n" +
" type: string";

Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(ClassWithUsingSizeOnCollection.class);
SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml);
}

public class ClassWithUsingSizeOnCollection {
@Size(min = 1, max = 100)
private List<String> myField;

public List<String> getMyField() {
return myField;
}

public void setMyField(List<String> myField) {
this.myField = myField;
}
}
}

0 comments on commit ca9a21e

Please sign in to comment.