Skip to content

Commit 584faa2

Browse files
author
user
committed
Support for @positive
1 parent a886363 commit 584faa2

File tree

5 files changed

+110
-14
lines changed

5 files changed

+110
-14
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SchemaUtils.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.springdoc.core.utils;
22

3+
import static org.springdoc.core.utils.Constants.OPENAPI_ARRAY_TYPE;
4+
import static org.springdoc.core.utils.Constants.OPENAPI_STRING_TYPE;
5+
36
import java.lang.annotation.Annotation;
47
import java.lang.reflect.Field;
58
import java.math.BigDecimal;
@@ -14,6 +17,9 @@
1417
import java.util.Set;
1518
import java.util.stream.Collectors;
1619

20+
import org.springframework.core.KotlinDetector;
21+
import org.springframework.lang.Nullable;
22+
1723
import io.swagger.v3.oas.annotations.Parameter;
1824
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
1925
import io.swagger.v3.oas.models.media.Schema;
@@ -23,17 +29,12 @@
2329
import jakarta.validation.constraints.Min;
2430
import jakarta.validation.constraints.NegativeOrZero;
2531
import jakarta.validation.constraints.Pattern;
32+
import jakarta.validation.constraints.Positive;
2633
import jakarta.validation.constraints.PositiveOrZero;
2734
import jakarta.validation.constraints.Size;
2835
import kotlin.reflect.KProperty;
2936
import kotlin.reflect.jvm.ReflectJvmMapping;
3037

31-
import org.springframework.core.KotlinDetector;
32-
import org.springframework.lang.Nullable;
33-
34-
import static org.springdoc.core.utils.Constants.OPENAPI_ARRAY_TYPE;
35-
import static org.springdoc.core.utils.Constants.OPENAPI_STRING_TYPE;
36-
3738
/**
3839
* The type Validation utils.
3940
*
@@ -189,6 +190,9 @@ public static boolean fieldRequired(Field field, @Nullable io.swagger.v3.oas.ann
189190
public static void applyValidationsToSchema(Schema<?> schema, List<Annotation> annotations) {
190191
annotations.forEach(anno -> {
191192
String annotationName = anno.annotationType().getSimpleName();
193+
if (annotationName.equals(Positive.class.getSimpleName())) {
194+
schema.setMinimum(BigDecimal.ONE);
195+
}
192196
if (annotationName.equals(PositiveOrZero.class.getSimpleName())) {
193197
schema.setMinimum(BigDecimal.ZERO);
194198
}

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/java/test/org/springdoc/api/v30/app18/HelloController.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@
2626

2727
package test.org.springdoc.api.v30.app18;
2828

29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.RestController;
32+
2933
import io.swagger.v3.oas.annotations.Parameter;
3034
import jakarta.validation.constraints.NegativeOrZero;
3135
import jakarta.validation.constraints.NotBlank;
3236
import jakarta.validation.constraints.NotEmpty;
37+
import jakarta.validation.constraints.Positive;
3338
import jakarta.validation.constraints.PositiveOrZero;
3439

35-
import org.springframework.web.bind.annotation.GetMapping;
36-
import org.springframework.web.bind.annotation.RequestParam;
37-
import org.springframework.web.bind.annotation.RestController;
38-
3940
/**
4041
* The type Hello controller.
4142
*/
@@ -108,4 +109,14 @@ public String persons6(@NotEmpty @Parameter(description = "persons name") String
108109
return "OK";
109110
}
110111

112+
/**
113+
* Persons 7 string.
114+
*
115+
* @param age the age
116+
* @return the string
117+
*/
118+
@GetMapping(value = "/persons7")
119+
public String persons7(@Positive int age) {
120+
return "OK";
121+
}
111122
}

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/java/test/org/springdoc/api/v31/app18/HelloController.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@
2626

2727
package test.org.springdoc.api.v31.app18;
2828

29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.RestController;
32+
2933
import io.swagger.v3.oas.annotations.Parameter;
3034
import jakarta.validation.constraints.NegativeOrZero;
3135
import jakarta.validation.constraints.NotBlank;
3236
import jakarta.validation.constraints.NotEmpty;
37+
import jakarta.validation.constraints.Positive;
3338
import jakarta.validation.constraints.PositiveOrZero;
3439

35-
import org.springframework.web.bind.annotation.GetMapping;
36-
import org.springframework.web.bind.annotation.RequestParam;
37-
import org.springframework.web.bind.annotation.RestController;
38-
3940
/**
4041
* The type Hello controller.
4142
*/
@@ -108,4 +109,14 @@ public String persons6(@NotEmpty @Parameter(description = "persons name") String
108109
return "OK";
109110
}
110111

112+
/**
113+
* Persons 7 string.
114+
*
115+
* @param age the age
116+
* @return the string
117+
*/
118+
@GetMapping(value = "/persons7")
119+
public String persons7(@Positive int age) {
120+
return "OK";
121+
}
111122
}

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/resources/results/3.0.1/app18.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,41 @@
222222
}
223223
}
224224
}
225+
},
226+
"/persons7": {
227+
"get": {
228+
"tags": [
229+
"hello-controller"
230+
],
231+
"summary": "Persons 7 string.",
232+
"description": "Persons 7 string.",
233+
"operationId": "persons7",
234+
"parameters": [
235+
{
236+
"name": "age",
237+
"in": "query",
238+
"description": "the age",
239+
"required": true,
240+
"schema": {
241+
"minimum": 1,
242+
"type": "integer",
243+
"format": "int32"
244+
}
245+
}
246+
],
247+
"responses": {
248+
"200": {
249+
"description": "the string",
250+
"content": {
251+
"*/*": {
252+
"schema": {
253+
"type": "string"
254+
}
255+
}
256+
}
257+
}
258+
}
259+
}
225260
}
226261
},
227262
"components": {}

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/resources/results/3.1.0/app18.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,41 @@
222222
}
223223
}
224224
}
225+
},
226+
"/persons7": {
227+
"get": {
228+
"tags": [
229+
"hello-controller"
230+
],
231+
"summary": "Persons 7 string.",
232+
"description": "Persons 7 string.",
233+
"operationId": "persons7",
234+
"parameters": [
235+
{
236+
"name": "age",
237+
"in": "query",
238+
"description": "the age",
239+
"required": true,
240+
"schema": {
241+
"minimum": 1,
242+
"type": "integer",
243+
"format": "int32"
244+
}
245+
}
246+
],
247+
"responses": {
248+
"200": {
249+
"description": "the string",
250+
"content": {
251+
"*/*": {
252+
"schema": {
253+
"type": "string"
254+
}
255+
}
256+
}
257+
}
258+
}
259+
}
225260
}
226261
},
227262
"components": {}

0 commit comments

Comments
 (0)