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;
}
Aggregations