diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SchemaUtils.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SchemaUtils.java index 4d2805fbc1..2190a08c98 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SchemaUtils.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SchemaUtils.java @@ -291,19 +291,29 @@ public static void applyValidationsToSchema(Schema schema, List a } if (annotationName.equals(DecimalMin.class.getSimpleName())) { DecimalMin min = (DecimalMin) anno; + BigDecimal minValue = BigDecimal.valueOf(Double.parseDouble(min.value())); if (min.inclusive()) { - schema.setMinimum(BigDecimal.valueOf(Double.parseDouble(min.value()))); + schema.setMinimum(minValue); + } + else if (OpenApiVersion.OPENAPI_3_1.getVersion().equals(openapiVersion)) { + schema.setExclusiveMinimumValue(minValue); } else { + schema.setMinimum(minValue); schema.setExclusiveMinimum(true); } } if (annotationName.equals(DecimalMax.class.getSimpleName())) { DecimalMax max = (DecimalMax) anno; + BigDecimal maxValue = BigDecimal.valueOf(Double.parseDouble(max.value())); if (max.inclusive()) { - schema.setMaximum(BigDecimal.valueOf(Double.parseDouble(max.value()))); + schema.setMaximum(maxValue); + } + else if (OpenApiVersion.OPENAPI_3_1.getVersion().equals(openapiVersion)) { + schema.setExclusiveMaximumValue(maxValue); } else { + schema.setMaximum(maxValue); schema.setExclusiveMaximum(true); } } diff --git a/springdoc-openapi-starter-common/src/test/java/org/springdoc/core/utils/SchemaUtilsDecimalBoundsTest.java b/springdoc-openapi-starter-common/src/test/java/org/springdoc/core/utils/SchemaUtilsDecimalBoundsTest.java new file mode 100644 index 0000000000..18ea104ea8 --- /dev/null +++ b/springdoc-openapi-starter-common/src/test/java/org/springdoc/core/utils/SchemaUtilsDecimalBoundsTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2019-2026 the original author or authors. + * + * 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 + * + * https://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 org.springdoc.core.utils; + +import java.lang.annotation.Annotation; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; + +import io.swagger.v3.oas.models.media.NumberSchema; +import io.swagger.v3.oas.models.media.Schema; +import jakarta.validation.constraints.DecimalMax; +import jakarta.validation.constraints.DecimalMin; +import org.junit.jupiter.api.Test; +import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVersion; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests that {@code @DecimalMin} and {@code @DecimalMax} keep their bound when it is exclusive. + * + * @author ErickGCA + */ +class SchemaUtilsDecimalBoundsTest { + + @DecimalMin(value = "0.5", inclusive = false) + @DecimalMax(value = "9.5", inclusive = false) + private BigDecimal exclusive; + + @DecimalMin("0.5") + @DecimalMax("9.5") + private BigDecimal inclusive; + + @Test + void exclusiveBoundsKeepTheirValueInOpenApi30() throws NoSuchFieldException { + Schema schema = applyValidations("exclusive", OpenApiVersion.OPENAPI_3_0); + + assertEquals(new BigDecimal("0.5"), schema.getMinimum()); + assertTrue(schema.getExclusiveMinimum()); + assertEquals(new BigDecimal("9.5"), schema.getMaximum()); + assertTrue(schema.getExclusiveMaximum()); + } + + @Test + void exclusiveBoundsKeepTheirValueInOpenApi31() throws NoSuchFieldException { + Schema schema = applyValidations("exclusive", OpenApiVersion.OPENAPI_3_1); + + assertEquals(new BigDecimal("0.5"), schema.getExclusiveMinimumValue()); + assertEquals(new BigDecimal("9.5"), schema.getExclusiveMaximumValue()); + assertNull(schema.getMinimum()); + assertNull(schema.getExclusiveMinimum()); + assertNull(schema.getMaximum()); + assertNull(schema.getExclusiveMaximum()); + } + + @Test + void inclusiveBoundsAreNotExclusive() throws NoSuchFieldException { + for (OpenApiVersion version : OpenApiVersion.values()) { + Schema schema = applyValidations("inclusive", version); + + assertEquals(new BigDecimal("0.5"), schema.getMinimum()); + assertEquals(new BigDecimal("9.5"), schema.getMaximum()); + assertNull(schema.getExclusiveMinimum()); + assertNull(schema.getExclusiveMinimumValue()); + assertNull(schema.getExclusiveMaximum()); + assertNull(schema.getExclusiveMaximumValue()); + } + } + + private Schema applyValidations(String fieldName, OpenApiVersion version) throws NoSuchFieldException { + List annotations = Arrays + .asList(SchemaUtilsDecimalBoundsTest.class.getDeclaredField(fieldName).getAnnotations()); + Schema schema = new NumberSchema(); + SchemaUtils.applyValidationsToSchema(schema, annotations, version.getVersion()); + return schema; + } + +}