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");
}
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);
}
}
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");
}
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");
}
Aggregations