Skip to content

Commit

Permalink
Add support for exclusiveMinimum and exclusiveMaximum (fabric8io#5868)
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo42 committed Nov 28, 2024
1 parent fc16600 commit 4536b0b
Show file tree
Hide file tree
Showing 8 changed files with 517 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ class PropertyMetadata {
private final String description;
private final Object defaultValue;
private Double min;
private Boolean exclusiveMinimum;
private Double max;
private Boolean exclusiveMaximum;
private String pattern;
private boolean nullable;
private String format;
Expand Down Expand Up @@ -275,8 +277,19 @@ public PropertyMetadata(JsonSchema value, BeanProperty beanProperty) {
// TODO: should probably move to a standard annotations
// see ValidationSchemaFactoryWrapper
nullable = beanProperty.getAnnotation(Nullable.class) != null;
max = ofNullable(beanProperty.getAnnotation(Max.class)).map(Max::value).orElse(max);
min = ofNullable(beanProperty.getAnnotation(Min.class)).map(Min::value).orElse(min);

ofNullable(beanProperty.getAnnotation(Max.class)).ifPresent(a -> {
max = a.value();
if (!a.inclusive()) {
exclusiveMaximum = true;
}
});
ofNullable(beanProperty.getAnnotation(Min.class)).ifPresent(a -> {
min = a.value();
if (!a.inclusive()) {
exclusiveMinimum = true;
}
});

// TODO: should the following be deprecated?
required = beanProperty.getAnnotation(Required.class) != null;
Expand All @@ -297,7 +310,9 @@ public void updateSchema(T schema) {
schema.setNullable(nullable);
}
schema.setMaximum(max);
schema.setExclusiveMaximum(exclusiveMaximum);
schema.setMinimum(min);
schema.setExclusiveMinimum(exclusiveMinimum);
schema.setPattern(pattern);
schema.setFormat(format);
if (preserveUnknownFields) {
Expand Down Expand Up @@ -530,8 +545,18 @@ private void handleTypeAnnotations(final T schema, BeanProperty beanProperty, Cl
.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()));
Optional.ofNullable(at.getAnnotation(Max.class)).ifPresent(a -> schema.setMaximum(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);
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public interface KubernetesJSONSchemaProps {

void setMaximum(Double max);

void setExclusiveMaximum(Boolean b);

void setMinimum(Double min);

void setExclusiveMinimum(Boolean b);

void setPattern(String pattern);

void setFormat(String format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.fabric8.crd.generator.approvaltests.printercolum.PrinterColumn;
import io.fabric8.crd.generator.approvaltests.replica.Replica;
import io.fabric8.crd.generator.approvaltests.selectablefield.SelectableField;
import io.fabric8.crd.generator.approvaltests.validation.Validation;
import io.fabric8.kubernetes.client.CustomResource;
import io.sundr.utils.Strings;
import org.approvaltests.Approvals;
Expand Down Expand Up @@ -183,6 +184,7 @@ static Stream<TestCase> crdApprovalCasesApiV2(String crdVersion) {
for (boolean parallel : new boolean[] { false, true }) {
cases.add(new TestCase("printercolumns.sample.fabric8.io", crdVersion, parallel, PrinterColumn.class));
cases.add(new TestCase("selectablefields.sample.fabric8.io", crdVersion, parallel, SelectableField.class));
cases.add(new TestCase("validations.samples.fabric8.io", crdVersion, parallel, Validation.class));
}
return cases.stream();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.generator.approvaltests.validation;

import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Version("v1alpha1")
@Group("samples.fabric8.io")
public class Validation extends CustomResource<ValidationSpec, Void> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.generator.approvaltests.validation;

import io.fabric8.generator.annotation.Max;
import io.fabric8.generator.annotation.Min;
import io.fabric8.generator.annotation.Pattern;
import lombok.Data;

@Data
public class ValidationSpec {

private ValidationOnInteger onInteger;
private ValidationOnIntegerPrim onIntegerPrim;
private ValidationOnLong onLong;
private ValidationOnLongPrim onLongPrim;
private ValidationOnFloat onFloat;
private ValidationOnFloatPrim onFloatPrim;
private ValidationOnDouble onDouble;
private ValidationOnDoublePrim onDoublePrim;
private ValidationOnString onString;

@Data
static class ValidationOnInteger {
@Min(1)
private Integer minimum1;
@Min(value = 1, inclusive = false)
private Integer minimumExclusive1;
@Max(1)
private Integer maximum1;
@Max(value = 1, inclusive = false)
private Integer maximumExclusive1;
@Min(1)
@Max(3)
private Integer minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private Integer minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnIntegerPrim {
@Min(1)
private int minimum1;
@Min(value = 1, inclusive = false)
private int minimumExclusive1;
@Max(1)
private int maximum1;
@Max(value = 1, inclusive = false)
private int maximumExclusive1;
@Min(1)
@Max(3)
private int minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private int minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnLong {
@Min(1)
private Long minimum1;
@Min(value = 1, inclusive = false)
private Long minimumExclusive1;
@Max(1)
private Long maximum1;
@Max(value = 1, inclusive = false)
private Long maximumExclusive1;
@Min(1)
@Max(3)
private Long minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private Long minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnLongPrim {
@Min(1)
private long minimum1;
@Min(value = 1, inclusive = false)
private long minimumExclusive1;
@Max(1)
private long maximum1;
@Max(value = 1, inclusive = false)
private long maximumExclusive1;
@Min(1)
@Max(3)
private long minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private long minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnFloat {
@Min(1)
private Float minimum1;
@Min(value = 1, inclusive = false)
private Float minimumExclusive1;
@Max(1)
private Float maximum1;
@Max(value = 1, inclusive = false)
private Float maximumExclusive1;
@Min(1)
@Max(3)
private Float minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private Float minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnFloatPrim {
@Min(1)
private float minimum1;
@Min(value = 1, inclusive = false)
private float minimumExclusive1;
@Max(1)
private float maximum1;
@Max(value = 1, inclusive = false)
private float maximumExclusive1;
@Min(1)
@Max(3)
private float minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private float minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnDouble {
@Min(1)
private Double minimum1;
@Min(value = 1, inclusive = false)
private Double minimumExclusive1;
@Max(1)
private Double maximum1;
@Max(value = 1, inclusive = false)
private Double maximumExclusive1;
@Min(1)
@Max(3)
private Double minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private Double minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnDoublePrim {
@Min(1)
private double minimum1;
@Min(value = 1, inclusive = false)
private double minimumExclusive1;
@Max(1)
private double maximum1;
@Max(value = 1, inclusive = false)
private double maximumExclusive1;
@Min(1)
@Max(3)
private double minimum1Maximum3;
@Min(value = 1, inclusive = false)
@Max(value = 3, inclusive = false)
private double minimumExclusive1MaximumExclusive3;
}

@Data
static class ValidationOnString {
@Pattern("(a|b)+")
private String pattern;
}

}
Loading

0 comments on commit 4536b0b

Please sign in to comment.