Skip to content

Commit 35b7114

Browse files
committed
code review
1 parent 0fcb6e3 commit 35b7114

File tree

5 files changed

+101
-82
lines changed

5 files changed

+101
-82
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/data/DataRestRequestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private void addParameters(OpenAPI openAPI, RequestMethod requestMethod, MethodA
227227
MethodParameter methodParameter, ParameterInfo parameterInfo, Parameter parameter) {
228228
List<Annotation> parameterAnnotations = Arrays.asList(getParameterAnnotations(methodParameter));
229229
if (requestBuilder.isValidParameter(parameter,methodAttributes)) {
230-
requestBuilder.applyBeanValidatorAnnotations(methodParameter, parameter, parameterAnnotations, parameterInfo.isParameterObject());
230+
requestBuilder.applyBeanValidatorAnnotations(methodParameter, parameter, parameterAnnotations, parameterInfo.isParameterObject(), openAPI.getOpenapi());
231231
operation.addParametersItem(parameter);
232232
}
233233
else if (!RequestMethod.GET.equals(requestMethod)) {

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
328328
}
329329
}
330330
// Process: applyValidationsToSchema
331-
applyBeanValidatorAnnotations(methodParameter, parameter, parameterAnnotations, parameterInfo.isParameterObject());
331+
applyBeanValidatorAnnotations(methodParameter, parameter, parameterAnnotations, parameterInfo.isParameterObject(), openAPI.getOpenapi());
332332
}
333333
else if (!RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.getVersion().equals(openAPI.getOpenapi())) {
334334
if (operation.getRequestBody() != null)
@@ -604,26 +604,27 @@ public Parameter buildParam(ParameterInfo parameterInfo, Components components,
604604
/**
605605
* Apply bean validator annotations.
606606
*
607-
* @param methodParameter the method parameter
608-
* @param parameter the parameter
609-
* @param annotations the annotations
607+
* @param methodParameter the method parameter
608+
* @param parameter the parameter
609+
* @param annotations the annotations
610610
* @param isParameterObject the is parameter object
611+
* @param openapiVersion the openapi version
611612
*/
612-
public void applyBeanValidatorAnnotations(final MethodParameter methodParameter, final Parameter parameter, final List<Annotation> annotations, final boolean isParameterObject) {
613+
public void applyBeanValidatorAnnotations(final MethodParameter methodParameter, final Parameter parameter, final List<Annotation> annotations, final boolean isParameterObject, String openapiVersion) {
613614
boolean annotatedNotNull = annotations != null && SchemaUtils.annotatedNotNull(annotations);
614615
if (annotatedNotNull && !isParameterObject) {
615616
parameter.setRequired(true);
616617
}
617618
if (annotations != null) {
618619
Schema<?> schema = parameter.getSchema();
619-
SchemaUtils.applyValidationsToSchema(schema, annotations);
620+
SchemaUtils.applyValidationsToSchema(schema, annotations, openapiVersion);
620621
if (schema instanceof ArraySchema && isParameterObject && methodParameter instanceof DelegatingMethodParameter mp) {
621622
Field field = mp.getField();
622623
if (field != null && field.getAnnotatedType() instanceof AnnotatedParameterizedType paramType) {
623624
java.lang.reflect.AnnotatedType[] typeArgs = paramType.getAnnotatedActualTypeArguments();
624625
for (java.lang.reflect.AnnotatedType typeArg : typeArgs) {
625626
List<Annotation> genericAnnotations = Arrays.stream(typeArg.getAnnotations()).toList();
626-
SchemaUtils.applyValidationsToSchema(schema.getItems(), genericAnnotations);
627+
SchemaUtils.applyValidationsToSchema(schema.getItems(), genericAnnotations, openapiVersion);
627628
}
628629
}
629630
}

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import jakarta.validation.constraints.DecimalMin;
2222
import jakarta.validation.constraints.Max;
2323
import jakarta.validation.constraints.Min;
24+
import jakarta.validation.constraints.Negative;
2425
import jakarta.validation.constraints.NegativeOrZero;
2526
import jakarta.validation.constraints.Pattern;
2627
import jakarta.validation.constraints.Positive;
2728
import jakarta.validation.constraints.PositiveOrZero;
2829
import jakarta.validation.constraints.Size;
2930
import kotlin.reflect.KProperty;
3031
import kotlin.reflect.jvm.ReflectJvmMapping;
32+
import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVersion;
3133

3234
import org.springframework.core.KotlinDetector;
3335
import org.springframework.lang.Nullable;
@@ -184,21 +186,36 @@ public static boolean fieldRequired(Field field, @Nullable io.swagger.v3.oas.ann
184186
/**
185187
* Apply validations to schema. the annotation order effects the result of the
186188
* validation.
187-
* @param schema the schema
188-
* @param annotations the annotations
189+
*
190+
* @param schema the schema
191+
* @param annotations the annotations
192+
* @param openapiVersion the openapi version
189193
*/
190-
public static void applyValidationsToSchema(Schema<?> schema, List<Annotation> annotations) {
194+
public static void applyValidationsToSchema(Schema<?> schema, List<Annotation> annotations, String openapiVersion) {
191195
annotations.forEach(anno -> {
192196
String annotationName = anno.annotationType().getSimpleName();
193197
if (annotationName.equals(Positive.class.getSimpleName())) {
194-
schema.setMinimum(BigDecimal.ONE);
198+
if(OpenApiVersion.OPENAPI_3_1.getVersion().equals(openapiVersion)){
199+
schema.setExclusiveMinimumValue(BigDecimal.ZERO);
200+
} else {
201+
schema.setMinimum(BigDecimal.ZERO);
202+
schema.setExclusiveMinimum(true);
203+
}
195204
}
196205
if (annotationName.equals(PositiveOrZero.class.getSimpleName())) {
197206
schema.setMinimum(BigDecimal.ZERO);
198207
}
199208
if (annotationName.equals(NegativeOrZero.class.getSimpleName())) {
200209
schema.setMaximum(BigDecimal.ZERO);
201210
}
211+
if (annotationName.equals(Negative.class.getSimpleName())) {
212+
if(OpenApiVersion.OPENAPI_3_1.getVersion().equals(openapiVersion)){
213+
schema.setExclusiveMaximumValue(BigDecimal.ZERO);
214+
} else {
215+
schema.setMaximum(BigDecimal.ZERO);
216+
schema.setExclusiveMaximum(true);
217+
}
218+
}
202219
if (annotationName.equals(Min.class.getSimpleName())) {
203220
schema.setMinimum(BigDecimal.valueOf(((Min) anno).value()));
204221
}

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,25 @@
5151
}
5252
}
5353
},
54-
"/persons2": {
54+
"/persons7": {
5555
"get": {
5656
"tags": [
5757
"hello-controller"
5858
],
59-
"summary": "Persons 2 string.",
60-
"description": "Persons 2 string.",
61-
"operationId": "persons2",
59+
"summary": "Persons 7 string.",
60+
"description": "Persons 7 string.",
61+
"operationId": "persons7",
6262
"parameters": [
6363
{
64-
"name": "name",
64+
"name": "age",
6565
"in": "query",
66-
"description": "persons name",
66+
"description": "the age",
6767
"required": true,
6868
"schema": {
69-
"minLength": 1,
70-
"type": "string"
69+
"minimum": 0,
70+
"exclusiveMinimum": true,
71+
"type": "integer",
72+
"format": "int32"
7173
}
7274
}
7375
],
@@ -85,14 +87,14 @@
8587
}
8688
}
8789
},
88-
"/persons3": {
90+
"/persons6": {
8991
"get": {
9092
"tags": [
9193
"hello-controller"
9294
],
93-
"summary": "Persons 3 string.",
94-
"description": "Persons 3 string.",
95-
"operationId": "persons3",
95+
"summary": "Persons 6 string.",
96+
"description": "Persons 6 string.",
97+
"operationId": "persons6",
9698
"parameters": [
9799
{
98100
"name": "name",
@@ -119,22 +121,22 @@
119121
}
120122
}
121123
},
122-
"/persons4": {
124+
"/persons5": {
123125
"get": {
124126
"tags": [
125127
"hello-controller"
126128
],
127-
"summary": "Persons 4 string.",
128-
"description": "Persons 4 string.",
129-
"operationId": "persons4",
129+
"summary": "Persons 5 string.",
130+
"description": "Persons 5 string.",
131+
"operationId": "persons5",
130132
"parameters": [
131133
{
132134
"name": "age",
133135
"in": "query",
134136
"description": "the age",
135137
"required": true,
136138
"schema": {
137-
"minimum": 0,
139+
"maximum": 0,
138140
"type": "integer",
139141
"format": "int32"
140142
}
@@ -154,22 +156,22 @@
154156
}
155157
}
156158
},
157-
"/persons5": {
159+
"/persons4": {
158160
"get": {
159161
"tags": [
160162
"hello-controller"
161163
],
162-
"summary": "Persons 5 string.",
163-
"description": "Persons 5 string.",
164-
"operationId": "persons5",
164+
"summary": "Persons 4 string.",
165+
"description": "Persons 4 string.",
166+
"operationId": "persons4",
165167
"parameters": [
166168
{
167169
"name": "age",
168170
"in": "query",
169171
"description": "the age",
170172
"required": true,
171173
"schema": {
172-
"maximum": 0,
174+
"minimum": 0,
173175
"type": "integer",
174176
"format": "int32"
175177
}
@@ -189,14 +191,14 @@
189191
}
190192
}
191193
},
192-
"/persons6": {
194+
"/persons3": {
193195
"get": {
194196
"tags": [
195197
"hello-controller"
196198
],
197-
"summary": "Persons 6 string.",
198-
"description": "Persons 6 string.",
199-
"operationId": "persons6",
199+
"summary": "Persons 3 string.",
200+
"description": "Persons 3 string.",
201+
"operationId": "persons3",
200202
"parameters": [
201203
{
202204
"name": "name",
@@ -223,24 +225,23 @@
223225
}
224226
}
225227
},
226-
"/persons7": {
228+
"/persons2": {
227229
"get": {
228230
"tags": [
229231
"hello-controller"
230232
],
231-
"summary": "Persons 7 string.",
232-
"description": "Persons 7 string.",
233-
"operationId": "persons7",
233+
"summary": "Persons 2 string.",
234+
"description": "Persons 2 string.",
235+
"operationId": "persons2",
234236
"parameters": [
235237
{
236-
"name": "age",
238+
"name": "name",
237239
"in": "query",
238-
"description": "the age",
240+
"description": "persons name",
239241
"required": true,
240242
"schema": {
241-
"minimum": 1,
242-
"type": "integer",
243-
"format": "int32"
243+
"minLength": 1,
244+
"type": "string"
244245
}
245246
}
246247
],

0 commit comments

Comments
 (0)