Skip to content

Commit dc996b3

Browse files
author
bnasslahsen
committed
Allow @Schema annotation to set an attribute as not required even if it's annotated with @NotNull. fixes #459
1 parent f8fef19 commit dc996b3

File tree

5 files changed

+153
-1
lines changed

5 files changed

+153
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ public Operation build(Components components, HandlerMethod handlerMethod, Reque
177177
parameter = parameterBuilder.mergeParameter(operationParameters, parameter);
178178
if (isValidParameter(parameter)) {
179179
applyBeanValidatorAnnotations(parameter, Arrays.asList(parameters[i].getAnnotations()));
180+
parameter = customiseParameter(parameter, parameterInfo, handlerMethod);
180181
}
181182
else if (!RequestMethod.GET.equals(requestMethod)) {
183+
parameter = customiseParameter(parameter, parameterInfo, handlerMethod);
182184
if (operation.getRequestBody() != null)
183185
requestBodyInfo.setRequestBody(operation.getRequestBody());
184186
requestBodyBuilder.calculateRequestBodyInfo(components, handlerMethod, methodAttributes, i,
@@ -293,7 +295,6 @@ else if (pathVar != null) {
293295
parameter = this.buildParam(QUERY_PARAM, components, parameterInfo, Boolean.TRUE, null, jsonView);
294296
}
295297

296-
parameter = customiseParameter(parameter, parameterInfo, handlerMethod);
297298
return parameter;
298299
}
299300

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app92;
20+
21+
import javax.validation.constraints.NotNull;
22+
23+
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.RequestMapping;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
@RestController
28+
@RequestMapping("/test")
29+
public class HelloController {
30+
31+
@GetMapping
32+
String index(@NotNull String test) {
33+
return null;
34+
}
35+
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app92;
20+
21+
import javax.validation.constraints.NotNull;
22+
23+
import io.swagger.v3.oas.models.parameters.Parameter;
24+
25+
import org.springframework.stereotype.Component;
26+
import org.springframework.web.method.HandlerMethod;
27+
28+
@Component
29+
public class ParameterCustomizer implements org.springdoc.core.customizers.ParameterCustomizer {
30+
@Override
31+
public Parameter customize(Parameter parameterModel, java.lang.reflect.Parameter parameter, HandlerMethod handlerMethod) {
32+
NotNull annotation = parameter.getAnnotation(NotNull.class);
33+
if (annotation != null) {
34+
parameterModel.required(false);
35+
}
36+
return parameterModel;
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app92;
20+
21+
22+
import test.org.springdoc.api.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
26+
public class SpringDocApp92Test extends AbstractSpringDocTest {
27+
28+
@SpringBootApplication
29+
static class SpringDocTestApp {}
30+
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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": "index",
20+
"parameters": [
21+
{
22+
"name": "test",
23+
"in": "query",
24+
"required": false,
25+
"schema": {
26+
"type": "string"
27+
}
28+
}
29+
],
30+
"responses": {
31+
"200": {
32+
"description": "default response",
33+
"content": {
34+
"*/*": {
35+
"schema": {
36+
"type": "string"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
},
45+
"components": {}
46+
}

0 commit comments

Comments
 (0)