Skip to content

Commit

Permalink
refs #1620 - close stream after reading file
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed May 9, 2022
1 parent 2409bd4 commit e094c6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import io.swagger.models.auth.AuthorizationValue;
import io.swagger.models.refs.RefFormat;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
Expand Down Expand Up @@ -138,7 +140,9 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
final Path pathToUse = parentDirectory.resolve(file).normalize();

if(Files.exists(pathToUse)) {
result = IOUtils.toString(new FileInputStream(pathToUse.toFile()), "UTF-8");
try(InputStream inputStream = new FileInputStream(pathToUse.toFile())){
result = IOUtils.toString(inputStream, "UTF-8");
}
} else {
String url = file;
if(url.contains("..")) {
Expand All @@ -149,7 +153,9 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
final Path pathToUse2 = parentDirectory.resolve(url).normalize();

if(Files.exists(pathToUse2)) {
result = IOUtils.toString(new FileInputStream(pathToUse2.toFile()), "UTF-8");
try(InputStream inputStream = new FileInputStream(pathToUse2.toFile())){
result = IOUtils.toString(inputStream, "UTF-8");
}
}
}
if (result == null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public void testReadExternalRef_RelativeFileFormat(@Injectable final List<Author
IOUtils.toString(fileInputStream, "UTF-8");
times = 1;
result = expectedResult;
fileInputStream.close();
times = 1;

}};

String actualResult = RefUtils.readExternalRef(filePath, RefFormat.RELATIVE, auths, parentDirectory);
Expand Down

0 comments on commit e094c6a

Please sign in to comment.