Skip to content

Commit a92f802

Browse files
author
springdoc
committed
fixes #161
1 parent eeefac4 commit a92f802

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public abstract class AbstractRequestBuilder {
2424
private final AbstractParameterBuilder parameterBuilder;
2525
private final RequestBodyBuilder requestBodyBuilder;
2626
private final OperationBuilder operationBuilder;
27+
private static final String[] ANNOTATIONS_FOR_REQUIRED = {NotNull.class.getName(), NotBlank.class.getName(), NotEmpty.class.getName()};
2728

2829
protected AbstractRequestBuilder(AbstractParameterBuilder parameterBuilder, RequestBodyBuilder requestBodyBuilder,
2930
OperationBuilder operationBuilder) {
@@ -87,7 +88,6 @@ public Operation build(Components components, HandlerMethod handlerMethod, Reque
8788
}
8889

8990
setParams(operation, operationParameters, requestBodyInfo);
90-
9191
// allow for customisation
9292
operation = customiseOperation(operation, handlerMethod);
9393

@@ -210,7 +210,9 @@ private void applyBeanValidatorAnnotations(final Parameter parameter, final List
210210
annotations.forEach(annotation -> annos.put(annotation.annotationType().getName(), annotation));
211211
}
212212

213-
if (annos.containsKey(NotNull.class.getName())) {
213+
boolean annotationExists = Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annos::containsKey);
214+
215+
if (annotationExists) {
214216
parameter.setRequired(true);
215217
}
216218

@@ -241,6 +243,12 @@ private void applyBeanValidatorAnnotations(final Parameter parameter, final List
241243
schema.setExclusiveMaximum(!max.inclusive());
242244
}
243245
}
246+
if (annos.containsKey(PositiveOrZero.class.getName())) {
247+
schema.setMinimum(BigDecimal.ZERO);
248+
}
249+
if (annos.containsKey(NegativeOrZero .class.getName())) {
250+
schema.setMaximum(BigDecimal.ZERO);
251+
}
244252
if (annos.containsKey(Pattern.class.getName())) {
245253
Pattern pattern = (Pattern) annos.get(Pattern.class.getName());
246254
schema.setPattern(pattern.regexp());

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app18/HelloController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import org.springframework.web.bind.annotation.RequestParam;
66
import org.springframework.web.bind.annotation.RestController;
77

8+
import javax.validation.constraints.NegativeOrZero;
89
import javax.validation.constraints.NotBlank;
10+
import javax.validation.constraints.NotEmpty;
11+
import javax.validation.constraints.PositiveOrZero;
912

1013
@RestController
1114
public class HelloController {
@@ -25,4 +28,18 @@ public String persons3(@NotBlank @Parameter(description = "persons name") @Reque
2528
return "OK";
2629
}
2730

31+
@GetMapping(value = "/persons4")
32+
public String persons4(@PositiveOrZero int age) {
33+
return "OK";
34+
}
35+
36+
@GetMapping(value = "/persons5")
37+
public String persons5(@NegativeOrZero int age) {
38+
return "OK";
39+
}
40+
@GetMapping(value = "/persons6")
41+
public String persons6(@NotEmpty @Parameter(description = "persons name") String name) {
42+
return "OK";
43+
}
44+
2845
}

springdoc-openapi-webmvc-core/src/test/resources/results/app18.json

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@
1111
}
1212
],
1313
"paths": {
14+
"/persons": {
15+
"get": {
16+
"operationId": "persons",
17+
"parameters": [
18+
{
19+
"name": "name",
20+
"in": "query",
21+
"required": true,
22+
"schema": {
23+
"type": "string"
24+
}
25+
}
26+
],
27+
"responses": {
28+
"200": {
29+
"description": "default response",
30+
"content": {
31+
"*/*": {
32+
"schema": {
33+
"type": "string"
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
},
1441
"/persons3": {
1542
"get": {
1643
"operationId": "persons3",
@@ -39,9 +66,67 @@
3966
}
4067
}
4168
},
42-
"/persons2": {
69+
"/persons5": {
4370
"get": {
44-
"operationId": "persons2",
71+
"operationId": "persons5",
72+
"parameters": [
73+
{
74+
"name": "age",
75+
"in": "query",
76+
"required": true,
77+
"schema": {
78+
"maximum": 0,
79+
"type": "integer",
80+
"format": "int32"
81+
}
82+
}
83+
],
84+
"responses": {
85+
"200": {
86+
"description": "default response",
87+
"content": {
88+
"*/*": {
89+
"schema": {
90+
"type": "string"
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}
97+
},
98+
"/persons4": {
99+
"get": {
100+
"operationId": "persons4",
101+
"parameters": [
102+
{
103+
"name": "age",
104+
"in": "query",
105+
"required": true,
106+
"schema": {
107+
"minimum": 0,
108+
"type": "integer",
109+
"format": "int32"
110+
}
111+
}
112+
],
113+
"responses": {
114+
"200": {
115+
"description": "default response",
116+
"content": {
117+
"*/*": {
118+
"schema": {
119+
"type": "string"
120+
}
121+
}
122+
}
123+
}
124+
}
125+
}
126+
},
127+
"/persons6": {
128+
"get": {
129+
"operationId": "persons6",
45130
"parameters": [
46131
{
47132
"name": "name",
@@ -67,13 +152,14 @@
67152
}
68153
}
69154
},
70-
"/persons": {
155+
"/persons2": {
71156
"get": {
72-
"operationId": "persons",
157+
"operationId": "persons2",
73158
"parameters": [
74159
{
75160
"name": "name",
76161
"in": "query",
162+
"description": "persons name",
77163
"required": true,
78164
"schema": {
79165
"type": "string"
@@ -95,6 +181,5 @@
95181
}
96182
}
97183
},
98-
"components": {
99-
}
100-
}
184+
"components": {}
185+
}

0 commit comments

Comments
 (0)