Skip to content

Commit bfed970

Browse files
author
bnasslahsen
committed
Add support for none required fields on @ParameterObject. Fixes #541
1 parent 75377e9 commit bfed970

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Objects;
1818
import java.util.stream.Stream;
1919

20+
import io.swagger.v3.oas.annotations.Parameter;
2021
import org.apache.commons.lang3.ArrayUtils;
2122
import org.springdoc.api.annotations.ParameterObject;
2223
import org.springdoc.core.converters.AdditionalModelsConverter;
@@ -141,16 +142,24 @@ public void initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDisc
141142
@Nullable
142143
static MethodParameter fromGetterOfField(Class<?> paramClass, Field field) {
143144
try {
145+
Annotation[] filedAnnotations = field.getDeclaredAnnotations();
146+
Parameter parameter = field.getAnnotation(Parameter.class);
147+
if (parameter != null && !parameter.required()) {
148+
Field fieldNullable = NullableFieldClass.class.getDeclaredField("nullableField");
149+
Annotation annotation = fieldNullable.getAnnotation(Nullable.class);
150+
filedAnnotations = ArrayUtils.add(filedAnnotations, annotation);
151+
}
152+
Annotation[] filedAnnotationsNew = filedAnnotations;
144153
return Stream.of(Introspector.getBeanInfo(paramClass).getPropertyDescriptors())
145154
.filter(d -> d.getName().equals(field.getName()))
146155
.map(PropertyDescriptor::getReadMethod)
147156
.filter(Objects::nonNull)
148157
.findFirst()
149158
.map(method -> new MethodParameter(method, -1))
150-
.map(param -> new DelegatingMethodParameter(param, field.getName(), field.getDeclaredAnnotations()))
159+
.map(param -> new DelegatingMethodParameter(param, field.getName(), filedAnnotationsNew))
151160
.orElse(null);
152161
}
153-
catch (IntrospectionException e) {
162+
catch (IntrospectionException | NoSuchFieldException e) {
154163
return null;
155164
}
156165
}
@@ -172,4 +181,9 @@ public int hashCode() {
172181
result = 31 * result + Arrays.hashCode(additionalParameterAnnotations);
173182
return result;
174183
}
184+
185+
private class NullableFieldClass {
186+
@Nullable
187+
private String nullableField;
188+
}
175189
}

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app102/RequestParams.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
package test.org.springdoc.api.app102;
22

3+
import java.util.Optional;
4+
35
import io.swagger.v3.oas.annotations.Parameter;
46

57
import org.springframework.lang.Nullable;
68

79
public class RequestParams {
8-
@Nullable
10+
911
@Parameter(description = "string parameter")
1012
private String stringParam;
1113

14+
private String stringParam1;
15+
16+
@Parameter(description = "string parameter2", required = true)
17+
private String stringParam2;
18+
1219
@Parameter(description = "int parameter")
1320
private int intParam;
1421

22+
private Optional<String> intParam2;
23+
24+
@Nullable
25+
private String intParam3;
26+
1527
public String getStringParam() {
1628
return stringParam;
1729
}
@@ -27,4 +39,37 @@ public int getIntParam() {
2739
public void setIntParam(int intParam) {
2840
this.intParam = intParam;
2941
}
42+
43+
public Optional<String> getIntParam2() {
44+
return intParam2;
45+
}
46+
47+
public void setIntParam2(Optional<String> intParam2) {
48+
this.intParam2 = intParam2;
49+
}
50+
51+
@Nullable
52+
public String getIntParam3() {
53+
return intParam3;
54+
}
55+
56+
public void setIntParam3(@Nullable String intParam3) {
57+
this.intParam3 = intParam3;
58+
}
59+
60+
public String getStringParam1() {
61+
return stringParam1;
62+
}
63+
64+
public void setStringParam1(String stringParam1) {
65+
this.stringParam1 = stringParam1;
66+
}
67+
68+
public String getStringParam2() {
69+
return stringParam2;
70+
}
71+
72+
public void setStringParam2(String stringParam2) {
73+
this.stringParam2 = stringParam2;
74+
}
3075
}

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,48 @@
3535
"type": "string"
3636
}
3737
},
38+
{
39+
"name": "stringParam1",
40+
"in": "query",
41+
"required": true,
42+
"schema": {
43+
"type": "string"
44+
}
45+
},
46+
{
47+
"name": "stringParam2",
48+
"in": "query",
49+
"description": "string parameter2",
50+
"required": true,
51+
"schema": {
52+
"type": "string"
53+
}
54+
},
3855
{
3956
"name": "intParam",
4057
"in": "query",
4158
"description": "int parameter",
42-
"required": true,
59+
"required": false,
4360
"schema": {
4461
"type": "integer",
4562
"format": "int32"
4663
}
64+
},
65+
{
66+
"name": "intParam2",
67+
"in": "query",
68+
"required": false,
69+
"schema": {
70+
"type": "string"
71+
}
72+
},
73+
{
74+
"name": "intParam3",
75+
"in": "query",
76+
"required": false,
77+
"schema": {
78+
"type": "string"
79+
}
4780
}
4881
],
4982
"responses": {
@@ -55,4 +88,4 @@
5588
}
5689
},
5790
"components": {}
58-
}
91+
}

0 commit comments

Comments
 (0)