Getting NullPointerException when URI does not have scheme
Xavier-Pisco opened this issue · 6 comments
Hi,
When trying to use the parse from an URI I'm getting a NPE because the URI doesn't have a scheme.
public static ExtendedJsonContext parse(URI uri) throws IOException {
if("classpath".contentEquals(uri.getScheme())) { // <-- Here uri.getScheme() returns null which throws an NPE on String.contentEquals()
try(var inputStream = resourceClassLoader.getResourceAsStream(uri.getPath().replaceFirst("^/", ""))) {
return parse(inputStream, uri);
}
}
// TODO: it does not support yet parsing http/https files directly
try(var inputStream = new FileInputStream(new File(uri))) {
return parse(inputStream, uri);
}
}I don't know if I'm doing something wrong and this isn't supposed to ever happen or if this is a bug.
Either way, I think an easy solution would be to change the if to something similar to:
if (uri.getScheme() && "classpath".contentEquals(uri.getScheme()))This should be enough to avoid this error.
how can I reproduce this? what URI are you setting?
I'm using this code URI.create("folder/file.json").
have you tried URI.create("file:folder/file.json") or URI.create("file:./folder/file.json") ?
That gave me another error java.lang.IllegalArgumentException: URI is not hierarchical.
From the File.java class, I could find that this happened because the path to the file was not absolute. I don't know if this should be something forced but if I used file:///home/.../folder/file.json this works.
Thank you for the help 😄 , this solves the problem for me.
ok,
I will try to make a release with the fix that you proposed..
For any one with this issue, unless you are parsing a classpath resource, for better control use a File instead of a URI
File file = new File("src/test/resources/openapi/http-external-refs.yml");
$RefParser parser = new $RefParser(file)because even checking for null schema in URI it will internally convert to new File(uri)