Skip to content

Support for @Positive #3001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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 @@ -23,6 +23,7 @@
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NegativeOrZero;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import jakarta.validation.constraints.Size;
import kotlin.reflect.KProperty;
Expand Down Expand Up @@ -189,6 +190,9 @@ public static boolean fieldRequired(Field field, @Nullable io.swagger.v3.oas.ann
public static void applyValidationsToSchema(Schema<?> schema, List<Annotation> annotations) {
annotations.forEach(anno -> {
String annotationName = anno.annotationType().getSimpleName();
if (annotationName.equals(Positive.class.getSimpleName())) {
schema.setMinimum(BigDecimal.ONE);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Value BigDecimal.ONE is valid only for integers.
For decimals, for example, we can have a valid value 0.00001.

Copy link

@kdebski85 kdebski85 Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the right approach is schema.setExclusiveMinimumValue(BigDecimal.ZERO);

However, according to https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/Schema.java#L441 it is supported only in OpenAPI 3.1.

}
if (annotationName.equals(PositiveOrZero.class.getSimpleName())) {
schema.setMinimum(BigDecimal.ZERO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import jakarta.validation.constraints.NegativeOrZero;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;

import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -108,4 +109,14 @@ public String persons6(@NotEmpty @Parameter(description = "persons name") String
return "OK";
}

/**
* Persons 7 string.
*
* @param age the age
* @return the string
*/
@GetMapping(value = "/persons7")
public String persons7(@Positive int age) {
return "OK";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import jakarta.validation.constraints.NegativeOrZero;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;

import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -108,4 +109,14 @@ public String persons6(@NotEmpty @Parameter(description = "persons name") String
return "OK";
}

/**
* Persons 7 string.
*
* @param age the age
* @return the string
*/
@GetMapping(value = "/persons7")
public String persons7(@Positive int age) {
return "OK";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,41 @@
}
}
}
},
"/persons7": {
"get": {
"tags": [
"hello-controller"
],
"summary": "Persons 7 string.",
"description": "Persons 7 string.",
"operationId": "persons7",
"parameters": [
{
"name": "age",
"in": "query",
"description": "the age",
"required": true,
"schema": {
"minimum": 1,
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "the string",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,41 @@
}
}
}
},
"/persons7": {
"get": {
"tags": [
"hello-controller"
],
"summary": "Persons 7 string.",
"description": "Persons 7 string.",
"operationId": "persons7",
"parameters": [
{
"name": "age",
"in": "query",
"description": "the age",
"required": true,
"schema": {
"minimum": 1,
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "the string",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
Expand Down