diff --git a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java index 7b93e4fb3b2..3c3b3eea2d3 100644 --- a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java +++ b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractJsonSchema.java @@ -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); + } + }); + } }); } diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/validation/ValidationSpec.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/validation/ValidationSpec.java index 27b78e0a486..282abdaee43 100644 --- a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/validation/ValidationSpec.java +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/validation/ValidationSpec.java @@ -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 mapItemMinLength1MaxLength3; } @Data diff --git a/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.validations.samples.fabric8.io.v1.approved.yml b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.validations.samples.fabric8.io.v1.approved.yml index 7e904430986..1a3d663d296 100644 --- a/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.validations.samples.fabric8.io.v1.approved.yml +++ b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.validations.samples.fabric8.io.v1.approved.yml @@ -384,16 +384,20 @@ 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 @@ -401,11 +405,19 @@ spec: 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)+"