Skip to content

Commit ad6e87f

Browse files
authored
Merge pull request #3175 from alesavin/master
AnnotationsUtils: API response header can be processed with only name…
2 parents cefc0b4 + 9985ac3 commit ad6e87f

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ public static Optional<Header> getHeader(io.swagger.v3.oas.annotations.headers.H
895895
}
896896

897897
Header headerObject = new Header();
898-
boolean isEmpty = true;
898+
boolean isEmpty = !StringUtils.isNotBlank(header.name());
899899
if (StringUtils.isNotBlank(header.description())) {
900900
headerObject.setDescription(header.description());
901901
isEmpty = false;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.swagger.v3.core.resolving;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import io.swagger.v3.core.util.AnnotationsUtils;
5+
import io.swagger.v3.oas.annotations.Operation;
6+
import io.swagger.v3.oas.annotations.headers.Header;
7+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
8+
import io.swagger.v3.oas.models.headers.Header.StyleEnum;
9+
import org.testng.Assert;
10+
import org.testng.annotations.DataProvider;
11+
import org.testng.annotations.Test;
12+
13+
import java.lang.reflect.Method;
14+
import java.util.Arrays;
15+
import java.util.Map;
16+
import java.util.Optional;
17+
18+
public class AnnotationsUtilsHeadersTest {
19+
20+
private io.swagger.v3.oas.models.headers.Header header() {
21+
return new io.swagger.v3.oas.models.headers.Header().style(StyleEnum.SIMPLE);
22+
}
23+
24+
@DataProvider
25+
private Object[][] expectedData() {
26+
return new Object[][]{
27+
{"methodOne", Optional.empty()},
28+
{"methodTwo", Optional.empty()},
29+
{"methodThree", Optional.empty()},
30+
{"methodFour", Optional.of(ImmutableMap.of(
31+
"", header().description("header"),
32+
"header1", header(),
33+
"header2", header().description("header 2"),
34+
"header3", header().$ref("#/components/schemas/header3")
35+
))},
36+
};
37+
}
38+
39+
@Test(dataProvider = "expectedData")
40+
public void extensionsTest(String methodName,
41+
Optional<Map<String, io.swagger.v3.oas.models.headers.Header>> expected)
42+
throws NoSuchMethodException {
43+
final Method method = getClass().getDeclaredMethod(methodName);
44+
final Header[] headers =
45+
Arrays.stream(method.getAnnotation(Operation.class).responses())
46+
.flatMap(response -> Arrays.stream(response.headers())).toArray(Header[]::new);
47+
48+
final Optional<Map<String, io.swagger.v3.oas.models.headers.Header>> optionalMap =
49+
AnnotationsUtils.getHeaders(headers, null);
50+
51+
Assert.assertEquals(optionalMap, expected);
52+
}
53+
54+
@Operation(description = "method")
55+
private void methodOne() {
56+
57+
}
58+
59+
@Operation(description = "method", responses = {
60+
@ApiResponse()
61+
})
62+
private void methodTwo() {
63+
64+
}
65+
66+
@Operation(description = "method", responses = {
67+
@ApiResponse(headers = {
68+
@Header(name = "")
69+
})
70+
})
71+
private void methodThree() {
72+
73+
}
74+
75+
@Operation(description = "method", responses = {
76+
@ApiResponse(headers = {
77+
@Header(name = "", description = "header"),
78+
@Header(name = "header1"),
79+
@Header(name = "header2", description = "header 2"),
80+
@Header(name = "header3", ref = "#/components/schemas/header3")
81+
})
82+
})
83+
private void methodFour() {
84+
85+
}
86+
}

0 commit comments

Comments
 (0)