Skip to content

Commit

Permalink
Set regex pattern on array
Browse files Browse the repository at this point in the history
  • Loading branch information
gcatanese authored and frantuma committed Mar 28, 2022
1 parent 636cff7 commit f17b48c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1340,9 +1340,15 @@ 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(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 @@ -8,6 +8,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import org.testng.annotations.Test;

import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Date;
Expand Down Expand Up @@ -645,6 +646,35 @@ public void setIds(String[] ids) {
}
}

@Test(description = "Shows how to provide an array with specific format")
public void testArrayWithPattern() {

String yaml =
"modelArrayWithPattern:\n" +
" type: object\n" +
" properties:\n" +
" ids:\n" +
" type: array\n" +
" items:\n" +
" pattern: \"[a-zA-Z]*\"\n" +
" type: string";
Map<String, io.swagger.v3.oas.models.media.Schema> schemaMap = readAll(modelArrayWithPattern.class);
SerializationMatchers.assertEqualsToYaml(schemaMap, yaml);
}

static class modelArrayWithPattern {
@Pattern(regexp="[a-zA-Z]*")
private String[] ids;

public String[] getIds() {
return ids;
}

public void setIds(String[] ids) {
this.ids = ids;
}
}

@Test(description = "Show how to completely override an object example")
public void testModelExampleOverride() {

Expand Down

0 comments on commit f17b48c

Please sign in to comment.