|
| 1 | +package io.swagger.v3.parser.processors; |
| 2 | + |
| 3 | +import java.net.URI; |
| 4 | +import java.net.URISyntaxException; |
| 5 | +import java.util.ArrayDeque; |
| 6 | +import java.util.Deque; |
| 7 | + |
| 8 | +final class ReferencePathUtils { |
| 9 | + |
| 10 | + private ReferencePathUtils() { |
| 11 | + } |
| 12 | + |
| 13 | + static boolean isAbsolute(String ref) { |
| 14 | + if (ref == null) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + if (isAbsoluteFilePath(ref)) { |
| 18 | + return true; |
| 19 | + } |
| 20 | + try { |
| 21 | + return new URI(ref).isAbsolute(); |
| 22 | + } catch (URISyntaxException e) { |
| 23 | + return hasScheme(ref); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static String resolve(String source, String ref) { |
| 28 | + if (source == null || ref == null) { |
| 29 | + return source; |
| 30 | + } |
| 31 | + try { |
| 32 | + URI sourceUri = new URI(source); |
| 33 | + if (!source.endsWith("/") && ref.startsWith("./") && "".equals(sourceUri.getPath())) { |
| 34 | + sourceUri = new URI(source + "/"); |
| 35 | + } else if ("".equals(sourceUri.getPath()) && !ref.startsWith("/")) { |
| 36 | + sourceUri = new URI(source + "/"); |
| 37 | + } |
| 38 | + |
| 39 | + URI resolved = sourceUri.resolve(new URI(ref)).normalize(); |
| 40 | + String resolvedRef = resolved.toString(); |
| 41 | + if (source.startsWith("./") && !resolved.isAbsolute() && |
| 42 | + !resolvedRef.startsWith(".") && !resolvedRef.startsWith("/")) { |
| 43 | + return "./" + resolvedRef; |
| 44 | + } |
| 45 | + return resolvedRef; |
| 46 | + } catch (URISyntaxException | IllegalArgumentException e) { |
| 47 | + return resolveRawPath(source, ref); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static String resolveRawPath(String source, String ref) { |
| 52 | + if (ref.isEmpty()) { |
| 53 | + return stripFragment(source); |
| 54 | + } |
| 55 | + if (isAbsolute(ref)) { |
| 56 | + return ref; |
| 57 | + } |
| 58 | + if (ref.startsWith("#")) { |
| 59 | + return stripFragment(source) + ref; |
| 60 | + } |
| 61 | + if (ref.startsWith("?")) { |
| 62 | + return stripQueryAndFragment(source) + ref; |
| 63 | + } |
| 64 | + |
| 65 | + String sourceFile = stripQueryAndFragment(source); |
| 66 | + int lastSeparator = Math.max(sourceFile.lastIndexOf('/'), sourceFile.lastIndexOf('\\')); |
| 67 | + if (lastSeparator == -1) { |
| 68 | + return normalizeRelativePath(ref); |
| 69 | + } |
| 70 | + |
| 71 | + String resolved = sourceFile.substring(0, lastSeparator + 1) + ref; |
| 72 | + if (hasScheme(sourceFile)) { |
| 73 | + return resolved; |
| 74 | + } |
| 75 | + return normalizeRelativePath(resolved); |
| 76 | + } |
| 77 | + |
| 78 | + private static String normalizeRelativePath(String ref) { |
| 79 | + int suffixStart = suffixStart(ref); |
| 80 | + String path = suffixStart == -1 ? ref : ref.substring(0, suffixStart); |
| 81 | + String suffix = suffixStart == -1 ? "" : ref.substring(suffixStart); |
| 82 | + boolean leadingDotSlash = path.startsWith("./"); |
| 83 | + boolean leadingSlash = path.startsWith("/"); |
| 84 | + Deque<String> segments = new ArrayDeque<>(); |
| 85 | + |
| 86 | + for (String segment : path.split("/")) { |
| 87 | + if (segment.isEmpty() || ".".equals(segment)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + if ("..".equals(segment)) { |
| 91 | + if (!segments.isEmpty() && !"..".equals(segments.peekLast())) { |
| 92 | + segments.removeLast(); |
| 93 | + } else if (!leadingSlash) { |
| 94 | + segments.addLast(segment); |
| 95 | + } |
| 96 | + } else { |
| 97 | + segments.addLast(segment); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + String normalized = String.join("/", segments); |
| 102 | + if (leadingSlash) { |
| 103 | + normalized = "/" + normalized; |
| 104 | + } else if (leadingDotSlash && !normalized.startsWith("..") && !normalized.isEmpty()) { |
| 105 | + normalized = "./" + normalized; |
| 106 | + } |
| 107 | + return normalized + suffix; |
| 108 | + } |
| 109 | + |
| 110 | + private static boolean isAbsoluteFilePath(String ref) { |
| 111 | + return ref.startsWith("/") || ref.startsWith("\\") || |
| 112 | + (ref.length() >= 3 && Character.isLetter(ref.charAt(0)) && ref.charAt(1) == ':' && |
| 113 | + (ref.charAt(2) == '/' || ref.charAt(2) == '\\')); |
| 114 | + } |
| 115 | + |
| 116 | + private static boolean hasScheme(String ref) { |
| 117 | + int colon = ref.indexOf(':'); |
| 118 | + if (colon <= 0 || !Character.isLetter(ref.charAt(0))) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + for (int i = 1; i < colon; i++) { |
| 122 | + char character = ref.charAt(i); |
| 123 | + if (!Character.isLetterOrDigit(character) && character != '+' && character != '-' && character != '.') { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + private static String stripFragment(String value) { |
| 131 | + int fragment = value.indexOf('#'); |
| 132 | + return fragment == -1 ? value : value.substring(0, fragment); |
| 133 | + } |
| 134 | + |
| 135 | + private static String stripQueryAndFragment(String value) { |
| 136 | + int suffixStart = suffixStart(value); |
| 137 | + return suffixStart == -1 ? value : value.substring(0, suffixStart); |
| 138 | + } |
| 139 | + |
| 140 | + private static int suffixStart(String value) { |
| 141 | + int query = value.indexOf('?'); |
| 142 | + int fragment = value.indexOf('#'); |
| 143 | + if (query == -1) { |
| 144 | + return fragment; |
| 145 | + } |
| 146 | + if (fragment == -1) { |
| 147 | + return query; |
| 148 | + } |
| 149 | + return Math.min(query, fragment); |
| 150 | + } |
| 151 | +} |
0 commit comments