Skip to content

Add support for jakarta.validation.constraints.Email #4876

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -64,6 +64,7 @@
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.UUIDSchema;
import io.swagger.v3.oas.models.media.XML;
import javax.validation.constraints.Email;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -1851,6 +1852,20 @@ protected boolean applyBeanValidatorAnnotations(Schema property, Annotation[] an
}
}
}
if (annos.containsKey("javax.validation.constraints.Email")) {
Email email = (Email) annos.get("javax.validation.constraints.Email");
boolean apply = checkGroupValidation(email.groups(), invocationGroups, acceptNoGroups);
if (apply) {
if (isStringSchema(property)) {
property.setFormat("email");
modified = true;
}
if (property.getItems() != null && isStringSchema(property.getItems())) {
property.getItems().setFormat("email");
modified = true;
}
}
}
if (validatorProcessor != null && validatorProcessor.getMode().equals(ValidatorProcessor.MODE.AFTER)) {
modified = validatorProcessor.applyBeanValidatorAnnotations(property, annotations, parent, applyNotNullAnnotations) || modified;
}
Expand Down Expand Up @@ -1961,6 +1976,16 @@ protected boolean applyBeanValidatorAnnotationsNoGroups(Schema property, Annotat
modified = true;
}
}
if (annos.containsKey("javax.validation.constraints.Email")) {
if (isStringSchema(property)) {
property.setFormat("email");
modified = true;
}
if (property.getItems() != null && isStringSchema(property.getItems())) {
property.getItems().setFormat("email");
modified = true;
}
}
return modified;
}

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

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
Expand All @@ -18,14 +19,15 @@ public class BeanValidationsModel {
@Max(99)
protected Integer age;

@Pattern(regexp = "(?![-._])[-._a-zA-Z0-9]{3,32}")
protected String username;

@Size(min = 6, max = 20)
protected String password;

protected String passwordHint;

@Pattern(regexp = "(.+?)@(.+?)")
@Email
protected String email;

@DecimalMin(value = "0.1", inclusive = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.oas.models.BeanValidationsModel;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.EmailSchema;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.NumberSchema;
import io.swagger.v3.oas.models.media.Schema;
Expand All @@ -23,6 +24,9 @@ public void readBeanValidatorTest() {

Assert.assertTrue(model.getRequired().contains("id"));

final StringSchema username = (StringSchema) properties.get("username");
Assert.assertEquals((String) username.getPattern(), "(?![-._])[-._a-zA-Z0-9]{3,32}");

final IntegerSchema age = (IntegerSchema) properties.get("age");
Assert.assertEquals(age.getMinimum(), new BigDecimal(13.0));
Assert.assertEquals(age.getMaximum(), new BigDecimal(99.0));
Expand All @@ -31,8 +35,8 @@ public void readBeanValidatorTest() {
Assert.assertEquals((int) password.getMinLength(), 6);
Assert.assertEquals((int) password.getMaxLength(), 20);

final StringSchema email = (StringSchema) properties.get("email");
Assert.assertEquals((String) email.getPattern(), "(.+?)@(.+?)");
final EmailSchema email = (EmailSchema) properties.get("email");
Assert.assertEquals((String) email.getFormat(), "email");

final NumberSchema minBalance = (NumberSchema) properties.get("minBalance");
Assert.assertTrue(minBalance.getExclusiveMinimum());
Expand Down