Search in sources :

Example 1 with ExtensionProperty

use of io.swagger.v3.oas.annotations.extensions.ExtensionProperty in project swagger-core by swagger-api.

the class AnnotationsUtils method getExtensions.

public static Map<String, Object> getExtensions(Extension... extensions) {
    final Map<String, Object> 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();
            JsonNode processedValue = null;
            final boolean propertyAsJson = property.parseValue();
            if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
                if (key.isEmpty()) {
                    if (propertyAsJson) {
                        try {
                            processedValue = Json.mapper().readTree(propertyValue);
                            map.put(StringUtils.prependIfMissing(propertyName, "x-"), processedValue);
                        } catch (Exception e) {
                            map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
                        }
                    } else {
                        map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
                    }
                } else {
                    Object value = map.get(key);
                    if (!(value instanceof Map)) {
                        value = new HashMap<String, Object>();
                        map.put(key, value);
                    }
                    @SuppressWarnings("unchecked") final Map<String, Object> mapValue = (Map<String, Object>) value;
                    if (propertyAsJson) {
                        try {
                            processedValue = Json.mapper().readTree(propertyValue);
                            mapValue.put(propertyName, processedValue);
                        } catch (Exception e) {
                            mapValue.put(propertyName, propertyValue);
                        }
                    } else {
                        mapValue.put(propertyName, propertyValue);
                    }
                }
            }
        }
    }
    return map;
}
Also used : Extension(io.swagger.v3.oas.annotations.extensions.Extension) ExtensionProperty(io.swagger.v3.oas.annotations.extensions.ExtensionProperty) HashMap(java.util.HashMap) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Extension (io.swagger.v3.oas.annotations.extensions.Extension)1 ExtensionProperty (io.swagger.v3.oas.annotations.extensions.ExtensionProperty)1 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1