SwaggerArchivePreparer implements DeploymentProcessor { @Override public void process() { if (this.deploymentContext != null && this.deploymentContext.isImplicit()) { return; } final String restApplicationPath = getRestApplicationPath(); if (!restApplicationPath.isEmpty()) { String path = contextPath.get() + restApplicationPath; path = path.replaceAll(" if (!Objects.equals(contextPath.get(), path)) { contextPath.set(path); } } if (archive.getName().endsWith(".war")) { WARArchive deployment = archive.as(WARArchive.class); deployment.addModule("io.swagger"); SwaggerArchive swaggerArchive = deployment.as(SwaggerArchive.class); 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); } if (this.root != null) { swaggerArchive.setContextRoot(this.root); } else { if (!swaggerArchive.hasContextRoot()) { if (deployment.getContextRoot() != null) { String path = deployment.getContextRoot(); if (!restApplicationPath.isEmpty()) { path = deployment.getContextRoot() + "/" + restApplicationPath; path = path.replaceAll("/+", "/"); } swaggerArchive.setContextRoot(path); } else { swaggerArchive.setContextRoot(contextPath.get()); } } } if (!swaggerArchive.hasResourcePackages()) { String[] packages = getPackagesForScanning(deployment).toArray(new String[0]); if (packages.length == 0) { SwaggerMessages.MESSAGES.noEligiblePackages(archive.getName()); } else { if (packages.length == 1) { SwaggerMessages.MESSAGES.configureSwaggerForPackage(archive.getName(), packages[0]); } else { SwaggerMessages.MESSAGES.configureSwaggerForSeveralPackages(archive.getName(), Arrays.asList(packages)); } swaggerArchive.setResourcePackages(packages); } } else { SwaggerMessages.MESSAGES.configureSwaggerForSeveralPackages(archive.getName(), Arrays.asList(swaggerArchive.getResourcePackages())); } deployment.addClass(io.swagger.jaxrs.listing.ApiListingResource.class); deployment.addClass(io.swagger.jaxrs.listing.SwaggerSerializers.class); } } @Inject  SwaggerArchivePreparer(Archive archive); @Override void process();  }
@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"); }
@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"); }
@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"); }