Skip to content

Commit 2dae34a

Browse files
author
bnasslahsen
committed
Incorrect schema addition on header if used in Spring mapping. Fixes #869.
1 parent 37dc83a commit 2dae34a

File tree

4 files changed

+208
-2
lines changed

4 files changed

+208
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
231231
String[] reflectionParametersNames = Arrays.stream(handlerMethod.getMethod().getParameters()).map(java.lang.reflect.Parameter::getName).toArray(String[]::new);
232232
if (pNames == null || Arrays.stream(pNames).anyMatch(Objects::isNull))
233233
pNames = reflectionParametersNames;
234-
parameters = DelegatingMethodParameter.customize(pNames, parameters,parameterBuilder.getDelegatingMethodParameterCustomizer());
234+
parameters = DelegatingMethodParameter.customize(pNames, parameters, parameterBuilder.getDelegatingMethodParameterCustomizer());
235235
RequestBodyInfo requestBodyInfo = new RequestBodyInfo();
236236
List<Parameter> operationParameters = (operation.getParameters() != null) ? operation.getParameters() : new ArrayList<>();
237237
Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap = getApiParameters(handlerMethod.getMethod());
@@ -326,7 +326,8 @@ public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes
326326
Parameter parameter = new Parameter().in(ParameterIn.HEADER.toString()).name(entry.getKey()).schema(new StringSchema().addEnumItem(entry.getValue()));
327327
if (map.containsKey(entry.getKey())) {
328328
parameter = map.get(entry.getKey());
329-
parameter.getSchema().addEnumItemObject(entry.getValue());
329+
if (StringUtils.isNotEmpty(entry.getValue()))
330+
parameter.getSchema().addEnumItemObject(entry.getValue());
330331
parameter.setSchema(parameter.getSchema());
331332
}
332333
map.put(entry.getKey(), parameter);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.app133;
20+
21+
import io.swagger.v3.oas.annotations.Parameter;
22+
import io.swagger.v3.oas.annotations.media.Schema;
23+
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RequestHeader;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
@RestController
29+
public class HelloController {
30+
31+
@GetMapping(path = "/test1" , headers = {"myHeader"})
32+
public String getMessageFromHeader1(
33+
@Parameter(name = "myHeader", description = "A header", schema = @Schema(allowableValues = {"foo", "bar"}))
34+
@RequestHeader("myHeader") String header
35+
) {
36+
return "bar " + header;
37+
}
38+
39+
@GetMapping("/test2")
40+
public String getMessageFromHeader2(
41+
@Parameter(name = "myHeader", description = "A header", schema = @Schema(type = "integer"))
42+
@RequestHeader("myHeader") Integer header
43+
) {
44+
return "bar " + header;
45+
}
46+
47+
@GetMapping(path = "/test3", headers = {"myHeader"})
48+
public String getMessageFromHeader3(
49+
@Parameter(name = "myHeader", description = "A header", schema = @Schema(type = "integer"))
50+
@RequestHeader("myHeader") Integer header
51+
) {
52+
return "bar " + header;
53+
}
54+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
package test.org.springdoc.api.app133;
24+
25+
import test.org.springdoc.api.AbstractSpringDocTest;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
30+
/**
31+
* Tests Spring meta-annotations as method parameters
32+
*/
33+
public class SpringDocApp133Test extends AbstractSpringDocTest {
34+
35+
@SpringBootApplication
36+
static class SpringDocTestApp {}
37+
38+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
"/test1": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "getMessageFromHeader1",
20+
"parameters": [
21+
{
22+
"name": "myHeader",
23+
"in": "header",
24+
"description": "A header",
25+
"required": true,
26+
"schema": {
27+
"type": "string",
28+
"enum": [
29+
"foo",
30+
"bar"
31+
]
32+
}
33+
}
34+
],
35+
"responses": {
36+
"200": {
37+
"description": "OK",
38+
"content": {
39+
"*/*": {
40+
"schema": {
41+
"type": "string"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
},
49+
"/test2": {
50+
"get": {
51+
"tags": [
52+
"hello-controller"
53+
],
54+
"operationId": "getMessageFromHeader2",
55+
"parameters": [
56+
{
57+
"name": "myHeader",
58+
"in": "header",
59+
"description": "A header",
60+
"required": true,
61+
"schema": {
62+
"type": "integer"
63+
}
64+
}
65+
],
66+
"responses": {
67+
"200": {
68+
"description": "OK",
69+
"content": {
70+
"*/*": {
71+
"schema": {
72+
"type": "string"
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
79+
},
80+
"/test3": {
81+
"get": {
82+
"tags": [
83+
"hello-controller"
84+
],
85+
"operationId": "getMessageFromHeader3",
86+
"parameters": [
87+
{
88+
"name": "myHeader",
89+
"in": "header",
90+
"description": "A header",
91+
"required": true,
92+
"schema": {
93+
"type": "integer"
94+
}
95+
}
96+
],
97+
"responses": {
98+
"200": {
99+
"description": "OK",
100+
"content": {
101+
"*/*": {
102+
"schema": {
103+
"type": "string"
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
111+
},
112+
"components": {}
113+
}

0 commit comments

Comments
 (0)