Search in sources :

Example 1 with RouterFactoryException

use of io.vertx.ext.web.api.contract.RouterFactoryException in project vertx-web by vert-x3.

the class OpenAPI3PathResolver method solve.

public Pattern solve() {
    // Filter parameters to get only path parameters
    parameters = parameters.stream().filter(parameter -> parameter.getIn().equals("path")).collect(Collectors.toList());
    if (ILLEGAL_PATH_MATCHER.matcher(oasPath).matches())
        throw new RouterFactoryException("Path template not supported", RouterFactoryException.ErrorType.INVALID_SPEC_PATH);
    Matcher parametersMatcher = OAS_PATH_PARAMETERS_PATTERN.matcher(oasPath);
    if (!parameters.isEmpty() && parametersMatcher.find()) {
        // Need to create a specific match pattern with path parameters and some string manipulation magic
        StringBuilder regex = new StringBuilder();
        int lastMatchEnd = 0;
        boolean endSlash = oasPath.charAt(oasPath.length() - 1) == '/';
        parametersMatcher.reset();
        int i = 0;
        while (parametersMatcher.find()) {
            // Append constant string
            regex.append(Pattern.quote(oasPath.substring(lastMatchEnd, parametersMatcher.start())));
            lastMatchEnd = parametersMatcher.end();
            String paramName = parametersMatcher.group(1);
            Optional<Parameter> parameterOptional = parameters.stream().filter(p -> p.getName().equals(paramName)).findFirst();
            if (parameterOptional.isPresent()) {
                // For every parameter style I have to generate a different regular expression
                Parameter parameter = parameterOptional.get();
                String style = (parameter.getStyle() != null) ? parameter.getStyle().toString() : "simple";
                boolean explode = (parameter.getExplode() != null) ? parameter.getExplode() : false;
                boolean isObject = OpenApi3Utils.isParameterObjectOrAllOfType(parameter);
                boolean isArray = OpenApi3Utils.isParameterArrayType(parameter);
                String groupName = "p" + i;
                if (style.equals("simple")) {
                    regex.append("(?<" + groupName + ">[^\\/\\;\\?\\:\\@\\&\\.\\\"\\<\\>\\#\\{\\}\\|\\\\\\^\\~\\[\\]\\`]*)?");
                    mappedGroups.put(groupName, paramName);
                } else if (style.equals("label")) {
                    if (isObject && explode) {
                        Map<String, OpenApi3Utils.ObjectField> properties = OpenApi3Utils.solveObjectParameters(parameter.getSchema());
                        for (Map.Entry<String, OpenApi3Utils.ObjectField> entry : properties.entrySet()) {
                            groupName = "p" + i;
                            regex.append("\\.?" + entry.getKey() + "=(?<" + groupName + ">[^\\/\\;\\?\\:\\@\\=\\.\\&\\\"\\<\\>\\#\\{\\}\\|\\\\\\^\\~\\[\\]\\`]*)");
                            mappedGroups.put(groupName, entry.getKey());
                            i++;
                        }
                    } else {
                        regex.append("\\.?(?<" + groupName + ">[^\\/\\;\\?\\:\\@\\=\\&\\\"\\<\\>\\#\\{\\}\\|\\\\\\^\\~\\[\\]\\`]*)?");
                        mappedGroups.put(groupName, paramName);
                    }
                } else if (style.equals("matrix")) {
                    if (isObject && explode) {
                        Map<String, OpenApi3Utils.ObjectField> properties = OpenApi3Utils.solveObjectParameters(parameter.getSchema());
                        for (Map.Entry<String, OpenApi3Utils.ObjectField> entry : properties.entrySet()) {
                            groupName = "p" + i;
                            regex.append("\\;" + entry.getKey() + "=(?<" + groupName + ">[^\\/\\;\\?\\:\\@\\=\\.\\&\\\"\\<\\>\\#\\{\\}\\|\\\\\\^\\~\\[\\]\\`]*)");
                            mappedGroups.put(groupName, entry.getKey());
                            i++;
                        }
                    } else if (isArray && explode) {
                        regex.append("(?<" + groupName + ">(?>;" + paramName + "=[^\\/\\;\\?\\:\\@\\&\\\"\\<\\>\\#\\{\\}\\|\\\\\\^\\~\\[\\]\\`]*)+)");
                        mappedGroups.put(groupName, paramName);
                    } else {
                        regex.append(";" + paramName + "=(?<" + groupName + ">[^\\/\\;\\?\\:\\@\\=\\&\\\"\\<\\>\\#\\{\\}\\|\\\\\\^\\~\\[\\]\\`]*)?");
                        mappedGroups.put(groupName, paramName);
                    }
                }
            } else {
                throw RouterFactoryException.createSpecInvalidException("Missing parameter description for parameter name: " + paramName);
            }
            i++;
        }
        regex.append(Pattern.quote(oasPath.substring(lastMatchEnd, (endSlash) ? oasPath.length() - 1 : oasPath.length())));
        if (endSlash)
            regex.append(QUERY_REGEX_WITH_SLASH);
        else
            regex.append(QUERY_REGEX_WITHOUT_SLASH);
        resolvedPattern = Pattern.compile(regex.toString());
    } else {
        resolvedPattern = Pattern.compile(Pattern.quote(oasPath));
    }
    return resolvedPattern;
}
Also used : List(java.util.List) Matcher(java.util.regex.Matcher) Map(java.util.Map) Parameter(io.swagger.v3.oas.models.parameters.Parameter) RouterFactoryException(io.vertx.ext.web.api.contract.RouterFactoryException) Optional(java.util.Optional) HashMap(java.util.HashMap) Pattern(java.util.regex.Pattern) Collectors(java.util.stream.Collectors) Matcher(java.util.regex.Matcher) RouterFactoryException(io.vertx.ext.web.api.contract.RouterFactoryException) Parameter(io.swagger.v3.oas.models.parameters.Parameter) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 RouterFactoryException (io.vertx.ext.web.api.contract.RouterFactoryException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1