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

added support for generic type bean validation annotations #4373

Merged
merged 4 commits into from
Mar 6, 2023
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 @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
Expand Down Expand Up @@ -77,6 +78,7 @@
import javax.xml.bind.annotation.XmlSchema;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -94,6 +96,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -703,7 +706,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
addRequiredItem(model, property.getName());
}
final boolean applyNotNullAnnotations = io.swagger.v3.oas.annotations.media.Schema.RequiredMode.AUTO.equals(requiredMode);
applyBeanValidatorAnnotations(property, annotations, model, applyNotNullAnnotations);
annotations = addGenericTypeArgumentAnnotationsForOptionalField(propDef, annotations);
applyBeanValidatorAnnotations(propDef, property, annotations, model, applyNotNullAnnotations);

props.add(property);
}
Expand Down Expand Up @@ -905,6 +909,40 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
return model;
}

private Annotation[] addGenericTypeArgumentAnnotationsForOptionalField(BeanPropertyDefinition propDef, Annotation[] annotations) {

boolean isNotOptionalType = Optional.ofNullable(propDef)
.map(BeanPropertyDefinition::getField)
.map(AnnotatedField::getAnnotated)
.map(field -> !(field.getType().equals(Optional.class)))
.orElse(false);

if (isNotOptionalType) {
return annotations;
}

Stream<Annotation> genericTypeArgumentAnnotations = extractGenericTypeArgumentAnnotations(propDef);
return Stream.concat(Stream.of(annotations), genericTypeArgumentAnnotations).toArray(Annotation[]::new);
}

private Stream<Annotation> extractGenericTypeArgumentAnnotations(BeanPropertyDefinition propDef) {
return Optional.ofNullable(propDef)
.map(BeanPropertyDefinition::getField)
.map(AnnotatedField::getAnnotated)
.map(this::getGenericTypeArgumentAnnotations)
.orElseGet(Stream::of);
}

private Stream<Annotation> getGenericTypeArgumentAnnotations(Field field) {
return Optional.of(field.getAnnotatedType())
.filter(annotatedType -> annotatedType instanceof AnnotatedParameterizedType)
.map(annotatedType -> (AnnotatedParameterizedType) annotatedType)
.map(AnnotatedParameterizedType::getAnnotatedActualTypeArguments)
.map(types -> Stream.of(types)
.flatMap(type -> Stream.of(type.getAnnotations())))
.orElseGet(Stream::of);
}

private boolean shouldResolveEnumAsRef(io.swagger.v3.oas.annotations.media.Schema resolvedSchemaAnnotation) {
return (resolvedSchemaAnnotation != null && resolvedSchemaAnnotation.enumAsRef()) || ModelResolver.enumsAsRef;
}
Expand Down Expand Up @@ -1298,6 +1336,15 @@ private AnnotatedType removeJsonIdentityAnnotations(AnnotatedType type) {
}
}

protected void applyBeanValidatorAnnotations(BeanPropertyDefinition propDef, Schema property, Annotation[] annotations, Schema parent, boolean applyNotNullAnnotations) {
applyBeanValidatorAnnotations(property, annotations, parent, applyNotNullAnnotations);

if (Objects.nonNull(property.getItems())) {
Annotation[] genericTypeArgumentAnnotations = extractGenericTypeArgumentAnnotations(propDef).toArray(Annotation[]::new);
applyBeanValidatorAnnotations(property.getItems(), genericTypeArgumentAnnotations, property, applyNotNullAnnotations);
}
}

protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annotations, Schema parent, boolean applyNotNullAnnotations) {
Map<String, Annotation> annos = new HashMap<>();
if (annotations != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Optional;

public class BeanValidationsModel {
@NotNull
Expand Down Expand Up @@ -36,7 +37,9 @@ public class BeanValidationsModel {
protected Integer birthYear;

@Size(min = 2, max = 10)
private List<String> items;
private List<@Size(min = 3, max = 4) String> items;

private Optional<@Size(min= 1, max=10) String> optionalValue;

public Long getId() {
return id;
Expand Down Expand Up @@ -117,4 +120,13 @@ public List<String> getItems() {
public void setItems(List<String> items) {
this.items = items;
}

public Optional<String> getOptionalValue() {
return optionalValue;
}

public void setOptionalValue(Optional<String> optionalValue) {
this.optionalValue = optionalValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ public void readBeanValidatorTest() {
final ArraySchema items = (ArraySchema) properties.get("items");
Assert.assertEquals((int) items.getMinItems(), 2);
Assert.assertEquals((int) items.getMaxItems(), 10);
Assert.assertEquals((int) items.getItems().getMinLength(), 3);
Assert.assertEquals((int) items.getItems().getMaxLength(), 4);

final StringSchema optionalValue = (StringSchema) properties.get("optionalValue");
Assert.assertEquals((int) optionalValue.getMinLength(), 1);
Assert.assertEquals((int) optionalValue.getMaxLength(), 10);
}
}