Skip to content

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

Closed

Description

@fmoussallam

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

Activity

  1. gracekarina commented on Jan 25, 2019

    @gracekarina
    Contributor

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

  2. fmoussallam commented on Jan 25, 2019

    @fmoussallam
    ContributorAuthor

    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

  3. gracekarina commented on Feb 16, 2019

    @gracekarina
    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?

  4. cvgaviao commented on Mar 30, 2019

    @cvgaviao
    Contributor

    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.

  5. fmoussallam commented on Mar 30, 2019

    @fmoussallam
    ContributorAuthor

    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.

  6. added a commit that references this issue on Apr 13, 2019
  7. gracekarina commented on Apr 13, 2019

    @gracekarina
    Contributor

    closed by #1068.

  8. jmini commented on Apr 24, 2019

    @jmini
    Contributor

    @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?

  9. fmoussallam commented on Apr 24, 2019

    @fmoussallam
    ContributorAuthor

    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)

  10. jmini commented on Apr 25, 2019

    @jmini
    Contributor

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

  11. fmoussallam commented on Apr 25, 2019

    @fmoussallam
    ContributorAuthor

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

  12. added a commit that references this issue on Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions