Skip to content

Commit 1435d2c

Browse files
Add a util class for applying bean validation annotations to a schema
1 parent f2fb590 commit 1435d2c

2 files changed

Lines changed: 236 additions & 166 deletions

File tree

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 56 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107

108108
import static io.swagger.v3.core.jackson.JAXBAnnotationsHelper.JAXB_DEFAULT;
109109
import static io.swagger.v3.core.util.RefUtils.constructRef;
110+
import static io.swagger.v3.core.util.ValidationAnnotationsUtils.*;
110111

111112
public class ModelResolver extends AbstractModelConverter implements ModelConverter {
112113

@@ -1817,8 +1818,8 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
18171818
}
18181819
boolean acceptNoGroups = !strategy.equals(Configuration.GroupsValidationStrategy.NEVER_IF_NO_CONTEXT);
18191820
// if we get here, validate only if groups match.
1820-
if (parent != null && annos.containsKey("javax.validation.constraints.NotNull") && applyNotNullAnnotations) {
1821-
NotNull anno = (NotNull) annos.get("javax.validation.constraints.NotNull");
1821+
if (parent != null && annos.containsKey(JAVAX_NOT_NULL) && applyNotNullAnnotations) {
1822+
NotNull anno = (NotNull) annos.get(JAVAX_NOT_NULL);
18221823
if (anno.groups().length == 0 && acceptNoGroups) {
18231824
;
18241825
// no groups, so apply
@@ -1833,131 +1834,77 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
18331834
}
18341835
}
18351836

1836-
if (annos.containsKey("javax.validation.constraints.NotEmpty")) {
1837-
NotEmpty anno = (NotEmpty) annos.get("javax.validation.constraints.NotEmpty");
1837+
if (annos.containsKey(JAVAX_NOT_EMPTY)) {
1838+
NotEmpty anno = (NotEmpty) annos.get(JAVAX_NOT_EMPTY);
18381839
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
18391840
if (apply) {
18401841
io.swagger.v3.oas.annotations.media.Schema ctxSchema = AnnotationsUtils.getSchemaAnnotation(annotations);
18411842
io.swagger.v3.oas.annotations.media.ArraySchema ctxArraySchema = AnnotationsUtils.getArraySchemaAnnotation(annotations);
1842-
if (isArraySchema(property)) {
1843-
if (ctxArraySchema == null || ctxArraySchema.minItems() == Integer.MAX_VALUE){
1844-
property.setMinItems(1);
1845-
modified = true;
1846-
}
1847-
} else if (isStringSchema(property)) {
1848-
if (ctxSchema == null || ctxSchema.minLength() == 0) {
1849-
property.setMinLength(1);
1850-
modified = true;
1851-
}
1852-
} else if (isObjectSchema(property)) {
1853-
if (ctxSchema == null || ctxSchema.minProperties() == 0) {
1854-
property.setMinProperties(1);
1855-
modified = true;
1856-
}
1857-
}
1843+
modified = ValidationAnnotationsUtils.applyNotEmptyConstraint(property, ctxSchema, ctxArraySchema) || modified;
18581844
if (applyNotNullAnnotations) {
18591845
modified = updateRequiredItem(parent, property.getName()) || modified;
18601846
}
18611847
}
18621848
}
18631849

1864-
if (annos.containsKey("javax.validation.constraints.NotBlank")) {
1865-
NotBlank anno = (NotBlank) annos.get("javax.validation.constraints.NotBlank");
1850+
if (annos.containsKey(JAVAX_NOT_BLANK)) {
1851+
NotBlank anno = (NotBlank) annos.get(JAVAX_NOT_BLANK);
18661852
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
18671853
if (apply) {
1868-
if (isStringSchema(property)) {
1869-
io.swagger.v3.oas.annotations.media.Schema ctxSchema = AnnotationsUtils.getSchemaAnnotation(annotations);
1870-
if (ctxSchema == null || ctxSchema.minLength() == 0) {
1871-
property.setMinLength(1);
1872-
modified = true;
1873-
}
1874-
}
1854+
io.swagger.v3.oas.annotations.media.Schema ctxSchema = AnnotationsUtils.getSchemaAnnotation(annotations);
1855+
modified = ValidationAnnotationsUtils.applyNotBlankConstraint(property, ctxSchema) || modified;
18751856
if (applyNotNullAnnotations) {
18761857
modified = updateRequiredItem(parent, property.getName()) || modified;
18771858
}
18781859
}
18791860
}
1880-
if (annos.containsKey("javax.validation.constraints.Min")) {
1881-
Min anno = (Min) annos.get("javax.validation.constraints.Min");
1861+
if (annos.containsKey(JAVAX_MIN)) {
1862+
Min anno = (Min) annos.get(JAVAX_MIN);
18821863
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
1883-
if (apply && isNumberSchema(property)) {
1884-
property.setMinimum(new BigDecimal(anno.value()));
1885-
modified = true;
1864+
if (apply) {
1865+
modified = ValidationAnnotationsUtils.applyMinConstraint(property, anno) || modified;
18861866
}
18871867
}
1888-
if (annos.containsKey("javax.validation.constraints.Max")) {
1889-
Max anno = (Max) annos.get("javax.validation.constraints.Max");
1868+
if (annos.containsKey(JAVAX_MAX)) {
1869+
Max anno = (Max) annos.get(JAVAX_MAX);
18901870
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
1891-
if (apply && isNumberSchema(property)) {
1892-
property.setMaximum(new BigDecimal(anno.value()));
1893-
modified = true;
1871+
if (apply) {
1872+
modified = ValidationAnnotationsUtils.applyMaxConstraint(property, anno) || modified;
18941873
}
18951874
}
1896-
if (annos.containsKey("javax.validation.constraints.Size")) {
1897-
Size anno = (Size) annos.get("javax.validation.constraints.Size");
1875+
if (annos.containsKey(JAVAX_SIZE)) {
1876+
Size anno = (Size) annos.get(JAVAX_SIZE);
18981877
boolean apply = checkGroupValidation(anno.groups(), invocationGroups, acceptNoGroups);
18991878
if (apply) {
1900-
if (isNumberSchema(property)) {
1901-
property.setMinimum(new BigDecimal(anno.min()));
1902-
property.setMaximum(new BigDecimal(anno.max()));
1903-
modified = true;
1904-
}
1905-
if (isStringSchema(property)) {
1906-
property.setMinLength(Integer.valueOf(anno.min()));
1907-
property.setMaxLength(Integer.valueOf(anno.max()));
1908-
modified = true;
1909-
}
1910-
if (isArraySchema(property)) {
1911-
property.setMinItems(anno.min());
1912-
property.setMaxItems(anno.max());
1913-
modified = true;
1914-
}
1879+
modified = ValidationAnnotationsUtils.applySizeConstraint(property, anno) || modified;
19151880
}
19161881
}
1917-
if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
1918-
DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
1882+
if (annos.containsKey(JAVAX_DECIMAL_MIN)) {
1883+
DecimalMin min = (DecimalMin) annos.get(JAVAX_DECIMAL_MIN);
19191884
boolean apply = checkGroupValidation(min.groups(), invocationGroups, acceptNoGroups);
1920-
if (apply && isNumberSchema(property)) {
1921-
property.setMinimum(new BigDecimal(min.value()));
1922-
property.setExclusiveMinimum(!min.inclusive());
1923-
modified = true;
1885+
if (apply) {
1886+
modified = ValidationAnnotationsUtils.applyDecimalMinConstraint(property, min) || modified;
19241887
}
19251888
}
1926-
if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
1927-
DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
1889+
if (annos.containsKey(JAVAX_DECIMAL_MAX)) {
1890+
DecimalMax max = (DecimalMax) annos.get(JAVAX_DECIMAL_MAX);
19281891
boolean apply = checkGroupValidation(max.groups(), invocationGroups, acceptNoGroups);
1929-
if (apply && isNumberSchema(property)) {
1930-
property.setMaximum(new BigDecimal(max.value()));
1931-
property.setExclusiveMaximum(!max.inclusive());
1932-
modified = true;
1892+
if (apply) {
1893+
modified = ValidationAnnotationsUtils.applyDecimalMaxConstraint(property, max) || modified;
19331894
}
19341895
}
1935-
if (annos.containsKey("javax.validation.constraints.Pattern")) {
1936-
Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
1896+
if (annos.containsKey(JAVAX_PATTERN)) {
1897+
Pattern pattern = (Pattern) annos.get(JAVAX_PATTERN);
19371898
boolean apply = checkGroupValidation(pattern.groups(), invocationGroups, acceptNoGroups);
19381899
if (apply) {
1939-
if (isStringSchema(property)) {
1940-
property.setPattern(pattern.regexp());
1941-
modified = true;
1942-
}
1943-
if (property.getItems() != null && isStringSchema(property.getItems())) {
1944-
property.getItems().setPattern(pattern.regexp());
1945-
modified = true;
1946-
}
1900+
modified = ValidationAnnotationsUtils.applyPatternConstraint(property, pattern) || modified;
19471901
}
19481902
}
1949-
if (annos.containsKey("javax.validation.constraints.Email")) {
1950-
Email email = (Email) annos.get("javax.validation.constraints.Email");
1903+
if (annos.containsKey(JAVAX_EMAIL)) {
1904+
Email email = (Email) annos.get(JAVAX_EMAIL);
19511905
boolean apply = checkGroupValidation(email.groups(), invocationGroups, acceptNoGroups);
19521906
if (apply) {
1953-
if (isStringSchema(property)) {
1954-
property.setFormat("email");
1955-
modified = true;
1956-
}
1957-
if (property.getItems() != null && isStringSchema(property.getItems())) {
1958-
property.getItems().setFormat("email");
1959-
modified = true;
1960-
}
1907+
modified = ValidationAnnotationsUtils.applyEmailConstraint(property, email) || modified;
19611908
}
19621909
}
19631910
if (validatorProcessor != null && validatorProcessor.getMode().equals(ValidatorProcessor.MODE.AFTER)) {
@@ -1995,90 +1942,33 @@ protected boolean applyBeanValidatorAnnotationsNoGroups(Schema property, Annotat
19951942
modified = updateRequiredItem(parent, property.getName());
19961943
}
19971944
}
1998-
if (annos.containsKey("javax.validation.constraints.Min")) {
1999-
if (isNumberSchema(property)) {
2000-
Min min = (Min) annos.get("javax.validation.constraints.Min");
2001-
property.setMinimum(new BigDecimal(min.value()));
2002-
modified = true;
2003-
}
1945+
if (annos.containsKey(JAVAX_MIN)) {
1946+
Min min = (Min) annos.get(JAVAX_MIN);
1947+
modified = ValidationAnnotationsUtils.applyMinConstraint(property, min) || modified;
20041948
}
2005-
if (annos.containsKey("javax.validation.constraints.Max")) {
2006-
if (isNumberSchema(property)) {
2007-
Max max = (Max) annos.get("javax.validation.constraints.Max");
2008-
property.setMaximum(new BigDecimal(max.value()));
2009-
modified = true;
2010-
}
1949+
if (annos.containsKey(JAVAX_MAX)) {
1950+
Max max = (Max) annos.get(JAVAX_MAX);
1951+
modified = ValidationAnnotationsUtils.applyMaxConstraint(property, max) || modified;
20111952
}
2012-
if (annos.containsKey("javax.validation.constraints.Size")) {
2013-
Size size = (Size) annos.get("javax.validation.constraints.Size");
2014-
if (isNumberSchema(property)) {
2015-
if (size.min() != 0) {
2016-
property.setMinimum(new BigDecimal(size.min()));
2017-
modified = true;
2018-
}
2019-
if (size.max() != Integer.MAX_VALUE) {
2020-
property.setMaximum(new BigDecimal(size.max()));
2021-
modified = true;
2022-
}
2023-
2024-
}
2025-
if (isStringSchema(property)) {
2026-
if (size.min() != 0) {
2027-
property.setMinLength(Integer.valueOf(size.min()));
2028-
modified = true;
2029-
}
2030-
if (size.max() != Integer.MAX_VALUE) {
2031-
property.setMaxLength(Integer.valueOf(size.max()));
2032-
modified = true;
2033-
}
2034-
}
2035-
if (isArraySchema(property)) {
2036-
if (size.min() != 0) {
2037-
property.setMinItems(size.min());
2038-
modified = true;
2039-
}
2040-
if (size.max() != Integer.MAX_VALUE) {
2041-
property.setMaxItems(size.max());
2042-
modified = true;
2043-
}
2044-
}
1953+
if (annos.containsKey(JAVAX_SIZE)) {
1954+
Size size = (Size) annos.get(JAVAX_SIZE);
1955+
modified = ValidationAnnotationsUtils.applySizeConstraint(property, size) || modified;
20451956
}
2046-
if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
2047-
DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
2048-
if (isNumberSchema(property)) {
2049-
property.setMinimum(new BigDecimal(min.value()));
2050-
property.setExclusiveMinimum(!min.inclusive());
2051-
modified = true;
2052-
}
1957+
if (annos.containsKey(JAVAX_DECIMAL_MIN)) {
1958+
DecimalMin min = (DecimalMin) annos.get(JAVAX_DECIMAL_MIN);
1959+
modified = ValidationAnnotationsUtils.applyDecimalMinConstraint(property, min) || modified;
20531960
}
2054-
if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
2055-
DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
2056-
if (isNumberSchema(property)) {
2057-
property.setMaximum(new BigDecimal(max.value()));
2058-
property.setExclusiveMaximum(!max.inclusive());
2059-
modified = true;
2060-
}
1961+
if (annos.containsKey(JAVAX_DECIMAL_MAX)) {
1962+
DecimalMax max = (DecimalMax) annos.get(JAVAX_DECIMAL_MAX);
1963+
modified = ValidationAnnotationsUtils.applyDecimalMaxConstraint(property, max) || modified;
20611964
}
2062-
if (annos.containsKey("javax.validation.constraints.Pattern")) {
2063-
Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
2064-
if (isStringSchema(property)) {
2065-
property.setPattern(pattern.regexp());
2066-
modified = true;
2067-
}
2068-
if (property.getItems() != null && isStringSchema(property.getItems())) {
2069-
property.getItems().setPattern(pattern.regexp());
2070-
modified = true;
2071-
}
1965+
if (annos.containsKey(JAVAX_PATTERN)) {
1966+
Pattern pattern = (Pattern) annos.get(JAVAX_PATTERN);
1967+
modified = ValidationAnnotationsUtils.applyPatternConstraint(property, pattern) || modified;
20721968
}
2073-
if (annos.containsKey("javax.validation.constraints.Email")) {
2074-
if (isStringSchema(property)) {
2075-
property.setFormat("email");
2076-
modified = true;
2077-
}
2078-
if (property.getItems() != null && isStringSchema(property.getItems())) {
2079-
property.getItems().setFormat("email");
2080-
modified = true;
2081-
}
1969+
if (annos.containsKey(JAVAX_EMAIL)) {
1970+
Email pattern = (Email) annos.get(JAVAX_EMAIL);
1971+
modified = ValidationAnnotationsUtils.applyEmailConstraint(property, pattern) || modified;
20821972
}
20831973
return modified;
20841974
}

0 commit comments

Comments
 (0)