All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.swagger.util.BaseReaderUtils Maven / Gradle / Ivy

The newest version!
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 BaseReaderUtils 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 parseExtensions(Extension[] extensions) {
        final Map map = new HashMap();
        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();
                            map.put(key, value);
                        }
                        @SuppressWarnings("unchecked")
                        final Map mapValue = (Map) value;
                        mapValue.put(propertyName, propertyValue);
                    }
                }
            }
        }

        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy