Skip to content

Commit 915a071

Browse files
committed
Support returning null in ParameterCustomizer/ Fixes #2822
1 parent 39ef9f4 commit 915a071

File tree

6 files changed

+206
-6
lines changed

6 files changed

+206
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,12 @@ protected void customiseParameter(Parameter parameter, ParameterInfo parameterIn
434434
int index = operationParameters.indexOf(parameter);
435435
for (ParameterCustomizer parameterCustomizer : parameterCustomizerList)
436436
parameter = parameterCustomizer.customize(parameter, parameterInfo.getMethodParameter());
437-
if (index != -1)
438-
operationParameters.set(index, parameter);
437+
if (index != -1) {
438+
if (parameter == null)
439+
operationParameters.remove(index);
440+
else
441+
operationParameters.set(index, parameter);
442+
}
439443
}
440444
}
441445

@@ -780,18 +784,18 @@ private boolean isRequestBodyParam(RequestMethod requestMethod, ParameterInfo pa
780784
}
781785

782786
/**
783-
* Checks whether Swagger's or Spring's RequestBody annotation is present on a parameter or method
784-
*
787+
* Checks whether Swagger's or Spring's RequestBody annotation is present on a parameter or method
788+
*
785789
* @param methodParameter the method parameter
786790
* @return the boolean
787-
*/
791+
*/
788792
private boolean checkRequestBodyAnnotation(MethodParameter methodParameter) {
789793
return methodParameter.hasParameterAnnotation(org.springframework.web.bind.annotation.RequestBody.class)
790794
|| methodParameter.hasParameterAnnotation(io.swagger.v3.oas.annotations.parameters.RequestBody.class)
791795
|| AnnotatedElementUtils.isAnnotated(Objects.requireNonNull(methodParameter.getParameter()), io.swagger.v3.oas.annotations.parameters.RequestBody.class)
792796
|| AnnotatedElementUtils.isAnnotated(Objects.requireNonNull(methodParameter.getMethod()), io.swagger.v3.oas.annotations.parameters.RequestBody.class);
793797
}
794-
798+
795799
/**
796800
* Check file boolean.
797801
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2024 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app234;
26+
27+
import test.org.springdoc.api.v30.app179.MyObj;
28+
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RestController;
31+
32+
33+
@RestController
34+
public class HelloController {
35+
36+
@GetMapping("/test")
37+
String test(MyObj obj) {
38+
return obj.getContent();
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2024 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app234;
26+
27+
public class MyObj {
28+
private final String id;
29+
30+
private final String content;
31+
32+
public MyObj(String id, String content) {
33+
this.id = id;
34+
this.content = content;
35+
}
36+
37+
public String getId() {
38+
return id;
39+
}
40+
41+
public String getContent() {
42+
return content;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2024 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app234;
26+
27+
import io.swagger.v3.oas.models.parameters.Parameter;
28+
import org.springdoc.core.customizers.ParameterCustomizer;
29+
30+
import org.springframework.core.MethodParameter;
31+
import org.springframework.stereotype.Component;
32+
33+
@Component
34+
public class MyPathParameterCustomizer implements ParameterCustomizer {
35+
@Override
36+
public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) {
37+
return null;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2024 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app234;
26+
27+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
28+
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
31+
public class SpringDocApp234Test extends AbstractSpringDocV30Test {
32+
33+
@SpringBootApplication
34+
static class SpringDocTestApp {}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/test": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "test",
20+
"responses": {
21+
"200": {
22+
"description": "OK",
23+
"content": {
24+
"*/*": {
25+
"schema": {
26+
"type": "string"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
},
35+
"components": {
36+
"schemas": {}
37+
}
38+
}

0 commit comments

Comments
 (0)