Search in sources :

Example 1 with Extension

use of io.swagger.annotations.Extension in project swagger-core by swagger-api.

the class BaseReaderUtilsTest method extensionsTest.

@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);
}
Also used : Extension(io.swagger.annotations.Extension) ApiOperation(io.swagger.annotations.ApiOperation) Method(java.lang.reflect.Method) Test(org.testng.annotations.Test)

Example 2 with Extension

use of io.swagger.annotations.Extension in project swagger-core by swagger-api.

the class BaseReaderUtils method parseExtensions.

/**
     * 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;
}
Also used : Extension(io.swagger.annotations.Extension) ExtensionProperty(io.swagger.annotations.ExtensionProperty) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Extension (io.swagger.annotations.Extension)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ExtensionProperty (io.swagger.annotations.ExtensionProperty)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1