Skip to content

Commit

Permalink
Add type support for strings Lists and Maps using @Size
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo42 committed Oct 22, 2024
1 parent 215f5c1 commit 9a6965d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,34 +591,49 @@ private void handleTypeAnnotations(final T schema, BeanProperty beanProperty, Cl

AnnotatedElement member = beanProperty.getMember().getAnnotated();
AnnotatedType fieldType = null;
AnnotatedType type = null;
AnnotatedType methodType = null;
if (member instanceof Field) {
fieldType = ((Field) member).getAnnotatedType();
} else if (member instanceof Method) {
fieldType = getFieldForMethod(beanProperty).map(Field::getAnnotatedType).orElse(null);
type = ((Method) member).getAnnotatedReceiverType();
methodType = ((Method) member).getAnnotatedReceiverType();
}

Stream.of(fieldType, type)
Stream.of(fieldType, methodType)
.filter(o -> !Objects.isNull(o))
.filter(AnnotatedParameterizedType.class::isInstance)
.map(AnnotatedParameterizedType.class::cast)
.map(AnnotatedParameterizedType::getAnnotatedActualTypeArguments)
.map(a -> a[typeIndex])
.forEach(at -> {
Optional.ofNullable(at.getAnnotation(Pattern.class)).ifPresent(a -> schema.setPattern(a.value()));
Optional.ofNullable(at.getAnnotation(Min.class)).ifPresent(a -> {
schema.setMinimum(a.value());
if (!a.inclusive()) {
schema.setExclusiveMinimum(true);
}
});
Optional.ofNullable(at.getAnnotation(Max.class)).ifPresent(a -> {
schema.setMaximum(a.value());
if (!a.inclusive()) {
schema.setExclusiveMaximum(true);
}
});
if ("string".equals(schema.getType())) {
ofNullable(at.getAnnotation(Pattern.class))
.ifPresent(a -> schema.setPattern(a.value()));

ofNullable(at.getAnnotation(Size.class))
.map(Size::min)
.filter(v -> v > 0)
.ifPresent(schema::setMinLength);

ofNullable(at.getAnnotation(Size.class))
.map(Size::max)
.filter(v -> v < Long.MAX_VALUE)
.ifPresent(schema::setMaxLength);

} else if ("number".equals(schema.getType()) || "integer".equals(schema.getType())) {
ofNullable(at.getAnnotation(Min.class)).ifPresent(a -> {
schema.setMinimum(a.value());
if (!a.inclusive()) {
schema.setExclusiveMinimum(true);
}
});
ofNullable(at.getAnnotation(Max.class)).ifPresent(a -> {
schema.setMaximum(a.value());
if (!a.inclusive()) {
schema.setExclusiveMaximum(true);
}
});
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ static class ValidationOnString {
private String maxLength1;
@Size(min = 1, max = 3)
private String minLength1maxLength3;

private List<@Size(min = 1, max = 3) String> listItemMinLength1MaxLength3;
private Map<String, @Size(min = 1, max = 3) String> mapItemMinLength1MaxLength3;
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,28 +384,40 @@ spec:
properties:
maxItems1:
additionalProperties:
maxLength: 1
type: "string"
maxProperties: 1
type: "object"
minItems1:
additionalProperties:
minLength: 1
type: "string"
minProperties: 1
type: "object"
minItems1maxItems3:
additionalProperties:
maxLength: 3
minLength: 1
type: "string"
maxProperties: 3
minProperties: 1
type: "object"
type: "object"
onString:
properties:
listItemMinLength1MaxLength3:
items:
type: "string"
type: "array"
listItemPattern:
items:
pattern: "(a|b)+"
type: "string"
type: "array"
mapItemMinLength1MaxLength3:
additionalProperties:
type: "string"
type: "object"
mapItemPattern:
additionalProperties:
pattern: "(a|b)+"
Expand Down

0 comments on commit 9a6965d

Please sign in to comment.