Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPIV3Parser#getExtensions() not finding extensions on OSGi #1003

Closed
fmoussallam opened this issue Jan 25, 2019 · 10 comments
Closed

OpenAPIV3Parser#getExtensions() not finding extensions on OSGi #1003

fmoussallam opened this issue Jan 25, 2019 · 10 comments

Comments

@fmoussallam
Copy link
Contributor

I am using the swagger-parser-v3 library from within an OSGi bundle.

The issue I am facing is that the ServiceLoader.load(SwaggerParserExtension.class) mechanism uses the current thread's class loader (see https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html#load-java.lang.Class-).

With that default behavior, getExtensions() does not return anything, and I can't parse v2 specifications.

I can workaround this by forcing the current thread classloader to SwaggerParserExtension.getClass().getClassLoader() but that is ugly.

Would it be possible to add a ParseOption to allow selecting the class loader?

Thank you

@gracekarina
Copy link
Contributor

Hi, please provide us with a PR and a failing test where we can see what you mean. Thanks

@fmoussallam
Copy link
Contributor Author

fmoussallam commented Jan 25, 2019

This test would fail for a v2 swagger.json spec:

    public void testIssue1003() {
    	ClassLoader ctcl = Thread.currentThread().getContextClassLoader();
    	try {
    		final ClassLoader cl = new URLClassLoader(new URL[] {});
    		Thread.currentThread().setContextClassLoader(cl);
            assertNotNull(new OpenAPIV3Parser().read("src/test/resources/swagger.json"));
    	} finally {
    		Thread.currentThread().setContextClassLoader(ctcl);
    	}
    }

Problem is where to place that test. OpenAPIV3ParserTest does not have any loadable extension on its classpath anyway.

Are there any tests actually loading extensions?

Thanks

@gracekarina
Copy link
Contributor

Hi, can you place it at swagger-parser module, and test? what do you mean here: Would it be possible to add a ParseOption to allow selecting the class loader?

@cvgaviao
Copy link
Contributor

cvgaviao commented Mar 30, 2019

In order to the new version to work with OSGi you need to install a bundle providing an OSGi ServiceLoader Mediator implementation. The one that I know is Aries SPI Fly.

fmoussallam added a commit to Talend/swagger-parser that referenced this issue Mar 30, 2019
@fmoussallam
Copy link
Contributor Author

I propose #1068 for your review.

The unit test is not great but at least it verifies that both classloaders are given a chance to provide extensions. This was separately tested in an OSGi environment.

gracekarina added a commit that referenced this issue Apr 13, 2019
OpenAPIV3Parser#getExtensions() not finding extensions on OSGi #1003
@gracekarina
Copy link
Contributor

closed by #1068.

@jmini
Copy link
Contributor

jmini commented Apr 24, 2019

@fmoussallam: I have the feeling there is the exact same issue with OpenAPIParser:

protected List<SwaggerParserExtension> getExtensions() {
List<SwaggerParserExtension> extensions = new ArrayList<>();
ServiceLoader<SwaggerParserExtension> loader = ServiceLoader.load(SwaggerParserExtension.class);
Iterator<SwaggerParserExtension> itr = loader.iterator();
while (itr.hasNext()) {
extensions.add(itr.next());
}
extensions.add(0, new OpenAPIV3Parser());
return extensions;
}

Can you confirm this?

@fmoussallam
Copy link
Contributor Author

Humm, definitely looks like the same issue. I was not aware OpenAPIParser was upward compatible with V3 (which I find a little bit confusing I have to say)

@jmini
Copy link
Contributor

jmini commented Apr 25, 2019

@fmoussallam: is this also a problem in an OSGi context? Do you want to push a similar patch for that as well?

@fmoussallam
Copy link
Contributor Author

Sure will do. I would also want to submit another PR for #997 which is trivial. Is that ok?

fmoussallam added a commit to Talend/swagger-parser that referenced this issue Apr 25, 2019
gracekarina added a commit that referenced this issue Apr 28, 2019
OpenAPIV3Parser#getExtensions() not finding extensions on OSGi #1003. Applying same fix to OpenAPIParser
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

No branches or pull requests

4 participants