Search in sources :

Example 1 with SwaggerArchive

use of org.wildfly.swarm.swagger.SwaggerArchive in project wildfly-swarm by wildfly-swarm.

the class SwaggerArchivePreparerTest method testWithSwaggerConfInWebInfClasses.

@Test
public void testWithSwaggerConfInWebInfClasses() {
    JAXRSArchive archive = ShrinkWrap.create(JAXRSArchive.class);
    archive.addResource(MyResource.class);
    archive.addResource(MyOtherResource.class);
    archive.add(new ByteArrayAsset("packages: com.myapp.mysubstuff".getBytes()), "WEB-INF/classes/META-INF/swarm.swagger.conf");
    SwaggerArchivePreparer preparer = new SwaggerArchivePreparer(archive);
    preparer.process();
    SwaggerArchive swaggerArchive = archive.as(SwaggerArchive.class);
    assertThat(swaggerArchive.getResourcePackages()).containsOnly("com.myapp.mysubstuff");
}
Also used : ByteArrayAsset(org.jboss.shrinkwrap.api.asset.ByteArrayAsset) JAXRSArchive(org.wildfly.swarm.jaxrs.JAXRSArchive) SwaggerArchive(org.wildfly.swarm.swagger.SwaggerArchive) Test(org.junit.Test)

Example 2 with SwaggerArchive

use of org.wildfly.swarm.swagger.SwaggerArchive in project wildfly-swarm by wildfly-swarm.

the class SwaggerArchivePreparer method process.

@Override
public void process() {
    if (this.deploymentContext != null && this.deploymentContext.isImplicit()) {
        return;
    }
    if (archive.getName().endsWith(".war")) {
        // Create a JAX-RS deployment archive
        WARArchive deployment = archive.as(WARArchive.class);
        deployment.addModule("io.swagger");
        // Make the deployment a swagger archive
        SwaggerArchive swaggerArchive = deployment.as(SwaggerArchive.class);
        // SWARM-1667: Add the custom CDI extension to the deployment to provide a workaround solution.
        deployment.addModule("org.wildfly.swarm.swagger", "deployment");
        deployment.addAsServiceProvider(Extension.class.getName(), "org.wildfly.swarm.swagger.deployment.SwaggerExtension");
        if (this.title != null) {
            swaggerArchive.setTitle(this.title);
        }
        if (this.description != null) {
            swaggerArchive.setDescription(this.description);
        }
        if (this.packages != null && !this.packages.isEmpty()) {
            swaggerArchive.setResourcePackages(this.packages.toArray(new String[this.packages.size()]));
        }
        if (this.tosUrl != null) {
            swaggerArchive.setTermsOfServiceUrl(this.tosUrl);
        }
        if (this.license != null) {
            swaggerArchive.setLicense(this.license);
        }
        if (this.licenseUrl != null) {
            swaggerArchive.setLicenseUrl(this.licenseUrl);
        }
        if (this.version != null) {
            swaggerArchive.setVersion(this.version);
        }
        if (this.schemes != null && !this.schemes.isEmpty()) {
            swaggerArchive.setSchemes(this.schemes.toArray(new String[this.schemes.size()]));
        }
        if (this.host != null) {
            swaggerArchive.setHost(this.host);
        }
        // get the context root from the deployment and tell swagger about it
        if (this.root != null) {
            swaggerArchive.setContextRoot(this.root);
        } else {
            if (!swaggerArchive.hasContextRoot()) {
                if (deployment.getContextRoot() != null) {
                    swaggerArchive.setContextRoot(deployment.getContextRoot());
                } else {
                    swaggerArchive.setContextRoot(contextPath.get());
                }
            }
        }
        // org.wildfly.swarm package space
        if (!swaggerArchive.hasResourcePackages()) {
            String packageName = null;
            for (Map.Entry<ArchivePath, Node> entry : deployment.getContent().entrySet()) {
                final ArchivePath key = entry.getKey();
                if (key.get().endsWith(".class")) {
                    String parentPath = key.getParent().get();
                    parentPath = parentPath.replaceFirst("/", "");
                    String parentPackage = parentPath.replaceFirst(".*/classes/", "");
                    parentPackage = parentPackage.replaceAll("/", ".");
                    if (parentPackage.startsWith("org.wildfly.swarm")) {
                        SwaggerMessages.MESSAGES.ignoringPackage(parentPackage);
                    } else {
                        packageName = parentPackage;
                        break;
                    }
                }
            }
            if (packageName == null) {
                SwaggerMessages.MESSAGES.noEligiblePackages(archive.getName());
            } else {
                SwaggerMessages.MESSAGES.configureSwaggerForPackage(archive.getName(), packageName);
                swaggerArchive.setResourcePackages(packageName);
            }
        } else {
            SwaggerMessages.MESSAGES.configureSwaggerForSeveralPackages(archive.getName(), Arrays.asList(swaggerArchive.getResourcePackages()));
        }
        // Now add the swagger resources to our deployment
        deployment.addClass(io.swagger.jaxrs.listing.ApiListingResource.class);
        deployment.addClass(io.swagger.jaxrs.listing.SwaggerSerializers.class);
    }
}
Also used : Extension(javax.enterprise.inject.spi.Extension) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) Node(org.jboss.shrinkwrap.api.Node) WARArchive(org.wildfly.swarm.undertow.WARArchive) Map(java.util.Map) SwaggerArchive(org.wildfly.swarm.swagger.SwaggerArchive)

Example 3 with SwaggerArchive

use of org.wildfly.swarm.swagger.SwaggerArchive in project wildfly-swarm by wildfly-swarm.

the class SwaggerArchivePreparerTest method testWithSwaggerConfInRoot.

@Test
public void testWithSwaggerConfInRoot() {
    JAXRSArchive archive = ShrinkWrap.create(JAXRSArchive.class);
    archive.addResource(MyResource.class);
    archive.addResource(MyOtherResource.class);
    archive.add(new ByteArrayAsset("packages: com.myapp.mysubstuff".getBytes()), "META-INF/swarm.swagger.conf");
    SwaggerArchivePreparer preparer = new SwaggerArchivePreparer(archive);
    preparer.process();
    SwaggerArchive swaggerArchive = archive.as(SwaggerArchive.class);
    assertThat(swaggerArchive.getResourcePackages()).containsOnly("com.myapp.mysubstuff");
}
Also used : ByteArrayAsset(org.jboss.shrinkwrap.api.asset.ByteArrayAsset) JAXRSArchive(org.wildfly.swarm.jaxrs.JAXRSArchive) SwaggerArchive(org.wildfly.swarm.swagger.SwaggerArchive) Test(org.junit.Test)

Example 4 with SwaggerArchive

use of org.wildfly.swarm.swagger.SwaggerArchive in project wildfly-swarm by wildfly-swarm.

the class SwaggerArchivePreparerTest method testWithoutSwaggerConf.

@Test
public void testWithoutSwaggerConf() {
    JAXRSArchive archive = ShrinkWrap.create(JAXRSArchive.class);
    archive.addResource(MyResource.class);
    archive.addResource(MyOtherResource.class);
    SwaggerArchivePreparer preparer = new SwaggerArchivePreparer(archive);
    preparer.process();
    SwaggerArchive swaggerArchive = archive.as(SwaggerArchive.class);
    assertThat(swaggerArchive.getResourcePackages()).containsOnly("com.myapp");
}
Also used : JAXRSArchive(org.wildfly.swarm.jaxrs.JAXRSArchive) SwaggerArchive(org.wildfly.swarm.swagger.SwaggerArchive) Test(org.junit.Test)

Aggregations

SwaggerArchive (org.wildfly.swarm.swagger.SwaggerArchive)4 Test (org.junit.Test)3 JAXRSArchive (org.wildfly.swarm.jaxrs.JAXRSArchive)3 ByteArrayAsset (org.jboss.shrinkwrap.api.asset.ByteArrayAsset)2 Map (java.util.Map)1 Extension (javax.enterprise.inject.spi.Extension)1 ArchivePath (org.jboss.shrinkwrap.api.ArchivePath)1 Node (org.jboss.shrinkwrap.api.Node)1 WARArchive (org.wildfly.swarm.undertow.WARArchive)1