Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set regex pattern on array #4151

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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