Skip to content

Commit 81ed7a0

Browse files
committed
Merge pull request #1451 from iushankin/issue-1417
Fixed #1417: Bug in scanning The Specification Extensions
2 parents 8378133 + 798bf6a commit 81ed7a0

File tree

4 files changed

+176
-68
lines changed

4 files changed

+176
-68
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.swagger.util;
2+
3+
import io.swagger.annotations.Extension;
4+
import io.swagger.annotations.ExtensionProperty;
5+
6+
import org.apache.commons.lang3.StringUtils;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* The <code>BaseReaderUtils</code> class is utility class which helps read annotations to the Swagger.
13+
*/
14+
public final class BaseReaderUtils {
15+
16+
private BaseReaderUtils() {
17+
18+
}
19+
20+
/**
21+
* Collects extensions.
22+
*
23+
* @param extensions is an array of extensions
24+
* @return the map with extensions
25+
*/
26+
public static Map<String, Object> parseExtensions(Extension[] extensions) {
27+
final Map<String, Object> map = new HashMap<String, Object>();
28+
for (Extension extension : extensions) {
29+
final String name = extension.name();
30+
final String key = name.length() > 0 ? StringUtils.prependIfMissing(name, "x-") : name;
31+
32+
for (ExtensionProperty property : extension.properties()) {
33+
final String propertyName = property.name();
34+
final String propertyValue = property.value();
35+
if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
36+
if (key.isEmpty()) {
37+
map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
38+
} else {
39+
Object value = map.get(key);
40+
if (value == null || !(value instanceof Map)) {
41+
value = new HashMap<String, Object>();
42+
map.put(key, value);
43+
}
44+
@SuppressWarnings("unchecked")
45+
final Map<String, Object> mapValue = (Map<String, Object>) value;
46+
mapValue.put(propertyName, propertyValue);
47+
}
48+
}
49+
}
50+
}
51+
52+
return map;
53+
}
54+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package io.swagger;
2+
3+
import io.swagger.annotations.ApiOperation;
4+
import io.swagger.annotations.Extension;
5+
import io.swagger.annotations.ExtensionProperty;
6+
import io.swagger.util.BaseReaderUtils;
7+
8+
import com.google.common.collect.ImmutableMap;
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.Collections;
15+
import java.util.Map;
16+
17+
public class BaseReaderUtilsTest {
18+
19+
@DataProvider
20+
private Object[][] expectedData() {
21+
return new Object[][]{
22+
{"methodOne", Collections.emptyMap()},
23+
{"methodTwo", Collections.emptyMap()},
24+
{"methodThree", ImmutableMap.of(
25+
"x-test1", "value1",
26+
"x-test2", "value2",
27+
"x-test", ImmutableMap.of("test1", "value1", "test2", "value2"))},
28+
{"methodFour", ImmutableMap.of(
29+
"x-test", ImmutableMap.of("test1", "value1", "test2", "value2"),
30+
"x-test1", "value1",
31+
"x-test2", "value2")},
32+
{"methodFive", ImmutableMap.of(
33+
"x-test1", ImmutableMap.of("test1", "value1", "test2", "value2"),
34+
"x-test2", "value2")},
35+
{"methodSix", ImmutableMap.of("x-test1", "value1", "x-test2", "value2")}
36+
};
37+
}
38+
39+
@Test(dataProvider = "expectedData")
40+
public void extensionsTest(String methodName, Map<String, Object> expected) throws NoSuchMethodException {
41+
final Method method = getClass().getDeclaredMethod(methodName);
42+
final Extension[] extensions = method.getAnnotation(ApiOperation.class).extensions();
43+
final Map<String, Object> map = BaseReaderUtils.parseExtensions(extensions);
44+
45+
Assert.assertEquals(map, expected);
46+
}
47+
48+
@ApiOperation(value = "method")
49+
private void methodOne() {
50+
51+
}
52+
53+
@ApiOperation(value = "method", extensions = {
54+
@Extension(name = "test", properties = {
55+
@ExtensionProperty(name = "test1", value = "")
56+
})})
57+
private void methodTwo() {
58+
59+
}
60+
61+
@ApiOperation(value = "method", extensions = {
62+
@Extension(properties = {
63+
@ExtensionProperty(name = "test1", value = "value1"),
64+
@ExtensionProperty(name = "test2", value = "value2")
65+
}),
66+
@Extension(name = "test", properties = {
67+
@ExtensionProperty(name = "test1", value = "value1"),
68+
@ExtensionProperty(name = "test2", value = "value2")
69+
})})
70+
private void methodThree() {
71+
72+
}
73+
74+
@ApiOperation(value = "method", extensions = {
75+
@Extension(name = "test", properties = {
76+
@ExtensionProperty(name = "test1", value = "value1"),
77+
@ExtensionProperty(name = "test2", value = "value2")
78+
}),
79+
@Extension(properties = {
80+
@ExtensionProperty(name = "test1", value = "value1"),
81+
@ExtensionProperty(name = "test2", value = "value2")
82+
})
83+
})
84+
private void methodFour() {
85+
86+
}
87+
88+
@ApiOperation(value = "method", extensions = {
89+
@Extension(properties = {
90+
@ExtensionProperty(name = "test1", value = "value1"),
91+
@ExtensionProperty(name = "test2", value = "value2")
92+
}),
93+
@Extension(name = "test1", properties = {
94+
@ExtensionProperty(name = "test1", value = "value1"),
95+
@ExtensionProperty(name = "test2", value = "value2")
96+
})
97+
})
98+
private void methodFive() {
99+
100+
}
101+
102+
@ApiOperation(value = "method", extensions = {
103+
@Extension(name = "test1", properties = {
104+
@ExtensionProperty(name = "test1", value = "value1"),
105+
@ExtensionProperty(name = "test2", value = "value2")
106+
}),
107+
@Extension(properties = {
108+
@ExtensionProperty(name = "test1", value = "value1"),
109+
@ExtensionProperty(name = "test2", value = "value2")
110+
})
111+
})
112+
private void methodSix() {
113+
114+
}
115+
}

modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/Reader.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import io.swagger.models.properties.MapProperty;
6060
import io.swagger.models.properties.Property;
6161
import io.swagger.models.properties.RefProperty;
62+
import io.swagger.util.BaseReaderUtils;
6263
import io.swagger.util.ParameterProcessor;
6364
import io.swagger.util.PathUtils;
6465
import io.swagger.util.ReflectionUtils;
@@ -332,7 +333,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
332333
}
333334

334335
if (operation != null) {
335-
addExtensionProperties(apiOperation.extensions(), operation.getVendorExtensions());
336+
operation.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(apiOperation.extensions()));
336337
}
337338
}
338339
if (operation != null) {
@@ -458,7 +459,7 @@ protected void readSwaggerConfig(Class<?> cls, SwaggerDefinition config) {
458459
tagConfig.externalDocs().url()));
459460
}
460461

461-
addExtensionProperties(tagConfig.extensions(), tag.getVendorExtensions());
462+
tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));
462463

463464
swagger.addTag(tag);
464465
}
@@ -525,37 +526,7 @@ protected void readInfoConfig(SwaggerDefinition config) {
525526
}
526527
}
527528

528-
addExtensionProperties(infoConfig.extensions(), info.getVendorExtensions());
529-
}
530-
531-
private void addExtensionProperties(Extension[] extensions, Map<String, Object> map) {
532-
for (Extension extension : extensions) {
533-
String name = extension.name();
534-
if (name.length() > 0) {
535-
536-
if (!name.startsWith("x-")) {
537-
name = "x-" + name;
538-
}
539-
540-
if (!map.containsKey(name)) {
541-
map.put(name, new HashMap<String, Object>());
542-
}
543-
544-
map = (Map<String, Object>) map.get(name);
545-
}
546-
547-
for (ExtensionProperty property : extension.properties()) {
548-
if (!property.name().isEmpty() && !property.value().isEmpty()) {
549-
550-
String propertyName = property.name();
551-
if (name.isEmpty() && !propertyName.startsWith("x-")) {
552-
propertyName = "x-" + propertyName;
553-
}
554-
555-
map.put(propertyName, property.value());
556-
}
557-
}
558-
}
529+
info.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(infoConfig.extensions()));
559530
}
560531

561532
protected Class<?> getSubResource(Method method) {

modules/swagger-servlet/src/main/java/io/swagger/servlet/Reader.java

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.swagger.servlet;
22

3-
import io.swagger.annotations.Extension;
4-
import io.swagger.annotations.ExtensionProperty;
53
import io.swagger.annotations.Info;
64
import io.swagger.annotations.SwaggerDefinition;
75
import io.swagger.models.Contact;
@@ -16,6 +14,7 @@
1614
import io.swagger.models.parameters.Parameter;
1715
import io.swagger.servlet.extensions.ReaderExtension;
1816
import io.swagger.servlet.extensions.ReaderExtensions;
17+
import io.swagger.util.BaseReaderUtils;
1918
import io.swagger.util.PathUtils;
2019
import io.swagger.util.ReflectionUtils;
2120

@@ -166,7 +165,7 @@ private void readSwaggerConfig(SwaggerDefinition config) {
166165
tagConfig.externalDocs().url()));
167166
}
168167

169-
addExtensionProperties(tagConfig.extensions(), tag.getVendorExtensions());
168+
tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));
170169

171170
swagger.addTag(tag);
172171
}
@@ -233,37 +232,6 @@ private void readInfoConfig(SwaggerDefinition config) {
233232
}
234233
}
235234

236-
addExtensionProperties(infoConfig.extensions(), info.getVendorExtensions());
237-
}
238-
239-
@SuppressWarnings("unchecked")
240-
private void addExtensionProperties(Extension[] extensions, Map<String, Object> map) {
241-
for (Extension extension : extensions) {
242-
String name = extension.name();
243-
if (name.length() > 0) {
244-
245-
if (!name.startsWith("x-")) {
246-
name = "x-" + name;
247-
}
248-
249-
if (!map.containsKey(name)) {
250-
map.put(name, new HashMap<String, Object>());
251-
}
252-
253-
map = (Map<String, Object>) map.get(name);
254-
}
255-
256-
for (ExtensionProperty property : extension.properties()) {
257-
if (!property.name().isEmpty() && !property.value().isEmpty()) {
258-
259-
String propertyName = property.name();
260-
if (name.isEmpty() && !propertyName.startsWith("x-")) {
261-
propertyName = "x-" + propertyName;
262-
}
263-
264-
map.put(propertyName, property.value());
265-
}
266-
}
267-
}
235+
info.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(infoConfig.extensions()));
268236
}
269237
}

0 commit comments

Comments
 (0)