Skip to content
Open
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 @@ -291,19 +291,29 @@ public static void applyValidationsToSchema(Schema<?> schema, List<Annotation> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Annotation> annotations = Arrays
.asList(SchemaUtilsDecimalBoundsTest.class.getDeclaredField(fieldName).getAnnotations());
Schema<?> schema = new NumberSchema();
SchemaUtils.applyValidationsToSchema(schema, annotations, version.getVersion());
return schema;
}

}