Skip to content

Conversation

@allen-ball
Copy link
Contributor

Extended OpenAPIV3Parser.readLocation() to read content from a jar:jarfile-uri-spec!/spec.yml styled URL as described in #1592. Additional test included.

@jhemelhof
Copy link

I've tried this PR as it's a feature we'd like to use, but I get a NPE if the Swagger contains a reference to another schema within the JAR

io.swagger.v3.parser.OpenAPIV3Parser - Exception while resolving:
java.lang.NullPointerException: null
	at io.swagger.v3.parser.util.PathUtils.getParentDirectoryOfFile(PathUtils.java:30)
	at io.swagger.v3.parser.ResolverCache.<init>(ResolverCache.java:76)
	at io.swagger.v3.parser.OpenAPIResolver.<init>(OpenAPIResolver.java:38)
	at io.swagger.v3.parser.OpenAPIResolver.<init>(OpenAPIResolver.java:32)
	at io.swagger.v3.parser.OpenAPIV3Parser.resolve(OpenAPIV3Parser.java:176)
	at io.swagger.v3.parser.OpenAPIV3Parser.readContents(OpenAPIV3Parser.java:156)
	at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:91)
	at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)

The actual exception leading up to it is an InvalidPathException (due to a ":" in the path) thrown in PathUtils that is ignored. Tweaking the ResolverCache constructor to not set a parentDirectory for jars seems to be enough to work around that:

        if(parentFileLocation != null) {
            if(parentFileLocation.startsWith("http") || parentFileLocation.startsWith("jar")) {
                parentDirectory = null;
            } else {
                parentDirectory = PathUtils.getParentDirectoryOfFile(parentFileLocation);
            }
        }

as during the loading of references it'll use the root path to load an external reference:

            if(parentDirectory != null) {
                contents = RefUtils.readExternalRef(file, refFormat, auths, parentDirectory);
            }
            else if(rootPath != null && rootPath.startsWith("http")) {
                contents = RefUtils.readExternalUrlRef(file, refFormat, auths, rootPath);
            }
            else if (rootPath != null) {
                contents = RefUtils.readExternalClasspathRef(file, refFormat, auths, rootPath);
            }

@allen-ball
Copy link
Contributor Author

Thank you. I've made your suggested change and the tests ran successfully.

@gracekarina gracekarina self-assigned this Sep 24, 2021
@gracekarina gracekarina merged commit 254b96c into swagger-api:master Feb 10, 2022
@gracekarina
Copy link
Contributor

thanks for this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants