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

org.openl.rules.project.openapi.OpenAPIRefResolver Maven / Gradle / Ivy

The newest version!
package org.openl.rules.project.openapi;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.swagger.v3.oas.models.OpenAPI;

import org.openl.util.ClassUtils;

public class OpenAPIRefResolver {
    private final Map resolvedByRefCache = new HashMap<>();
    private final OpenAPI openAPI;

    public OpenAPIRefResolver(OpenAPI openAPI) {
        this.openAPI = Objects.requireNonNull(openAPI, "openAPI cannot be null");
    }

    public Object resolve(String ref) {
        return resolveByRef(ref, () -> {
        });
    }

    public  T resolve(T obj, Function getRefFunc) {
        return resolve(obj, getRefFunc, () -> {
        });
    }

    @SuppressWarnings("unchecked")
    public  T resolve(T obj, Function getRefFunc, Runnable retNotFoundFunc) {
        if (obj != null && getRefFunc.apply(obj) != null) {
            return resolve((T) resolveByRef(getRefFunc.apply(obj), retNotFoundFunc), getRefFunc);
        }
        return obj;
    }

    private Object resolveByRef(String ref, Runnable retNotFoundFunc) {
        if (resolvedByRefCache.containsKey(ref)) {
            return resolvedByRefCache.get(ref);
        }
        String expression = ref.substring(1);
        String[] expressionParts = expression.split("(?=/)");
        Object resolvedByRef = openAPI;
        try {
            for (String expressionPart : Arrays.stream(expressionParts)
                    .map(e -> e.substring(1))
                    .collect(Collectors.toList())) {
                if (resolvedByRef != null) {
                    try {
                        resolvedByRef = ClassUtils.get(resolvedByRef, expressionPart);
                    } catch (Exception e) {
                        if (Map.class.isAssignableFrom(resolvedByRef.getClass())) {
                            resolvedByRef = ((Map) resolvedByRef).get(expressionPart);
                        } else {
                            resolvedByRef = null;
                        }
                    }
                }
            }
        } catch (Exception e) {
            resolvedByRef = null;
        }
        if (resolvedByRef != openAPI && resolvedByRef != null) {
            resolvedByRefCache.put(ref, resolvedByRef);
            return resolvedByRef;
        } else {
            resolvedByRefCache.put(ref, null);
            retNotFoundFunc.run();
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy