Skip to content

Commit 7b7984d

Browse files
committed
Fix relative links going higher than spec itself
1 parent 557ce57 commit 7b7984d

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

boat-engine/src/main/java/com/backbase/oss/boat/transformers/bundler/ExamplesProcessor.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public void processMediaType(MediaType mediaType, String relativePath, boolean d
119119
}
120120

121121
private void putComponentExample(String key, Example example) {
122+
log.debug("Put Component Example: [{}], [{}]", key, example);
122123
getComponentExamplesFromOpenAPI().put(key, example);
123124
}
124125

@@ -155,7 +156,6 @@ private void fixInlineExamples(ExampleHolder exampleHolder, String relativePath,
155156
return;
156157
}
157158

158-
URI resolvedUri;
159159
Optional<String> fragment;
160160
if (refPath.contains("#") && !refPath.startsWith("#/components/examples")) {
161161
fragment = Optional.of(StringUtils.substringAfter(refPath, "#"));
@@ -164,11 +164,12 @@ private void fixInlineExamples(ExampleHolder exampleHolder, String relativePath,
164164
fragment = Optional.empty();
165165
}
166166

167-
resolvedUri = resolveUri(relativePath, refPath);
167+
URI resolvedUri = resolveUri(relativePath, refPath);
168168
try {
169169
String content = readContent(Paths.get(resolvedUri));
170170

171171
if (fragment.isPresent()) {
172+
log.debug("Read content from [{}], finding fragment [{}]", resolvedUri, fragment.get());
172173
String exampleName = StringUtils.substringAfterLast(fragment.get(), "/");
173174
// resolve fragment from json node
174175
JsonNode jsonNode = yamlObjectMapper.readTree(content);
@@ -178,19 +179,22 @@ private void fixInlineExamples(ExampleHolder exampleHolder, String relativePath,
178179
processInLineExample(exampleHolder,relativePath,exampleNode, exampleName);
179180

180181
} else {
182+
log.debug("Read content from [{}], set content and dereference", resolvedUri);
181183
exampleHolder.setContent(content);
182184
dereferenceExample(exampleHolder);
183185
}
184186
if (derefenceExamples) {
187+
log.debug("Read content from [{}], not setting content and dereference because...", resolvedUri);
185188
dereferenceExample(exampleHolder);
186189
}
187190

188191
} catch (IOException e) {
189-
throw new TransformerException("Unable to fix inline examples", e);
192+
throw new TransformerException("Unable to fix inline examples from " + resolvedUri, e);
190193
}
191194
}
192195

193196
private void processInLineExample(ExampleHolder exampleHolder, String relativePath, JsonNode exampleNode, String exampleName) throws IOException {
197+
log.debug("Process inline example [{}], [{}], [{}], [{}]", exampleHolder, relativePath, exampleNode, exampleName);
194198
if (exampleNode.has("$ref")) {
195199
String refPath = exampleNode.get("$ref").asText();
196200
exampleHolder.replaceRef(refPath);
@@ -227,17 +231,23 @@ private String readContent(Path path) throws IOException {
227231
}
228232

229233
private URI resolveUri(String relativePath, String refPath) {
230-
URI resolvedUri;
231234
if (relativePath == null) {
232-
resolvedUri = rootUri.resolve(StringUtils.strip(refPath, "./"));
235+
return rootUri.resolve(removeTrainingDotSlash(refPath));
233236
} else {
234-
resolvedUri = rootUri.resolve(checkTrailingSlash(relativePath.replace("\\", "/")))
237+
return rootUri.resolve(checkTrailingSlash(relativePath.replace("\\", "/")))
235238
.resolve(refPath.replace("\\", "/"));
236239
}
237-
return resolvedUri;
240+
}
241+
242+
private String removeTrainingDotSlash(String path) {
243+
if (StringUtils.startsWith(path,"./")) {
244+
return StringUtils.substring(path, 2);
245+
}
246+
return path;
238247
}
239248

240249
private void dereferenceExample(ExampleHolder exampleHolder) {
250+
log.debug("dereferenceExample: '{}'", exampleHolder);
241251
String rootName = exampleHolder.getExampleName();
242252
int count = 0;
243253
while (existsButNotMatching(cache.get(makeCountedName(rootName, count)), exampleHolder)) {

boat-engine/src/test/java/com/backbase/oss/boat/transformers/BundlerTests.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
import io.swagger.v3.oas.models.responses.ApiResponse;
1717
import io.swagger.v3.oas.models.responses.ApiResponses;
1818
import io.swagger.v3.parser.models.RefFormat;
19+
import lombok.extern.slf4j.Slf4j;
20+
import org.hamcrest.BaseMatcher;
21+
import org.hamcrest.Description;
22+
import org.junit.jupiter.api.Test;
23+
1924
import java.io.File;
2025
import java.io.IOException;
2126
import java.net.URISyntaxException;
@@ -26,12 +31,9 @@
2631
import java.util.Collections;
2732
import java.util.Map;
2833
import java.util.function.Function;
29-
import lombok.extern.slf4j.Slf4j;
30-
import org.hamcrest.BaseMatcher;
31-
import org.hamcrest.Description;
32-
import org.junit.jupiter.api.Test;
3334

34-
import static org.hamcrest.CoreMatchers.*;
35+
import static org.hamcrest.CoreMatchers.is;
36+
import static org.hamcrest.CoreMatchers.notNullValue;
3537
import static org.hamcrest.MatcherAssert.assertThat;
3638
import static org.hamcrest.Matchers.nullValue;
3739
import static org.junit.jupiter.api.Assertions.*;
@@ -87,7 +89,7 @@ void testBundleExamples() throws OpenAPILoaderException, IOException {
8789
@Test
8890
void testBundleHttp() throws OpenAPILoaderException, IOException {
8991

90-
String url = "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/refs/heads/main/tests/v3.0/pass/petstore.yaml";
92+
String url = "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/refs/heads/main/_archive_/schemas/v3.0/pass//petstore.yaml";
9193
OpenAPI openAPI = OpenAPILoader.load(url);
9294
OpenAPI openAPIUnproccessed = openAPI;
9395

@@ -105,8 +107,9 @@ void testBundleNonExistingFiles() throws OpenAPILoaderException, IOException {
105107
try {
106108
bundler.transform(openAPI, Collections.EMPTY_MAP);
107109
fail("Expected TransformerException");
108-
}catch (TransformerException e){
109-
assertEquals("Unable to fix inline examples",e.getMessage());
110+
} catch (TransformerException e){
111+
assertTrue(e.getMessage().startsWith("Unable to fix inline examples from file"));
112+
assertTrue(e.getMessage().endsWith("notexist.json"));
110113
}
111114

112115
}

0 commit comments

Comments
 (0)