Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.swagger.util;

import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
* The <code>BaseReaderUtils</code> class is utility class which helps read annotations to the Swagger.
*/
public final class BaseReaderUtils {

private BaseReaderUtils() {

}

/**
* Collects extensions.
*
* @param extensions is an array of extensions
* @return the map with extensions
*/
public static Map<String, Object> parseExtensions(Extension[] extensions) {
final Map<String, Object> map = new HashMap<String, Object>();
for (Extension extension : extensions) {
final String name = extension.name();
final String key = name.length() > 0 ? StringUtils.prependIfMissing(name, "x-") : name;

for (ExtensionProperty property : extension.properties()) {
final String propertyName = property.name();
final String propertyValue = property.value();
if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
if (key.isEmpty()) {
map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
} else {
Object value = map.get(key);
if (value == null || !(value instanceof Map)) {
value = new HashMap<String, Object>();
map.put(key, value);
}
@SuppressWarnings("unchecked")
final Map<String, Object> mapValue = (Map<String, Object>) value;
mapValue.put(propertyName, propertyValue);
}
}
}
}

return map;
}
}
115 changes: 115 additions & 0 deletions modules/swagger-core/src/test/java/io/swagger/BaseReaderUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package io.swagger;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.util.BaseReaderUtils;

import com.google.common.collect.ImmutableMap;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;

public class BaseReaderUtilsTest {

@DataProvider
private Object[][] expectedData() {
return new Object[][]{
{"methodOne", Collections.emptyMap()},
{"methodTwo", Collections.emptyMap()},
{"methodThree", ImmutableMap.of(
"x-test1", "value1",
"x-test2", "value2",
"x-test", ImmutableMap.of("test1", "value1", "test2", "value2"))},
{"methodFour", ImmutableMap.of(
"x-test", ImmutableMap.of("test1", "value1", "test2", "value2"),
"x-test1", "value1",
"x-test2", "value2")},
{"methodFive", ImmutableMap.of(
"x-test1", ImmutableMap.of("test1", "value1", "test2", "value2"),
"x-test2", "value2")},
{"methodSix", ImmutableMap.of("x-test1", "value1", "x-test2", "value2")}
};
}

@Test(dataProvider = "expectedData")
public void extensionsTest(String methodName, Map<String, Object> expected) throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod(methodName);
final Extension[] extensions = method.getAnnotation(ApiOperation.class).extensions();
final Map<String, Object> map = BaseReaderUtils.parseExtensions(extensions);

Assert.assertEquals(map, expected);
}

@ApiOperation(value = "method")
private void methodOne() {

}

@ApiOperation(value = "method", extensions = {
@Extension(name = "test", properties = {
@ExtensionProperty(name = "test1", value = "")
})})
private void methodTwo() {

}

@ApiOperation(value = "method", extensions = {
@Extension(properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
}),
@Extension(name = "test", properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
})})
private void methodThree() {

}

@ApiOperation(value = "method", extensions = {
@Extension(name = "test", properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
}),
@Extension(properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
})
})
private void methodFour() {

}

@ApiOperation(value = "method", extensions = {
@Extension(properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
}),
@Extension(name = "test1", properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
})
})
private void methodFive() {

}

@ApiOperation(value = "method", extensions = {
@Extension(name = "test1", properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
}),
@Extension(properties = {
@ExtensionProperty(name = "test1", value = "value1"),
@ExtensionProperty(name = "test2", value = "value2")
})
})
private void methodSix() {

}
}
39 changes: 5 additions & 34 deletions modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.util.BaseReaderUtils;
import io.swagger.util.ParameterProcessor;
import io.swagger.util.PathUtils;
import io.swagger.util.ReflectionUtils;
Expand Down Expand Up @@ -264,7 +265,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
String httpMethod = extractOperationMethod(apiOperation, method, SwaggerExtensions.chain());

Operation operation = null;
if(apiOperation != null || config.isScanAllResources() || httpMethod != null || methodPath != null) {
if(apiOperation != null || config.isScanAllResources() || httpMethod != null || methodPath != null) {
operation = parseMethod(cls, method, globalParameters);
}
if (operation == null) {
Expand Down Expand Up @@ -332,7 +333,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
}

if (operation != null) {
addExtensionProperties(apiOperation.extensions(), operation.getVendorExtensions());
operation.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(apiOperation.extensions()));
}
}
if (operation != null) {
Expand Down Expand Up @@ -458,7 +459,7 @@ protected void readSwaggerConfig(Class<?> cls, SwaggerDefinition config) {
tagConfig.externalDocs().url()));
}

addExtensionProperties(tagConfig.extensions(), tag.getVendorExtensions());
tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));

swagger.addTag(tag);
}
Expand Down Expand Up @@ -525,37 +526,7 @@ protected void readInfoConfig(SwaggerDefinition config) {
}
}

addExtensionProperties(infoConfig.extensions(), info.getVendorExtensions());
}

private void addExtensionProperties(Extension[] extensions, Map<String, Object> map) {
for (Extension extension : extensions) {
String name = extension.name();
if (name.length() > 0) {

if (!name.startsWith("x-")) {
name = "x-" + name;
}

if (!map.containsKey(name)) {
map.put(name, new HashMap<String, Object>());
}

map = (Map<String, Object>) map.get(name);
}

for (ExtensionProperty property : extension.properties()) {
if (!property.name().isEmpty() && !property.value().isEmpty()) {

String propertyName = property.name();
if (name.isEmpty() && !propertyName.startsWith("x-")) {
propertyName = "x-" + propertyName;
}

map.put(propertyName, property.value());
}
}
}
info.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(infoConfig.extensions()));
}

protected Class<?> getSubResource(Method method) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.swagger.servlet;

import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.Info;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.models.Contact;
Expand All @@ -16,6 +14,7 @@
import io.swagger.models.parameters.Parameter;
import io.swagger.servlet.extensions.ReaderExtension;
import io.swagger.servlet.extensions.ReaderExtensions;
import io.swagger.util.BaseReaderUtils;
import io.swagger.util.PathUtils;
import io.swagger.util.ReflectionUtils;

Expand Down Expand Up @@ -166,7 +165,7 @@ private void readSwaggerConfig(SwaggerDefinition config) {
tagConfig.externalDocs().url()));
}

addExtensionProperties(tagConfig.extensions(), tag.getVendorExtensions());
tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));

swagger.addTag(tag);
}
Expand Down Expand Up @@ -233,37 +232,6 @@ private void readInfoConfig(SwaggerDefinition config) {
}
}

addExtensionProperties(infoConfig.extensions(), info.getVendorExtensions());
}

@SuppressWarnings("unchecked")
private void addExtensionProperties(Extension[] extensions, Map<String, Object> map) {
for (Extension extension : extensions) {
String name = extension.name();
if (name.length() > 0) {

if (!name.startsWith("x-")) {
name = "x-" + name;
}

if (!map.containsKey(name)) {
map.put(name, new HashMap<String, Object>());
}

map = (Map<String, Object>) map.get(name);
}

for (ExtensionProperty property : extension.properties()) {
if (!property.name().isEmpty() && !property.value().isEmpty()) {

String propertyName = property.name();
if (name.isEmpty() && !propertyName.startsWith("x-")) {
propertyName = "x-" + propertyName;
}

map.put(propertyName, property.value());
}
}
}
info.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(infoConfig.extensions()));
}
}